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 am using the *_add_form_fields action to add fields to a custom taxonomy. One of those fields is a wp_editor().

The problem I am facing is that when I output the WordPress editor on the page like so:

wp_editor('test', 'mydescription', array('textarea_name' => 'my_description')); 

and then if I click in the editor on the page and change the default value from test to something else the $_POST['my_description'] variable is still set to test

Should I be adding an additional setting to my editor? Is there a reason why I cannot change the value of the textarea?

EDIT

Below is a very simple test case that shows this happening. Place this in your functions.php file and then create a new tag. The posted value for 'my_description" will not change.

class Test{

    function __construct() {

        add_action('add_tag_form_fields', array($this, 'add_tag_form_fields'));

        add_action('created_term', array($this, 'created_term'));
    }

    function add_tag_form_fields($tag){

        if ( current_user_can( 'publish_posts' ) ): ?>

        <div class="form-field">
            <?php wp_editor('test', 'mydescription', array('textarea_name' => 'my_description')); ?>
        </div>

        <?php
    }

    function created_term($tag){
        echo '<pre>';
        print_r($_POST);
        echo '</pre>';
            die();
    }
}
new Test();

EDIT

This ONLY happens when attaching to "created_term" action. If you attach to "edited_terms" it works as expected and I think this is a result of ajax being used on the create term page... I have updated the test code to show this.

share|improve this question
    
It did change for me: Array ( [action] => add-tag [screen] => edit-post_tag [taxonomy] => post_tag [post_type] => post [_wpnonce_add-tag] => 4f7d3df228 [_wp_http_referer] => /wp-admin/edit-tags.php?taxonomy=post_tag [tag-name] => Test99 [slug] => [description] => [my_description] => something else ), maybe something else in your code a problem? –  Rob Vermeer Jan 23 '12 at 22:50
    
see second edit. It appears that this only happens when you "add" a term. –  Mike Jan 24 '12 at 16:58
add comment

3 Answers

up vote 5 down vote accepted
+50

tinyMCE textarea is initially unseen by the used serialize function:

$.post(ajaxurl, $('#addtag').serialize(), function(r){ ....

you will need to call tinyMCE.triggerSave() to make it 'visible'

a simple script with:

jQuery('#submit').mousedown( function() {
    tinyMCE.triggerSave();
    }); 

should do the trick. This in an external file, enqueued with wp_enqueue_script worked in my test.

share|improve this answer
    
This does not work for me. It appears the data is posted, THEN your click function code is triggered. –  Mike Jan 26 '12 at 19:47
    
did you enqueued the script in the head? if you enqueue it in the footer, it will be loaded after wp-scripts and that'd be too late. –  ungestaltbar Jan 26 '12 at 20:15
    
instead of 'click' you actually could bind it to $('#submit').mousedown( ... ) :) in that case it shouldn't matter where and when the script is loaded. –  ungestaltbar Jan 26 '12 at 20:29
    
Thanks. You saved me after I lost hours of debuggind my plugin with no success. –  Smartik Oct 9 '13 at 13:46
add comment

In your edited_terms function you need to save the value and in your add_tag_form_fields you need replace your test with the saved data.

something like:

class Test{

    function __construct() {

        //do_action('add_tag_form_fields', $taxonomy);
        add_action('add_tag_form_fields', array($this, 'add_tag_form_fields'));

        //do_action("edited_terms", $term_id, $tt_id, $taxonomy);
        add_action('edited_terms', array($this, 'edited_terms'));
    }

    function add_tag_form_fields($term){

        if ( current_user_can( 'publish_posts' ) ): ?>

        <div class="form-field">
            <?php 
            $saved = get_option('termmeta_'.$term->term_id);
            $saved = (empty($saved))? 'test': $saved;
            wp_editor($saved, 'mydescription', array('textarea_name' => 'my_description')); ?>
        </div>

        <?php
    }

    function edited_terms($term_id){
        if (isset($_POST['mydescription'])){
            update_option('termmeta_'.$term_id,$_POST['mydescription']);
        }
    }
}
new Test();

Now if you want a much easier way of adding extra fields of all types to your tags/categories or custom taxonomy edit forms without reinventing the wheel take a look at TAX Meta Class

share|improve this answer
    
See my second comment, it appears that this only happens when adding a term. And I think this is a result of it using ajax. –  Mike Jan 24 '12 at 16:59
    
@Mike what Ajax where are you using Ajax? –  Bainternet Jan 24 '12 at 17:47
    
When you add a new category/term WordPress uses ajax to post the data and create the new term. For some reason this does not allow the wp_editor element to have the correct value. Try the test code in the original question. –  Mike Jan 24 '12 at 19:01
add comment

According to the codex for wp_editor the first argument should be the content. So rather than 'test' you should put $_POST['my_description'] if that is what you would like the initial content to be.

share|improve this answer
1  
the issue is not with the content showing up correctly. It is with the posted variable being the same as the original content even if you change the textarea content to something else. –  Mike Jan 20 '12 at 19:38
add comment

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.