1

I want "a link" in my content "automaticly" replace with "new link"

Heres the explanation, in my all post content I want a link of www.lol.com/?http:/rapidshare.com automaticly replace with new link http://rapidshare.com

Whats the code to add in my functions.php ?? or any plugin that automaticly replace that old link with new one?

1 Answer 1

1

This is the SQL:

UPDATE <YOUR_PREFIX_HERE>_posts SET post_content = replace(post_content, 'www.lol.com/?http://rapidshare.com', 'http://rapidshare.com');

Either inject it directly into the database (via PHPmyAdmin and the like) or use WordPress to do so.

Here's the WordPress way:

global $wpdb;
$wpdb->query(
    $wpdb->prepare(
        "UPDATE <YOUR_PREFIX_HERE>_posts
        SET post_content = replace(post_content, %s, %s)",
        'www.lol.com/?http://rapidshare.com',
        'http://rapidshare.com'
        )
);

// EDIT
For completeness (as @birgire suggested): if you don't know the table prefix, you can let WordPress look that up:

global $wpdb;
$wpdb->query(
    $wpdb->prepare(
        "UPDATE {$wpdb->posts}
        SET post_content = replace(post_content, %s, %s)",
        'www.lol.com/?http://rapidshare.com',
        'http://rapidshare.com'
        )
);
9
  • Do I have to fill up UPDATE <YOUR_PREFIX_HERE>? and what is it?
    – user31445
    Commented Apr 13, 2013 at 12:02
  • I put the sql query but I've got this error #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '<YOUR_PREFIX_HERE>_posts SET post_content = replace(post_content, 'www.lol.com/?' at line 1
    – user31445
    Commented Apr 13, 2013 at 12:04
  • You just have to set <YOUR_PREFIX_HERE> to your WP table prefix. Normally, this is wp, however, you may have changed that to something else. You can see this either in your database (all WP tables have the same prefix), or in your wp-config.php file.
    – tfrommen
    Commented Apr 13, 2013 at 12:05
  • 1
    You can use "UPDATE {$wpdb->posts} SET post_content = ..." or "UPDATE {$wpdb->prefix}posts SET post_content = ..." .
    – birgire
    Commented Apr 13, 2013 at 12:24
  • @birgire Of course, that's right. I'll add that to my answer. I just thought, you'd normally know your table prefix (or if not, you most likely did not change it, so it's wp_), and thus wp_posts is slightly faster than {$wpdb->posts}. ;)
    – tfrommen
    Commented Apr 13, 2013 at 12:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.