Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to create a meta box that will let me upload image in a custom post. Everything is working almost fine but... It doesn't save any data -.-' I can't figure what I'm doing wrong. Could you please take a look and explain me my mistake? Thank you.

This is my post_type: fi_bi_po_ho (yes it's correct)

/*
 * Let's add a useful metabox that will help you add images in homepage custom posts
 */
/**
 * Adds a meta box to the post editing screen
 */
function etpub_custom_meta() {
    add_meta_box( 'etpub_meta', __( 'Upload your image here', 'etpub-textdomain' ), 'etpub_meta_callback', 'fi_bi_po_ho' );
}
add_action( 'add_meta_boxes', 'etpub_custom_meta' );

/**
 * Outputs the content of the meta box
 */
function etpub_meta_callback( $post ) {
wp_nonce_field( basename( __FILE__ ), 'etpub_nonce' );
$etpub_stored_meta = get_post_meta( $post->ID );
?>

    <p>
        <label for="meta-image" class="etpub-row-title"><?php _e( 'Example File Upload', 'etpub-textdomain' )?></label>
        <input type="text" name="meta-image" id="meta-image" value="<?php if ( isset ( $etpub_stored_meta['meta-image'] ) ) echo $etpub_stored_meta['meta-image'][0]; ?>" />
        <input type="button" id="meta-image-button" class="button" value="<?php _e( 'Choose or Upload an Image', 'etpub-textdomain' )?>" />
    </p>

<?php
}

/**
 * Adds the meta box stylesheet when appropriate
 */
function etpub_admin_styles(){
    global $typenow;
    if( $typenow == 'fi_bi_po_ho' ) {
        wp_enqueue_style( 'etpub_meta_box_styles', get_template_directory_uri() . '/css/meta.css' );
    }
}
add_action( 'admin_print_styles', 'etpub_admin_styles' );
/**
 * Loads the image management javascript
 */
function etpub_image_enqueue() {
    global $typenow;
    if( $typenow == 'fi_bi_po_ho' ) {
        wp_enqueue_media();

        // Registers and enqueues the required javascript.
        wp_register_script( 'meta-box-image', get_template_directory_uri() . '/js/meta.js', array( 'jquery' ) );
        wp_localize_script( 'meta-box-image', 'meta_image',
            array(
                'title' => __( 'Choose or Upload an Image', 'etpub-textdomain' ),
                'button' => __( 'Use this image', 'etpub-textdomain' ),
            )
        );
        wp_enqueue_script( 'meta-box-image' );
    }
}
add_action( 'admin_enqueue_scripts', 'etpub_image_enqueue' );

/**
 * Saves the custom meta input
 */
function etpub_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[ 'etpub_nonce' ] ) && wp_verify_nonce( $_POST[ 'etpub_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false';

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

    // Checks for input and sanitizes/saves if needed
    if( isset( $_POST[ 'meta-text' ] ) ) {
        update_post_meta( $post_id, 'meta-text', sanitize_text_field( $_POST[ 'meta-text' ] ) );
    }
    // Checks for input and saves if needed
    if( isset( $_POST[ 'meta-image' ] ) ) {
        update_post_meta( $post_id, 'meta-image', $_POST[ 'meta-image' ] );
    }

}
add_action( 'save_post', 'etpub_meta_save' );
share|improve this question
    
Are you sure that your etpub_meta_save function is passing validation? Try adding die("Validation passed"); just before updating your update_post_meta() calls. Also, remove isset( $_POST[ 'etpub_nonce' ] ) from the nonce vitrification check, as if it's not set, it won't be valid, thus it's unnecessary. –  David Gard Feb 17 at 16:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.