I have the below code cobbled together. I've got it saved in a standalone PHP file so I can just run it on demand.
It should be pretty clear what it's meant to do: loop over all posts of type 'product' and 'product-variation', then use preg_replace to remove all div and span tags.
For some reason though, the below function doesn't seem to remove the tags:
<?php
require "wp-config.php";
// Post Types to include
$args = array(
'posts_per_page' => '50',
'post_type' => array(
'product',
'product-variation'
)
);
// The Query
$query = new WP_Query( $args );
$posts = $query->posts;
foreach($posts as $post) {
$tags = array( 'div', 'span');
$content = $post->post_content;
foreach ($tags as $tag) {
$cleanedcontent = preg_replace('#<\s*' . $tag . '[^>]*>.*?<\s*/\s*'. $tag . '>#msi', '', $content);
}
echo $cleanedcontent . '<hr/>';
// $cleaned_post = array(
// 'ID' => $post->ID,
// 'post_content' => $cleanedcontent
// );
// wp_update_post( $cleaned_post );
}
echo 'Done.'
?>
Eventually I'll be using wp_update_post to save the "cleaned" content - that section is currently commented out.
My code is partially based on this: strip only specific tags (like <p>), but keep other tags (like <br/>)
Any ideas why my function isn't stripping out the div tags?
I have the below code cobbled together. I've got it saved in a standalone PHP file so I can just run it on demand.
It should be pretty clear what it's meant to do: loop over all posts of type 'product' and 'product-variation', then use preg_replace to remove all div and span tags.
For some reason though, the below function doesn't seem to remove the tags:
<?php
require "wp-config.php";
// Post Types to include
$args = array(
'posts_per_page' => '50',
'post_type' => array(
'product',
'product-variation'
)
);
// The Query
$query = new WP_Query( $args );
$posts = $query->posts;
foreach($posts as $post) {
$tags = array( 'div', 'span');
$content = $post->post_content;
foreach ($tags as $tag) {
$cleanedcontent = preg_replace('#<\s*' . $tag . '[^>]*>.*?<\s*/\s*'. $tag . '>#msi', '', $content);
}
echo $cleanedcontent . '<hr/>';
// $cleaned_post = array(
// 'ID' => $post->ID,
// 'post_content' => $cleanedcontent
// );
// wp_update_post( $cleaned_post );
}
echo 'Done.'
?>
Eventually I'll be using wp_update_post to save the "cleaned" content - that section is currently commented out.
My code is partially based on this: strip only specific tags (like <p>), but keep other tags (like <br/>)
Any ideas why my function isn't stripping out the div tags?
Share Improve this question asked Apr 30, 2019 at 7:31 warm__tapewarm__tape 611 silver badge11 bronze badges 5 |3 Answers
Reset to default 0Im not sure what you are actually doing here but I think you are looking to create a block of code from the post content then strip unnecessary tags.
I would use strip tags: https://www.w3schools/php/func_string_strip_tags.asp
<?php
require "wp-config.php";
// Post Types to include
$args = array(
'posts_per_page' => '50',
'post_type' => array(
'product',
'product-variation'
)
);
// The Query
$query = new WP_Query( $args );
$posts = $query->posts;
foreach($posts as $post) {
$content = $post->post_content;
$content = echo strip_tags($content, "<div><p><h1>"); // where you pass the code block, then state the tags that you wish to keep. I also cannot recall if it will need to be echo'd or not.
}
echo 'Done.'; ?>
Where you pass the code block, then state the tags that you wish to keep. I also cannot recall if it will need to be echo'd or not.
Apologies if I have misunderstood.
Thanks, Jason.
You could pass the content through wp_kses()
as well. Here is the codex entry for it - https://codex.wordpress/Function_Reference/wp_kses
Replace
$content = $post->post_content;
foreach ($tags as $tag) {
$cleanedcontent = preg_replace('#<\s*' . $tag . '[^>]*>.*?<\s*/\s*'. $tag . '>#msi', '', $content);
}
with
$content = $cleanedcontent = $post->post_content;
foreach ($tags as $tag) {
$cleanedcontent = preg_replace('#<\s*' . $tag . '[^>]*>.*?<\s*/\s*'. $tag . '>#msi', '', $cleanedcontent);
}
echo $cleanedcontent
still contains div and span tags- that's the problem. Not where/how i'm saving$cleanedcontent
back to the post. – warm__tape Commented Apr 30, 2019 at 13:16