0

I'm having trouble with my "custom meta box/write panel" script that contains radio buttons. (see below)...It currently does not seem to save the selection made after publishing the custom post type, not sure what Ive missed???

How I plan to utilize this essentially is by letting the client choose one of the radio button choices, and depending on what selection is made I will output that as a class into an anchor tag to help instantiate a specific image overlay.

Thank you for your help!

<?php
add_action( 'add_meta_boxes', 'musicreleases_linktype_meta_box_add' );
function musicreleases_linktype_meta_box_add()
{
    add_meta_box( 'musicreleases_linktype_meta_id', 'Link Type (Optional)', 'musicreleases_linktype_meta_box_cb', 'eprmusicrelease', 'normal', 'low' );
}

function musicreleases_linktype_meta_box_cb( $post )
{
    $values = get_post_custom( $post->ID );
    $radio = isset( $values['meta_box_musicreleases_linktype'] ) ? esc_attr( $values['meta_box_musicreleases_linktype'][0] ) : '';
    wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );
    ?>
    <p>
        <input type="radio" name="linktype" value="freedownload_album" <?php echo ($radio); ?>/> Free<br />
        <input type="radio" name="linktype" value="buyonitunes_album" <?php echo ($radio); ?>/> iTunes<br />
        <input type="radio" name="linktype" value="none_album" <?php echo ($radio); ?>/> None<br />
    </p>
    <?php   
}

add_action( 'save_post', 'musicreleases_linktype_meta_box_save' );
function musicreleases_linktype_meta_box_save( $post_id )
{
    if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
    if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;
    if( !current_user_can( 'edit_post' ) ) return;

    $allowed = array( 
        'a' => array( 
            'href' => array() 
        )
    );

    if( isset( $_POST['meta_box_musicreleases_linktype'] ) )
        update_post_meta( $post_id, 'meta_box_musicreleases_linktype', wp_kses( $_POST['meta_box_musicreleases_linktype'], $allowed ) );
}
?>


EDIT #1

Seems to be working great now, thank you for the help!
Only problem now as I mentioned is its not showing which radio button has been selected after hitting publish...(see below)

enter image description here


EDIT #2

Also curious how it may be possible to output the "pretty version" of each inputs value for my custom columns so that they appear like - "iTunes", rather then "buyonitunes_album".

EDIT #3

No big deal if this isnt possible, but is there any way to potentially make the values appear neater for the columns? ie. "freedownload_album" -> "Free", for example. (See below)

enter image description here

This is how Im currently outputting the value to my columns...

function eprmusicrelease_custom_columns($column){
    global $post;
    switch ($column)
    {
        case "albumthumb":
            if (has_post_thumbnail($post->ID))
                echo get_the_post_thumbnail($post->ID, 'music-release-img-col');
            else
                echo 'No Image Available';
            break;
        case "albumlink":
            if (get_post_meta($post->ID, 'meta_box_musicreleases_url', true))
                echo make_clickable (get_post_meta ($post->ID, 'meta_box_musicreleases_url', true));
            else
                echo 'No URL Chosen';
            break;
        case "linktype":
            echo get_post_meta($post->ID, 'meta_box_musicreleases_linktype', true);
            break;
        case 'author':
            echo get_post_meta( $post->ID , 'author' , true ); 
            break;
        case 'date':
            echo get_post_meta( $post->ID , 'date' , true ); 
            break;
    }
}
?>

1 Answer 1

5

The error is in you saving function where you are looking for $_POST['meta_box_musicreleases_linktype'] which is never set and should actually be $_POST['linktype'], and the use of wp_kses is not right here, if you want to validate the data and you know all of the accepted values the you can simply use in_array try:

add_action( 'save_post', 'musicreleases_linktype_meta_box_save' );
function musicreleases_linktype_meta_box_save( $post_id ){
    if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
    if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;
    if( !current_user_can( 'edit_post' ) ) return;

    //accepted values whitelist
    $allowed = array('free','itunes','none');

    if( isset( $_POST['linktype'] )  && in_array($_POST['linktype'], $allowed))
        update_post_meta( $post_id, 'meta_box_musicreleases_linktype',  $_POST['linktype'] );
}

this way the data will be saved only if the value is in the accepted "whitelist" and anything else will be avoided.

Update

to show the selected value on the backend you need to fix you metabox function which currently i have no idea what you tried to do there but here is a simple fix

function musicreleases_linktype_meta_box_cb( $post )
{
    $value = get_post_meta( $post->ID,'meta_box_musicreleases_linktype',true );
    wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );
    ?>
    <p>
        <input type="radio" name="linktype" value="freedownload_album" <?php echo ($value == 'freedownload_album')? 'checked="checked"':''; ?>/> Free<br />
        <input type="radio" name="linktype" value="buyonitunes_album" <?php echo ($value == 'buyonitunes_album')? 'checked="checked"':''; ?>/> iTunes<br />
        <input type="radio" name="linktype" value="none_album" <?php echo ($value == 'none_album')? 'checked="checked"':''; ?>/> None<br />
    </p>
    <?php   
}
5
  • +1 for validation rather than the unnecessary (for database input) escaping. Apr 30, 2012 at 9:03
  • Good deal, I will give this a shot. Thanks for taking the time to write out an example for me by the way. I thought for sure though that my inputs were potentially written incorrectly as well?...that all still looks good to you?...
    – Mr.Brown
    Apr 30, 2012 at 16:09
  • @bainternet - its definitely working since I'm able to retrieve its output now but its still not showing what selection has been made in the backend after publishing...ie. (all radio buttons appear empty still) see original post for example
    – Mr.Brown
    Apr 30, 2012 at 16:41
  • @bainternet - Again, thanks for the help...Im ready to check your answer off, just need that last bit working first though as I previously stated.
    – Mr.Brown
    May 1, 2012 at 21:53
  • Thank you so much for the help man, it really is appreciated...I originally tried to adapt an existing meta box I had made from an older tutorial for this one which obviously had some stuff in it I wasnt aware didnt need to be there! Thanks again.
    – Mr.Brown
    May 3, 2012 at 2:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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