Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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() {
        add_meta_box(
                        'my-meta-box',
                        'Ajouter le logo de la société',
                        'mytheme_show_box',
                        'link',
                        'normal',
                        'high'
        );
}

// 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" ?>
        <?php
}


// 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_enqueue_script('media-upload');
        wp_enqueue_script('thickbox');
        wp_register_script('my-upload', get_bloginfo('template_url') . '/functions/my-script.js', array('jquery','media-upload','thickbox'));
        wp_enqueue_script('my-upload');
}
function my_admin_styles() {
        wp_enqueue_style('thickbox');
}
add_action('admin_print_scripts', 'my_admin_scripts');
add_action('admin_print_styles', 'my_admin_styles');

Thank you for your help !

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

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')
share|improve this answer
    
Thank you, your post helped me a lot. I have a problem though that I just found... if it's a link I created before the meta box worked, it lets me save the data, and it doesn't if it is new (even if I update it next), any idea why ? –  matoma16 Feb 29 '12 at 14:43
    
It's hard to say. Temporarily, try dying on a failure of a check rather than returning, e.g. wp_die('reason for failure'). That way you can determine which check your a failing on, and that might help determine the problem. –  Stephen Harris Feb 29 '12 at 15:12
    
I did and still can't see the problem. no problem while checking nonce or if I can manage links, it goes in my first 'if'-no previous meta value in db-and none of my var given in add_post_meta are empty. it gives me the same thing when editing the box to one of friday's post but saves the link. did I get my add_post_meta wrong for links? 'if ( $new_meta_value && '' == $meta_value ){ add_post_meta( $post_id, $meta_key, $new_meta_value, true ); // wp_die($post_id.' '. $meta_key.' '.$new_meta_value); }' Only showing the if part, not enough space but i can give the whole code –  matoma16 Feb 29 '12 at 15:49
    
Doh! Links are post types, so you can't use _post_meta functions. In fact, I don't think you can store link meta data unless you store it in your options table. –  Stephen Harris Feb 29 '12 at 16:09
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.