WordPress Post problem
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

Post content:

<img class="class" title="title" src="http://www.site.com/wp-content/uploads/2012/11/56878.jpg" alt="" width="448" height="1998" />

or

<a href="http://www.site.com/wp-content/uploads/2012/11/56878.jpg"><img class="class" title="title" src="http://www.site.com/wp-content/uploads/2012/11/56878.jpg" alt="" width="448" height="1998" /></a>

Problem: i have more than 5000 post. I want to delete link from all post (if have).
How can i do it?

Are you just looking for an idea on how to do it (pseudo code) or are you looking for a working code that will strip links from your posts?
janhancic almost 7 years ago
Need working code. PHP+MySQL or someting else.
hatasu almost 7 years ago
awarded to Wikimedia
Tags
#wordpress

Crowdsource coding tasks.

2 Solutions


Since all the posts are stored in MySQL, you could iterate over all the posts that you have and then use a simple find/replace to change each post that has your link in it and save it back to the database.

I need working code, sorry.
hatasu almost 7 years ago

If you want to delete before dump it to the page, you should use these ways

For example,

<?php

$s = '<a href="http://www.site.com/wp-content/uploads/2012/11/56878.jpg">
    <img class="class" title="title" src="http://www.site.com/wp-content/uploads/2012/11/56878.jpg" alt="" width="448" height="1998" />
     </a>';

echo preg_replace("/<\\/?a(\\s+.*?>|>)/", "", $s);

?>

If you want to replace all of the post in the database, you will just write an update code. If you don't know how to do it, tell us your posts table information.

UPDATE:

Here is an example how you can update your post rows,

  <?php
     $query = mysql_query("select * from postTable");
     while($postRow = mysql_fetch_array($query)) {
       $postID = $postRow["postID"];
       $post = $postRow["postContent"];
       $post = preg_replace("/<\\/?a(\\s+.*?>|>)/", "", $post);
       mysql_query("update postTable set postContent = '".mysql_real_escape_string($post)."' where postID = ".$postID);
     }
  ?>

* Don't forget to get backup before trying this.

I try it, and if work, you got money.
hatasu almost 7 years ago
preg_replace pattern doesn't work
hatasu almost 7 years ago
View Timeline