• Resolved gavimobile

    (@gavimobile)


    hi folks, i have added an additional visual editor with wp_editor to my custom post type in a meta box. it looks fine i just cannot manage to save anything. it might be saving, but it for sure is’nt retrieving. Ive been looking for hours online with no success. i am using the latest version of wp. 3.4.x

    <?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('', '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">0</span></td>
    	<td class="autosave-info">
    	<span class="autosave-message">?</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
        }
    }
    ?>

    thanks in advance!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter gavimobile

    (@gavimobile)

    wow, cant believe i missed it. here is a sample code to allow multiple visual editors (wp_editor or wysiwyg) using tinymce within a metabox.
    this will add the metabox to post’s

    <?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', 'post', '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
        }
    }
    ?>

    What’s different between these too? It’s hard to sift through all of that at a glance.

    Thread Starter gavimobile

    (@gavimobile)

    s3w47m88!
    please note that this thread is marked as resolved!

    thanks anyways ??

    Hi gavimobile,

    I hope this doesn’t come off rude, but I am aware it was marked as resolved. However, you didn’t clearly state what you did to resolve it. So I was hoping you would take a moment to enlighten me, and any others who face this same issue, so this forum topic helps the community.

    Thanks for your time.

    Thread Starter gavimobile

    (@gavimobile)

    s3w47m88,
    This thread was created 5 months ago and I don’t remember what I did. Do you really need to know the difference between the two? I shared the second one because that was the working solution. I always attempt to help others by showing the code that I used to get things working. In addition, I added lots of comments which should explain things! Let me know if you need to know the difference between the working one and the non working one and it i have time ill go through It and see if I can find something. A

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘multiple instances of tinymce with wp_editor with custom post_type – cant save’ is closed to new replies.