WordPress.org

Ready to get started?Download WordPress

Forums

meta_box problem. Update works on some posts but not others! (7 posts)

  1. s1ngular1ty
    Member
    Posted 2 years ago #

    I have a problem with a plugin I am developing.

    I had added a metabox to the new_post/edit post page as follows:

    http://pastebin.com/k3BjzG6L

    Now this should create a new meta box with a wp_editor. Then on the save post action the data from the wp_editor should be saved to a new meta field. And the wp_editor box for that post takes that saved data and displays it as the new default content of the wp_editor.

    The bizzare thing is that this code works fine for new posts but when I try to update posts for some posts it works and for others not!

    When it doesnt work the metabox just refuses to update.

    I have tried other standard input fileds like radio buttons and checkboxes instead of wp_editor and I have the same issue.

    Please if someone can have a look at this and let me know what is going on I would really appreciate it.

  2. s1ngular1ty
    Member
    Posted 2 years ago #

    Having played around with this issue a litte more it seems my problem lies with this section of code:

    $vclLockedContent = $_POST['vclLockedContent'];  //lockedcontent
            if (get_post_meta($post_id, 'vclLockedContent', TRUE) != ''){
                    update_post_meta($post_id, 'vclLockedContent',$vclLockedContent);
            }
            else{
                    add_post_meta($post_id, 'vclLockedContent', $vclLockedContent);
            }

    It seems that when I remove all content from my wp_editor box and save it, it means it no longer does an update_post_meta() but always tries to add_post_meta() instead. At least that is my diagnosis.

    Can anyone suggest a better way of writing this if statement? I need a way of determing whether to do an add_post_meta or an update_post_meta.

  3. gavimobile
    Member
    Posted 2 years ago #

    ok, i got it! here is an example of a meta box with a tinymce editor or visual edior in the post_type. i have not spotted an issue yet. seems to be working flawless!.

    <?php
    add_action( 'add_meta_boxes', 'mpc_add_description_meta_box' );
    // Add the products Meta Box
    function mpc_add_description_meta_box() {
        add_meta_box('mpc_add_description_meta_box', 'Product Description (Language 2)', 'mpc_add_description_meta_box_callback', 'products', 'normal', 'high');
    }
    
    // the description output
    function mpc_add_description_meta_box_callback() {
        global $post;
        // Noncename needed to verify where the data originated
        echo '<input type="hidden" name="mpc_products_description_noncename" id="mpc_products_description_noncename" value="'.wp_create_nonce(plugin_basename(__FILE__)).'" />';
        // Get the location data if its already been entered
        $input = get_post_meta($post->ID, 'mpc_products_description', true);
       // echo '<input type="text" name="mpc_products_description" value="' . $input  . '" "  />';
    	wp_editor($input, 'mpc_products_description', array('textarea_name' => 'mpc_products_description', 'editor_css' => '<style>#wp-mpc_products_description-editor-container{background-color:white;style="width:100%;}</style>'));
    
    	echo '<table id="post-status-info" cellspacing="0"><tbody><tr><td id="wp-word-count">Word count: <span class="word-count_2">';
    	$words = strip_tags($input);
    	$count = str_word_count($words, 0);
    	echo $count;
    	echo '</span></td>
    	<td class="autosave-info">
    	<span class="autosave-message">&nbsp;</span>
    	</td>
    	</tr></tbody></table>';
    }
    
    //save the data
    add_action('save_post', 'mpc_save_description_meta', 1, 2); // save the custom fields
    function mpc_save_description_meta($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['mpc_products_description_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.
        $mpc_description_meta['mpc_products_description'] = $_POST['mpc_products_description'];
        // Add values of $mpc_description_meta as custom fields
        foreach ($mpc_description_meta as $key => $value) { // Cycle through the $mpc_description_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
        }
    }
    ?>
  4. wspencer
    Member
    Posted 2 years ago #

    Am I mistaken, or does your code only allow for pages to be edited and not posts?

    Also, you can use the WordPress function check_admin_referer() to help with the authentication.

  5. gavimobile
    Member
    Posted 2 years ago #

    mspencer, it was created for custom post type named products. Channing where it says products to page or post on line 5 will allow this code to work for both pages or posts.
    I need to rewrite my comments, they were built off of other scripts/tutorials I found online and applied it for my needs.
    I will check out that function to see what it can do. Thanks

  6. Chase Wiseman
    Member
    Posted 2 years ago #

    I wanted to chime in on this after the fact based on the code you posted. Just for future reference, you don't have to do this:

    if ( get_post_meta( ... ) != ' ' )
        update_post_meta( ... );
    else
       add_post_meta( ... );

    You can simply use update_post_meta( ... ); by itself, which handles that logic for you. Of course, there is nothing wrong with the way you are doing it, but simplifying it may help you avoid future headaches.

  7. gavimobile
    Member
    Posted 2 years ago #

    Chase. thanks for the tip! just saw your post now.
    s1ngular1ty, has your issue been solved? it would be great if you can set the status of this topic to resolved unless your problem has not been solved yet.

Topic Closed

This topic has been closed to new replies.

About this Topic