最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp query - Remove divs and spans from post content

programmeradmin0浏览0评论

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
  • how do you access this function? – Vishwa Commented Apr 30, 2019 at 8:56
  • It sits in a file so I can run it manually. $content = $post->post_content; gets the content correctly, the issue is that the preg_replace doesn't clean it. – warm__tape Commented Apr 30, 2019 at 9:41
  • Yes, you have to do something with $cleanedcontent, because now it only get's overwritten in the $posts loop. – moped Commented Apr 30, 2019 at 9:49
  • Yes, eventually I'll save it back to $post->post_content. – warm__tape Commented Apr 30, 2019 at 13:13
  • At the moment, 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
Add a comment  | 

3 Answers 3

Reset to default 0

Im 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);
}
发布评论

评论列表(0)

  1. 暂无评论