0

I already have a meta box in the edit post that has a select list with custom taxonomies. But after i save the post the select list returns it value to none, so i don't know how to save the selected value. Can't solve this as i can't figure other codes that don't deal with this one.

Here's my php that creates the metabox and the select list:

<?
function prfx_custom_meta() {
add_meta_box( 'prfx_meta', __( 'Reproductor 1', 'prfx-textdomain' ),     'prfx_meta_callback', 'post' );
}
add_action( 'add_meta_boxes', 'prfx_custom_meta' );

function prfx_meta_callback( $post ) {
wp_nonce_field( basename( __FILE__ ), 'prfx_nonce' );
$prfx_stored_meta = get_post_meta( $post->ID );
?>

<p>
<label for="Netu" class="prfx-row-title"><?php _e( 'Netu', 'prfx-textdomain' )?></label>
<select name="Netu" id="Netu">
<option value="Ninguno" <?php if ( isset ( $prfx_stored_meta['Ninguno'] ) ) selected(        $prfx_stored_meta['Ninguno'][0], 'Ninguno' ); ?>><?php _e( 'Netu', 'prfx-textdomain' )?></option>';
<option value="Netu" <?php if ( isset ( $prfx_stored_meta['Netu-cast'] ) ) selected(        $prfx_stored_meta['Netu-cast'][0], 'Netu Castellano' ); ?>><?php _e( 'Netu 
Castellano', 'prfx-textdomain' )?></option>';
<option value="Netu-vose" <?php if ( isset ( $prfx_stored_meta['Netu-vose'] ) ) selected(  $prfx_stored_meta['Netu-vose'][0], 'Netu Inglés Subtitulado' ); ?>><?php _e( 
'Netu Inglés Subtitulado', 'prfx-textdomain' )?></option>';
<option value="Netu-latino" <?php if ( isset ( $prfx_stored_meta['Netu-latino'] ) )    selected( $prfx_stored_meta['Netu-latino'][0], 'Netu Latino' ); ?>><?php _e( 'Netu Latino', 'prfx-textdomain' )?></option>';
</select>
</p>

And here's the one that should save the value:

 <?php }
 function prfx_meta_save( $post_id ) {

// Checks save status 
$is_autosave = wp_is_post_autosave( $post_id );
$is_revision = wp_is_post_revision( $post_id );
$is_valid_nonce = ( isset( $_POST[ 'prfx_nonce' ] ) && wp_verify_nonce( $_POST[          'prfx_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false';

// Exits script depending on save status
if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
return;
}

add_action( 'save_post', 'prfx_meta_save' );

if( isset( $_POST[ 'Netu' ] ) ) {
update_post_meta( $post_id, 'Netu', $_POST[ 'Netu' ] );
}

if( isset( $_POST[ 'Ninguno' ] ) ) {
update_post_meta( $post_id, 'Ninguno', $_POST[ 'Ninguno' ] );
}

if( isset( $_POST[ 'Netu-cast' ] ) ) {
update_post_meta( $post_id, 'Netu-cast', $_POST[ 'Netu-cast' ] );
}

if( isset( $_POST[ 'Netu-vose' ] ) ) {
update_post_meta( $post_id, 'Netu-vose', $_POST[ 'Netu-vose' ] );
}

if( isset( $_POST[ 'Netu-latino' ] ) ) {
update_post_meta( $post_id, 'Netu-latino', $_POST[ 'Netu-latino' ] );
}
}
?>

1 Answer 1

0

You have the add_action that's supposed to hook your prfx_meta_save function inside the function itself, so it never gets hooked or called. You need to move the add_action outside the function.

This will never work:

function prfx_meta_save( $post_id ) {
    add_action( 'save_post', 'prfx_meta_save' );
}

Move it outside the function:

function prfx_meta_save( $post_id ) {
    // your save code
}
add_action( 'save_post', 'prfx_meta_save' );
5
  • Done... Still not working...
    – Rodrigo
    Jul 15, 2014 at 2:26
  • how is it not working? is the meta value saved?
    – Milo
    Jul 15, 2014 at 2:32
  • The meta value does not save and returns to none
    – Rodrigo
    Jul 15, 2014 at 2:42
  • are you sure it's not saving? your issue is in the code that sets the previous selected option, not the code that saves the value.
    – Milo
    Jul 15, 2014 at 3:07
  • This code has driven me crazy... made a different question with another code: wordpress.stackexchange.com/questions/154575/…
    – Rodrigo
    Jul 15, 2014 at 3:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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