Reputation: 1
I have textarea for post meta:
<textarea id ="product_description" style="width: 100%" name="product_description"><?php echo get_post_meta($post->ID, 'product_description', true); ?></textarea>
if ( isset( $_POST[ 'product_description' ] ) ) {
update_post_meta( $post_id, 'product_description', sanitize_text_field($_POST['product_description']) );
}
i want to change textarea with wp_editor(). Please help me.. I have tried to find various reviews but failed...
Upvotes: -1
Views: 219
Reputation: 300
To replace the textarea with a WordPress editor using wp_editor(), you need to replace the textarea tag with a function call that outputs the WordPress editor.
Here is an example of how to do it:
Replace the existing textarea with the following code:
<?php
$content = get_post_meta( $post->ID, 'product_description', true );
$editor_id = 'product_description_editor';
$settings = array(
'textarea_name' => 'product_description',
'media_buttons' => false,
'textarea_rows' => 10,
'teeny' => true,
'quicktags' => false
);
wp_editor( $content, $editor_id, $settings );
?>
This will output the WordPress editor instead of the textarea. Note that the wp_editor function takes several arguments, including the content to be displayed in the editor, the editor ID, and various settings.
Make sure to replace the existing if ( isset( $_POST[ 'product_description' ] ) ) { ... } code with the following code:
<?php
if ( isset( $_POST[ 'product_description' ] ) ) {
update_post_meta( $post_id, 'product_description', wp_kses_post( $_POST[ 'product_description' ] ) );
}
?>
The wp_kses_post function is used to sanitize the editor content before it is saved to the database. This function allows basic HTML tags and attributes while removing potentially dangerous script tags and attributes.
I hope this helps!
Upvotes: 0