17

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.

2
  • 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? Commented Jan 23, 2012 at 22:50
  • see second edit. It appears that this only happens when you "add" a term.
    – Mike
    Commented Jan 24, 2012 at 16:58

3 Answers 3

25
+50

tinyMCE <textarea> element is initially unseen by the used serialize function:

$.post(
    ajaxurl,
    $('#addtag').serialize(), function(r) {
        // Content here.
    }
});

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

Below is a simple snippet that should do the trick:

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

This in an external file, enqueued with wp_enqueue_script(); it worked for the test I've conducted.

4
  • 1
    This does not work for me. It appears the data is posted, THEN your click function code is triggered.
    – Mike
    Commented Jan 26, 2012 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. Commented Jan 26, 2012 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. Commented Jan 26, 2012 at 20:29
  • Worked like a charm thanks!
    – nitigyan
    Commented Mar 22, 2019 at 19:49
4

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

3
  • 1
    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
    Commented Jan 24, 2012 at 16:59
  • @Mike what Ajax where are you using Ajax?
    – Bainternet
    Commented Jan 24, 2012 at 17:47
  • 1
    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
    Commented Jan 24, 2012 at 19:01
0

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.

1
  • 2
    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
    Commented Jan 20, 2012 at 19:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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