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.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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.
Method 2
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
Method 3
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.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0