0

I have added a custom post page option but it's not working; the output is always empty.

I have this debug code in my loop (the code that retrieves the information in frontend):

$colorvalue = get_post_meta( get_the_ID(), 'slider_is_dark', 1 );

echo '<script>alert("' . $colorvalue . '");</script>';

Then I have this in my functions.php:

add_action( 'add_meta_boxes', 'slider_checkboxes' );
function slider_checkboxes() {
    add_meta_box(
        'slider',
        'Slider',
        'slider_box_content',
        'post',
        'normal',
        'default'
    );
}

function slider_box_content( $post_id ) {
    wp_nonce_field( plugin_basename( __FILE__ ), 'slider_nonce' );

    echo '<input type="checkbox" name="slider_is_dark" value="1" /> Slider is dark';
}

add_action( 'save_post', 'slider_data' );
function slider_data() {
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
        return;

    if ( !wp_verify_nonce( $_POST['slider_nonce'], plugin_basename( __FILE__ ) ) )
        return;

    if ( isset( $_POST['slider_is_dark'] ) )
        update_post_meta( $post_id, 'slider_is_dark', 1 );
    else
        update_post_meta( $post_id, 'slider_is_dark', 0 );
}

The alert always returns empty. Is there a problem in my code or am I doing it wrong?

0

You need the $post_id passed in to the function you are calling from the hook. Try making your function declaration like this:

function slider_data( $post_id ) 
| improve this answer | |

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.