0

I am having a problem with post meta. I have a custom meta box for posts. But when I select the option it doesn't work. Neither does this code throw any error. One week ago it worked like charm. Here is all the code I use.

add_action( 'add_meta_boxes', 'themeo_inpost_layout_box' );

function themeo_inpost_layout_box() {

    if ( ! current_theme_supports( 'themeo-inpost-layouts' ) ) {
        return;
    }

    add_meta_box( 'themeo_inpost_layout_box', 'Layout Settings', 'themeo_inpost_layout_box_show', array('post', 'page' ), 'normal', 'high' );

}

function themeo_inpost_layout_box_show() {

    $ini = new Themeo_Meta_Boxes();

    $ini->show_meta_box('themeo_inpost_layout_box');

}
add_action( 'save_post', 'themeo_inpost_layout_save');
/**
 * Save the layout options when we save a post or page.
 *
 * Since there's no sanitizing of data, the values are pulled from identically named keys in $_POST.
 *
 * @since 1.0.0
 *
 * @param int     $post_id Post ID.
 * @param WP_Post $post    Post object.
 * @return void Return early if `themeo_layout` is not a key in `POST` data.
 */
function themeo_inpost_layout_save( $post_id ) {

    if ( ! isset( $_POST['themeo_layout'] ) ) {
        return;
    }

    $data = wp_parse_args(
        $_POST['themeo_layout'],
        array(
            '_themeo_layout'                => '',
            '_themeo_custom_body_class'     => '',
            '_themeo_custom_post_class' => '',
        )
    );

    $data = array_map( 'themeo_sanitize_html_classes', $data );

    themeo_save_custom_fields( $data, 'themeo_inpost_layout_save', 'themeo_inpost_layout_nonce' );

}

function themeo_save_custom_fields( array $data, $nonce_action, $nonce_name, $post ) {

    // Verify the nonce.
    if ( ! isset( $_POST[ $nonce_name ] ) || ! wp_verify_nonce( $_POST[ $nonce_name ], $nonce_action ) ) {
        return;
    }

    // Don't try to save the data under autosave, ajax, or future post.
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return;
    }
    if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
        return;
    }
    if ( defined( 'DOING_CRON' ) && DOING_CRON ) {
        return;
    }
    
    $post = get_post( $post );

    // Don't save if WP is creating a revision (same as DOING_AUTOSAVE?).
    if ( 'revision' === get_post_type( $post ) ) {
        return;
    }

    // Check that the user is allowed to edit the post.
    if ( ! current_user_can( 'edit_post', $post->ID ) ) {
        return;
    }

    // Cycle through $data, insert value or delete field.
    foreach ( (array) $data as $field => $value ) {
        // Save $value, or delete if the $value is empty.
        if ( $value ) {
            update_post_meta( $post->ID, $field, $value );
        } else {
            delete_post_meta( $post->ID, $field );
        }
    }

}

function themeo_sanitize_html_classes( $classes, $return_format = 'input' ) {

    if ( 'input' === $return_format ) {
        $return_format = is_array( $classes ) ? 'array' : 'string';
    }

    $classes = is_array( $classes ) ? $classes : explode( ' ', $classes );

    $sanitized_classes = array_map( 'sanitize_html_class', $classes );

    if ( 'array' === $return_format ) {
        return $sanitized_classes;
    } else {
        return implode( ' ', $sanitized_classes );
    }

}

I don't have any idea why the post meta doesn't get saved. The meta box does show in posts when I save changes, it doesn't save data. Thanks in advance

Your Answer

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

Browse other questions tagged or ask your own question.