Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

replacing textarea with wp_editor() WordPress

  • SOLVED

I have a third party PlugIn that generate a metafield-textarea on a custom backend page. I want to have wp_editor() for this textarea in the backend.

I am looking for a solution to implement wp_editor() without changing the code of the third party plugin only with a custom Function in the function.php.

Any Help is appreciated.

Answers (3)

2013-01-06

Dbranes answers:

If you have the id value of the textbox, you can use the javascript command

tinyMCE.execCommand('mceAddControl', false, 'textbox_id');


to add the tinyMCE buttons to it dynamically.



Peter Brain comments:

I have the ID of the textarea. Can you example a function for the JS command


Dbranes comments:

here is a simple example that you could test in your <em>functions.php</em> file:

add_action('admin_footer','my_tinyMCE');
function my_tinyMCE(){
echo "
<script>
jQuery(document).ready(function() {
var my_textarea_id ='textarea_id'; // change this to match your textarea id
if(jQuery('#'+my_textarea_id).length){
tinyMCE.execCommand('mceAddControl', false, my_textarea_id);
}
});
</script>
";
}


Peter Brain comments:

This work but the editor collide with the main tinyMCE instance. I use tiny MCE Advance and now the table edit buttons don't work. Is there any workaround to avoid this ?


Dbranes comments:

it seems to work well on my wp 3.5 install with tinyMCE Advanced:

[[LINK href="http://i.imgur.com/047WE.jpg"]]http://i.imgur.com/047WE.jpg[[/LINK]]

I can edit tables in main editor and sub editors.


Peter Brain comments:

I got it. The issue depends on the Mime Types settings in my .htaccess

My last issue is that the metabox append linebreaks automatically to the content. e.g.

in the editor the html look like:
<h2>text</h2>
<table>
...

The page with content is rendered like:
<h2>text</h2>
<br>
<br>
<br>
<table>

Any idea how to stop this ?


Dbranes comments:

this is the reason:

$attribute_value = str_replace("\n", "", nl2br($attribute_value));


i.e. the nl2br function is applied to the values, in the plugin code.

So to fix this for the tinyMCE textareas, you can add this into your functions.php file:

add_filter('wpp_stat_filter_lease_terms','my_nobr_filter');
add_filter('wpp_stat_filter_pet_policy','my_nobr_filter');
add_filter('wpp_stat_filter_pet_school','my_nobr_filter');

function my_nobr_filter($s){
return str_replace("<br />","",$s);
}



Dbranes comments:

i.e. the filters have the form:

add_filter('wpp_stat_filter_{meta_key}','my_nobr_filter');

where <strong>{meta_key}</strong> is in the set <strong>(lease_terms, pet_policy, school, tagline)</strong>


Peter Brain comments:

Everything is working now. Thanx a lot!

2013-01-06

Naveen Chand answers:

In your functions.php, add this code:

add_action( 'edit_page_form', 'mytextarea_for_page' );
function mytextarea_for_page() {
wp_editor( ' ', 'the_xyz' );
}


Replace <strong>the_xyz</strong> with the name of the textarea. The above code is for Add New PAGE. The below code is for the Add New POST:

add_action( 'edit_form_advanced', 'mytextarea_for_post' );
function mytextarea_for_post() {
wp_editor( ' ', 'the_xyz' );
}


In both cases, replace <strong>the_xyz</strong> with the name attribute of the textarea. For example, I may use <strong>excerpt</strong> there to get a nice editor for Excerpts textarea field.

Hope this helps.


Peter Brain comments:

No luck. The textarea field is not a part of a form. It is in a table on a custom post page.


Naveen Chand comments:

Do you have the meta_key value of the custom field. If not try and get it in the plugin or from your database and Add this code to your functions.php
add_action( 'add_meta_boxes', 'my_custom_box' );
add_action( 'save_post', 'saving_my_data' );

function my_custom_box() {
add_meta_box( 'my_box', 'My Custom Field Editor Box', 'my_wp_editor' );
}

function my_wp_editor( $post ) {
$field_value = get_post_meta( $post->ID, '_my_custom_field_meta_key', false );
wp_editor( $field_value[0], '_my_custom_field_meta_key' );
}

function saving_my_data( $post_id ) {
if ( isset ( $_POST['_my_custom_field_meta_key'] ) ) {
update_post_meta( $post_id, '_my_custom_field_meta_key', $_POST['_my_custom_field_meta_key'] );
}

}

All you have to do in this code is replace <strong>_my_custom_field_meta_key</strong> with your meta_key name. I am sure you will get the meta_key name of the field if you search it in your wp_postmeta table. Grab that meta_key and put that in place of <strong>_my_custom_field_meta_key</strong>.

Kindly let me know if it doesn't solve your issue.


Naveen Chand comments:

Note that there are four places where you have to replace <strong>_my_custom_field_meta_key</strong> with your own meta_key name. To test this code, simply run it as it is, and you'll find the data is stored in your database with my key: <strong>_my_custom_field_meta_key</strong>.


Peter Brain comments:

Finally I got it to work with:
add_action( 'edit_form_advanced', 'my_second_editor' );
function my_second_editor() {
$settings = array( 'textarea_rows' => '30', 'quicktags' => false);
wp_editor( '', '_my_custom_field_id', $settings );
}


But it collide with the main instance of tinyMCE. e.g. I use tinyMCE advance and the table edit Buttons don't work. Any idea how to seperate this second instance of the editor and get the visual/htnl buttons to work ?


Naveen Chand comments:


add_action( 'edit_form_advanced', 'my_second_editor' );

function my_second_editor() {

$settings = array( 'tinymce' => array('theme_advanced_buttons9' => 'bold, italic, ul, min_size, max_size'), 'textarea_rows' => '30', 'quicktags' => false);

wp_editor( '', '_my_custom_field_id', $settings );

}


Notice that I have inserted tinymce array into the $settings and created an instance called 'theme_advanced_buttons9'. You can add whatever controls you want here in this array. A list of possible buttons and controls are available [[LINK href="http://www.tinymce.com/wiki.php/Buttons/controls"]]here[[/LINK]].

2013-01-07

Arnav Joy answers:

which plugin are you using?


Peter Brain comments:

I use this Plugin: [[LINK href="http://wordpress.org/extend/plugins/wp-property/"]][[/LINK]]


Peter Brain comments:

http://wordpress.org/extend/plugins/wp-property/