The add_meta_boxes_link WordPress action fires when link-specific meta boxes are added.
Usage
add_action('add_meta_boxes_link', 'your_function_name');
function your_function_name($link) {
// your custom code here
}
Parameters
- $link (object) – The Link object.
More information
See WordPress Developer Resources: add_meta_boxes_link
Examples
Adding a custom meta box for links
Create a custom meta box for link objects in the WordPress admin:
add_action('add_meta_boxes_link', 'add_custom_link_meta_box');
function add_custom_link_meta_box($link) {
add_meta_box('custom_link_meta_box', __('Custom Link Meta Box'), 'display_custom_link_meta_box', 'link', 'normal', 'high');
}
Displaying the custom meta box
Display the custom meta box with a simple input field:
function display_custom_link_meta_box($link) {
$custom_value = get_post_meta($link->ID, 'custom_value', true);
echo '<input type="text" name="custom_value" value="' . esc_attr($custom_value) . '" />';
}
Saving the custom meta box data
Save the custom meta box data when the link is updated:
add_action('edit_link', 'save_custom_link_meta_box_data');
function save_custom_link_meta_box_data($link_id) {
if (isset($_POST['custom_value'])) {
update_post_meta($link_id, 'custom_value', sanitize_text_field($_POST['custom_value']));
}
}
Removing the default meta boxes
Remove some default meta boxes for link objects:
add_action('add_meta_boxes_link', 'remove_default_link_meta_boxes');
function remove_default_link_meta_boxes($link) {
remove_meta_box('linkxfndiv', 'link', 'normal');
remove_meta_box('linkcategorydiv', 'link', 'normal');
}
Adding multiple custom meta boxes
Add multiple custom meta boxes for link objects:
add_action('add_meta_boxes_link', 'add_multiple_custom_link_meta_boxes');
function add_multiple_custom_link_meta_boxes($link) {
add_meta_box('custom_link_meta_box_1', __('Custom Link Meta Box 1'), 'display_custom_link_meta_box_1', 'link', 'normal', 'high');
add_meta_box('custom_link_meta_box_2', __('Custom Link Meta Box 2'), 'display_custom_link_meta_box_2', 'link', 'normal', 'high');
}