Using WordPress ‘add_meta_boxes_link’ PHP action

The add_meta_boxes_link WordPress PHP action fires when link-specific meta boxes are added. It allows you to add custom meta boxes to the link editing screen.

Usage

add_action('add_meta_boxes_link', 'your_custom_function');
function your_custom_function($link) {
    // your custom code here
}

Parameters

  • $link (object): The link object being edited.

More information

See WordPress Developer Resources: add_meta_boxes_link

Examples

Add a custom meta box to the link editing screen to allow users to add a description to links.

add_action('add_meta_boxes_link', 'add_link_description_meta_box');
function add_link_description_meta_box($link) {
    add_meta_box('link_description', 'Link Description', 'link_description_callback', null, 'normal', 'high');
}

function link_description_callback($link) {
    $link_description = get_post_meta($link->link_id, '_link_description', true);
    echo '<textarea name="link_description" id="link_description" rows="5" cols="30">' . esc_textarea($link_description) . '</textarea>';
}

Add a custom meta box to the link editing screen to allow users to set the link target attribute (e.g., “_blank” for opening in a new tab).

add_action('add_meta_boxes_link', 'add_link_target_meta_box');
function add_link_target_meta_box($link) {
    add_meta_box('link_target', 'Link Target', 'link_target_callback', null, 'normal', 'high');
}

function link_target_callback($link) {
    $link_target = get_post_meta($link->link_id, '_link_target', true);
    echo '<select name="link_target" id="link_target">';
    echo '<option value="_self" ' . selected($link_target, '_self', false) . '>Same tab</option>';
    echo '<option value="_blank" ' . selected($link_target, '_blank', false) . '>New tab</option>';
    echo '</select>';
}