Problem with meta box in Links

I have troubles saving the data of the meta box in the link part of the admin.
My code is working, because when I try it in the post or page part, it saves the data, but in Links it just won’t.

Here is my code. Should I write something else instead of ‘save_post’ ? I saw some used ‘save_link’ ans tried even though people replying said it didn’t exist, but it doesn’t work…
What I mean by that is that it works when I add it to post or page, but when I change ‘page’ (or ‘post’) to ‘link’ when I add the meta box, nothing is saved, and my input comes back blank when I go to the link form again, when I should have the link of my image. The data isn’t saved in the database either and I have no idea why.

Here is my code :

    add_action( 'admin_init', 'meta_boxes_setupa' );
function meta_boxes_setupa() {
    add_action( 'add_meta_boxes', 'mytheme_add_box' );
    add_action( 'save_post', 'mytheme_save_data', 10, 2 );
// add_action('admin_menu', 'mytheme_add_box');
// Add meta box
function mytheme_add_box() {
                        'Ajouter le logo de la société',

// Callback function to show fields in meta box
function mytheme_show_box( $object, $box ) {
        // global $post;
        // echo '<input type="hidden" name="mytheme_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
        // $meta = get_post_meta($post->ID, '_upload_image', true);
        <?php wp_nonce_field( basename( __FILE__ ), 'url_nonce' ); ?>
                <label for="upload_image">Sélectionner le logo : </label>
                <input type="text" name="upload_image" id="upload_image" value="<?php echo esc_attr( get_post_meta( $object->ID, '_upload_image', true ) ); ?>" size="40" readonly="readonly" />
                <input type="button" name="upload_image_button" id="upload_image_button" value="Parcourir" ?>

// Save data from meta box
function mytheme_save_data($post_id, $post) {
        if ( !isset( $_POST['url_nonce'] ) || !wp_verify_nonce( $_POST['url_nonce'], basename( __FILE__ ) ) )
        return $post_id;

    $post_type = get_post_type_object( $post->post_type );
    /* Check if the current user has permission to edit the post. */
    if ( !current_user_can( $post_type->cap->edit_post, $post_id ) )
        return $post_id;

    /* Get the posted data and sanitize it for use as an HTML class. */
        $new_meta_value = ( isset( $_POST['upload_image'] ) ? sanitize_html_class( $_POST['upload_image'] ) : '' );
    /* Get the meta key. */
    $meta_key = '_upload_image';
    /* Get the meta value of the custom field key. */
    $meta_value = get_post_meta( $post_id, $meta_key, true );
    /* If a new meta value was added and there was no previous value, add it. */
    if ( $new_meta_value && '' == $meta_value )
        add_post_meta( $post_id, $meta_key, $new_meta_value, true );
    /* If the new meta value does not match the old value, update it. */
    elseif ( $new_meta_value && $new_meta_value != $meta_value )
       update_post_meta( $post_id, $meta_key, $new_meta_value );
    /* If there is no new meta value but an old value exists, delete it. */
    elseif ( '' == $new_meta_value && $meta_value )
        delete_post_meta( $post_id, $meta_key, $meta_value );

function my_admin_scripts() {
        wp_register_script('my-upload', get_bloginfo('template_url') . '/functions/my-script.js', array('jquery','media-upload','thickbox'));
function my_admin_styles() {
add_action('admin_print_scripts', 'my_admin_scripts');
add_action('admin_print_styles', 'my_admin_styles');

Thank you for your help !

Solutions Collecting From Web of "Problem with meta box in Links"

The hook you are after is edit_link (I couldn’t find any documentation). It’s fired from inside wp_insert_link (itself fired whenever a link is created or updated).

The action only passes one argument, the link’s ID, to the callback. So you would need to change 2 to 1, on your add_action call:

add_action( 'edit_link', 'mytheme_save_data', 10, 1 );

and remove the $post variable from your callback function:

function mytheme_save_data($post_id) {
     //Your function here

Other differences….

  1. In your metabox callback function (that displays the upload button etc). you use $object->ID. For links this should be $object->link_id.
  2. In your edit_link callback, you use $post to get the post type. This variable is no longer available to you and in any case links are not post types
  3. Because of the above, you’ll have to alter how you check permissions, I think what you want to check is current_user_can('manage_links')