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!
1234567891011121314151617181920212223242526272829303132333435363738 <?phpadd_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)
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 #3No 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)
img src=”http://i.stack.imgur.com/YnIrA.png” alt=”" />
This is how Im currently outputting the value to my columns…
12345678910111213141516171819202122232425262728 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');elseecho '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));elseecho '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;}}?>
Answer
The error is in you saving function where you are looking for
|
1 |
$_POST['meta_box_musicreleases_linktype'] |
which is never set and should actually be
|
1 |
$_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:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
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
|
1 2 3 4 5 6 7 8 9 10 11 12 |
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
} |



