• fedegonzaleznavarro

    (@fedegonzaleznavarro)


    Hello, thankyou for checking this out. Well, basically what I’ve done is the following.

    I made a plugin that creates a Custom Post called “Productos”.
    After registering the Custom Post I create a Custom Meta Box to be in the Editor so I can get the user to input 4 fields: Title(input), Description(textarea), Small Image URL(input), Large Image URL (input).

    Up to that point everything works great. I’m able to see the Custom Meta Box in the Edit Page and input data, but when I update the page it dissapears. I seem to have a problem saving the data of those input fields.

    Note: My knowledge of PHP is limited( let’s say 3 out of 10), I’ve followed many tutorials and I can find my way around wordpress alright. This code I’m using I recycled it from other projects I’ve done and it works, I seem to have a syntax problem or maybe I’m overlooking something.

    Bellow I will paste the code for the Custom Meta Box, but I will also paste a link to pastebin.com with the full content of my plugin file, just in case.

    I want to thank you if you are taking the time to check it out and give me a hand.

    Link to the Plugin File in Pastebin

    // Admin Meta Box for Products
    
    add_action( 'add_meta_boxes', 'product_data_box' );
    function product_data_box() {
        add_meta_box(
            'product_data_box',
            __( 'Product Information', 'myplugin_textdomain' ),
            'product_data_box_content',
            'productos',
            'normal',
            'core',
            'low'
        );
    }
    
    // The Product Admin  Metabox
    
    function product_data_box_content() {
        global $post;
    
        // Noncename needed to verify where the data originated
        echo '<input type="hidden" name="product_noncename" id="product_noncename" value="' . wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
    
        // Get the location data if its already been entered
        $product_title = get_post_meta($post->ID, 'product_title', true);
        $product_desc = get_post_meta($post->ID, 'product_desc', true);
        $small_img = get_post_meta($post->ID, 'small_img', true);
        $large_img = get_post_meta($post->ID, 'large_img', true);
    
        // Echo out the field
        echo '<label>Titulo del Producto';
        echo '<input type="text" name="product_title" value="' . $product_title  . '" class="widefat" />';
        echo '</label>';
        echo '<label>Descripción del Producto';
        echo '<textarea type="text" name="product_desc" class="widefat" >' . $product_desc . '</textarea>';
        echo '</label>';
        echo '<label>Imagen Pequeña';
        echo '<input type="text" name="small_img"  value="' . $small_img . '" class="widefat" />';
        echo '</label>';
        echo '<label>Imagen Grande';
        echo '<input type="text" name="large_img" value="' . $large_img  . '" class="widefat" />';
        echo '</label>';
    
    }
    
    // Save the Metabox Data
    
    function events_meta_save($post_id, $post) {
    
        // verify this came from the our screen and with proper authorization,
        // because save_post can be triggered at other times
        if ( !wp_verify_nonce( $_POST['eventmeta_noncename'], plugin_basename(__FILE__) )) {
        return $post->ID;
        }
    
        // Is the user allowed to edit the post or page?
        if ( !current_user_can( 'edit_post', $post->ID ))
            return $post->ID;
    
        // OK, we're authenticated: we need to find and save the data
        // We'll put it into an array to make it easier to loop though.
    
        $events_meta['product_title'] = $_POST['product_title'];
        $events_meta['product_desc'] = $_POST['product_desc'];
        $events_meta['small_img'] = $_POST['small_img'];
        $events_meta['large_img'] = $_POST['large_img'];
    
        // Add values of $events_meta as custom fields
    
        foreach ($events_meta as $key => $value) { // Cycle through the $events_meta array!
    
            if( $post->post_type == 'revision' ) return; // Don't store custom data twice
    
            $value = implode(',', (array)$value); // If $value is an array, make it a CSV (unlikely)
    
            if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value
    
                update_post_meta($post->ID, $key, $value);
    
            } else { // If the custom field doesn't have a value
                add_post_meta($post->ID, $key, $value);
            }
            if(!$value) delete_post_meta($post->ID, $key); // Delete if blank
        }
    
    }
  • The topic ‘Saving from Custom Admin Meta Box not working’ is closed to new replies.