The link_target_meta_box WordPress PHP function displays form fields for changing the link target in the WordPress admin area.
Usage
link_target_meta_box( $link );
Input:
$link = (object) array(
'link_target' => '_blank'
);
Output: Displays the form fields for changing the link target in the WordPress admin area.
Parameters
$link(object) – Required. The current link object.
More information
See WordPress Developer Resources: link_target_meta_box
Examples
Displaying link target options for a custom link
// Create a link object
$link = (object) array(
'link_target' => '_blank'
);
// Use the link_target_meta_box function to display form fields
link_target_meta_box( $link );
Displaying link target options for a menu item
// Get the menu item object
$menu_item = wp_setup_nav_menu_item( $menu_item_id );
// Create a link object from the menu item
$link = (object) array(
'link_target' => $menu_item->target
);
// Display the form fields for changing the link target
link_target_meta_box( $link );
Adding link target options to a custom admin page
// Custom function to render the link target options
function my_admin_page() {
// Create a link object
$link = (object) array(
'link_target' => '_blank'
);
// Display the form fields
link_target_meta_box( $link );
}
// Add the custom admin page
add_action( 'admin_menu', function() {
add_submenu_page( 'options-general.php', 'My Admin Page', 'My Admin Page', 'manage_options', 'my-admin-page', 'my_admin_page' );
});
Adding link target options to a widget form
// Custom function to render the link target options in a widget form
function my_widget_form( $instance ) {
// Get the link target from the widget instance
$link_target = ! empty( $instance['link_target'] ) ? $instance['link_target'] : '';
// Create a link object
$link = (object) array(
'link_target' => $link_target
);
// Display the form fields
link_target_meta_box( $link );
}
Displaying link target options for a custom post type
// Custom function to render the link target options in a meta box
function my_custom_post_type_meta_box( $post ) {
// Get the link target from post meta
$link_target = get_post_meta( $post->ID, '_my_link_target', true );
// Create a link object
$link = (object) array(
'link_target' => $link_target
);
// Display the form fields
link_target_meta_box( $link );
}
// Add the meta box to the custom post type
add_action( 'add_meta_boxes', function() {
add_meta_box( 'my-custom-post-type-meta-box', 'Link Target', 'my_custom_post_type_meta_box', 'my_custom_post_type', 'side', 'default' );
});