Support Forum
Support › MB Custom Post Type › How to Pre Filter Metabox WYSIWYG post_content
Hi there,
I have a custom post type of 'Testimonials'. For this custom post type I have a MB WYSIWYG field like this:
$meta_boxes[] = [
'title' => __( 'Testimonial content', 'text_domain' ),
'post_types' => 'Testimonials',
'id' => 'testimonial_details',
'context' => 'normal',
'fields' => [
[
'id' => 'post_content', //I.E the_content()
'type' => 'wysiwyg',
'desc' => __( 'Enter your testimonial content.', 'text_domain' ),
'options' => [
'tinymce' => [
'toolbar1' => 'formatselect,bold,italic,bullist,numlist,link,|,undo redo',
'toolbar2' => '',
'block_formats' => 'Paragraph=p;',
],
'drag_drop_upload' => false,
'quicktags' => true,
'media_buttons' => false,
'textarea_rows' => 10,
],
'required' => true,
],
],
];
All works exactly as expected.
The Problem
============
I use a 'content_save_pre' filter function on 'the_content()' for all my post types (Page, Post and CPT) to strip any unwanted tags in the TinyMce Editors like this before it hits the database:
function stripUnwanted($the_content ){
// Unacceptable tags
$strip_tags = "div|h1|h3|h4|h5";
// Filter the_content() on selected post types
if ( get_post_type( get_the_ID() ) == 'post' ||
get_post_type( get_the_ID() ) == 'page' ||
get_post_type( get_the_ID() ) == 'testimonials' ) {
$the_content = preg_replace("#<\s*\/?(".$strip_tags.")\s*[^>]*?>#im", '', $the_content);
}
return $the_content;
}
add_filter('content_save_pre','stripUnwanted',99);
Q) This filter successfully strips my unwanted tags from the built in WordPress TinyMce WYSIWYG wp-editors for 'Post' and 'Page'. However it NEVER catches (filters) the 'testimonials' CPT WYSIWYG generated by Metabox? What am I missing?
Any help / advice appreciated?
Many Thanks 🙂
Hello,
I think the filter hook is running before the Meta Box hook so it does not work with the CPT. You can use the Sanitization feature to strip HTML tags in the field value.
https://docs.metabox.io/sanitization/