• Resolved e7traf-host

    (@e7traf-host)


    i have a big wordpress installation more than 200K post

    all i need to do is remove all <img> tags from posts content.

    completly removing from my posts not only striping from show.

    any ideas!

Viewing 15 replies - 1 through 15 (of 20 total)
  • Can you set the img tag in the CSS to display:none?

    Find which div the image is in that you want removing; then target that div’s classes through the css, until you hit the image you don’t want to display.

    It’s difficult without seeing code; but I think it can be done, I did something similar on my category pages.

    Moderator bcworkz

    (@bcworkz)

    jajah22, that’s a good idea for hiding output, but I believe the OP needs the img tags actually removed from the database.

    e7traf-host, I believe this could be done with mySQL alone. I’m terrible at mySQL, but I’m guessing something like this:
    UPDATE wp_posts WHERE post_type = 'post' SET post_content = REPLACE(post_content, REGEXP(\<img.*\>), '');

    Confirm the proper syntax, and test thoroughly on a limited basis before tuning loose on the entire DB, after it has been backed up.

    If all else fails you can brute force it in PHP by querying all posts, looping through all the posts and doing a preg_replace() on post_content, then updating each post in turn.

    Thread Starter e7traf-host

    (@e7traf-host)

    @jahjah22
    iam already doing this but i neeed to completly remove it from the databse because i have more than 200K post if i delete all img tags from my database i will save a lot of database size so my site loading speed will increase

    Thread Starter e7traf-host

    (@e7traf-host)

    your sql query is very very good but your REGEX have one problem it is matching from the begining of <img to the end of the line so if i have any other data like html or text in the same line it will also replaced.

    please test it in:
    https://gskinner.com/RegExr/

    Thread Starter e7traf-host

    (@e7traf-host)

    @bcworkz

    now iam going with your advice “looping all posts” but i have 2 problems to do this please see this

    https://wordpress.stackexchange.com/questions/116376/delete-replace-img-tags-in-post-content-for-auto-published-posts-after-10-hours

    Thread Starter e7traf-host

    (@e7traf-host)

    @bcworkz

    could you please update your REGEX to achieve the goal for me?

    Thread Starter e7traf-host

    (@e7traf-host)

    i have edited your REGEX to my needs but mysql doesn’t support REGEX with replace statement

    so i will go with the second solution

    Moderator bcworkz

    (@bcworkz)

    Sorry about the bad query and RegExp, I did warn you this was not my thing ??

    The first snippet in your link is pretty much what I had in mind, though it only needs to be run once to clean up your DB. Instead of hooking an action, the code could reside on it’s own page that includes wp-load.php in order to load the WP environment. Then just request the page to execute the script.

    Do some careful testing on a limited scope before turning it loose on the entire DB.

    To ensure this does not need to be done again, you need to prevent users from inserting img tags in the first place. Filtering through submitted content as the second linked snippet does would work, but WP already has a tag filtering facility which would be more efficient. It is used in part to enforce the ‘unfiltered_html’ capability.

    Hook ‘wp_kses_allowed_html’. Your callback is passed the global $allowedposttags if the second passed parameter is 'post'. So if the current user should not post images and the second parameter is 'post', unset $allowedposttags['img'];

    Thread Starter e7traf-host

    (@e7traf-host)

    so does you mean that if i edited wp-includes/kses.php & removed img from $allowedposttags this will prevent any future img ?

    iam thinking about another thing what your opinion in this?

    <?php
    function remove_images_form_past_posts() {
    if ( get_transient('images_removed_from_past') ) return;
    $today = getdate();
    $query = new WP_Query( 'year=2013 & monthnum=9 & day=20 & post_type=post & author=-1 & post_status=publish' );
    
      $query = get_posts('nopaging=1');
      if ( $query ) {
        foreach ( $query as $post ) {
          $newcontent = preg_replace('/<img[^>]+\>/i', '', $post->post_content);
          $newpost = array( 'ID' => $post->ID, 'post_content' => $newcontent);
          wp_update_post($newpost);
        }
        set_transient('images_removed_from_past', 1);
      }
    }
    add_action('admin_init','remove_images_form_past_posts');
    ?>
    Moderator bcworkz

    (@bcworkz)

    No, do not edit kses.php (or any core file). Things get messy quickly when you do that. The reason for providing filter and action hooks is so no one is tempted to do so. You can remove the img tag from the allowed post tags array by hooking into ‘wp_kses_allowed_html’. Specifics were mentioned in the last paragraph of my previous post. If there’s something you don’t understand, tell me what it is, I’ll try to clarify.

    Your code appears to do what you intend, but I question if it’s necessary to really go through recent posts and strip out img tags if the tags are disallowed in the first place. You certainly need to do this for all past posts, but once that’s done and img tags are disallowed, is it really necessary to process recent posts?

    Thread Starter e7traf-host

    (@e7traf-host)

    @bcworkz
    i will go with my code to update the past posts then i wil go with a new way to prevent this

    but please help me to fix my code

    The code above is perfect & do the job but have one problem: this code is unfortunately update 1 post only although the $args is matching 9 posts.

    I have tested the get_posts alone & it is working perfectly & executing 9 posts. I have stopped the WP_super_cache plugin & also no way. I have tried to change “set_transient $value from 1 to 9 with no way.

    Any ideas!

    Thread Starter e7traf-host

    (@e7traf-host)

    y latest code is

    function remove_images_form_past_posts() {
    if ( get_transient('images_removed_from_past') ) return;
    $args = array( 'post_type' => 'post', 'year' => 2013, 'monthnum' => 9, 'day' => 27, 'post_status' =>'publish', 'category' => 30, 'post_author' => 3, 'nopaging' => 1 );
    $posts = get_posts( $args );
    if ( $posts ) {
    foreach ( $posts as $post ){
      setup_postdata( $post );
      $newcontent = preg_replace('/<img[^>]+\>/i', '', $post->post_content);
      $newpost = array( 'ID' => $post->ID, 'post_content' => $newcontent);
      wp_update_post($newpost);
    }
      wp_reset_postdata();
      set_transient('images_removed_from_past', 1);
    }
    }
    add_action('admin_init','remove_images_form_past_posts');
    Thread Starter e7traf-host

    (@e7traf-host)

    the solution iiiiiiiiiiiiiiiiiiiis for every one searching for it:

    function remove_images_form_past_posts2() {
            if ( get_transient('images_removed_from_past2') )
                return;
            $args = array(
                        'post_type' => 'post',
                        'year' => 2013,
                        'monthnum' => 9,
                        'day' =>25,
                        'post_status' =>'publish',
                        'category' => 30,
                        'post_author' => 3,
                        'nopaging' => 1
                    );
            $posts = get_posts( $args );
                if ( $posts ) {
                    foreach ( $posts as $post ) {
                        $newcontent = preg_replace('/<img[^>]+\>/i', '', $post->post_content);
                        $newpost = array(
                                       'ID' => $post->ID,
                                       'post_content' => $newcontent
                                   );
                        wp_update_post($newpost);
                    }
                    set_transient('images_removed_from_past2',true, 60 * 60 * 24);
                }
            }
    add_action('admin_init','remove_images_form_past_posts2');

    change the $args to your needs, change the REGEX to your needs, if you will use this more than 1 time delete the old one & create anew one with new names for the function & transient

    wish this help

    Moderator bcworkz

    (@bcworkz)

    I looked your code over fairly closely and could not see any issues. So I setup a quick test on my own installation and your code did exactly what you would expect in removing img tags from all posts returned by get_posts().

    I’ve no idea why it’s not working for you, it’s very strange.

    Thread Starter e7traf-host

    (@e7traf-host)

    no my friend the last post for me the code is worked perfectly, i posting it to every body who searching for this problem

    thanks for your help

Viewing 15 replies - 1 through 15 (of 20 total)
  • The topic ‘remove all img tags from post_content’ is closed to new replies.