WordPress.org

Support

Support » How-To and Troubleshooting » Search entire site for URL and replace with a different URL?

Search entire site for URL and replace with a different URL?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Something like this plugin?

    Luke Etheridge
    Member

    @luke-etheridge

    Thanks for the response.

    No nothing that’s a one-time search and replace.

    I mean some kind of filter that can be inserted to the functions.php file that will constantly change one URL for another wherever it may be and whenever it’s inserted (now and in the future).

    In layman’s terms what I want is, if I were to create a post/widget/excerpt/header button tomorrow (or in 10 days, or a months time) that includes

    http://www.mysite.com/login

    in it, then it will automatically be swapped for

    http://www.mysite.com/login?redirect_to=<?php echo "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; ?>

    I’m pretty sure there is a way to do this with ‘preg_replace’ or something but PHP isn’t my expertise.

    function mySearchAndReplace($content) {
        $str='http://www.example.com/login';
        $replacement='http://www.example.com/login?redirect_to=http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    
        $content = preg_replace(
                '@' . preg_quote($str,'@') . '@',
                $replacement,
                $content
            );
        return $content;
    }

    To test the function you can do:

    $content='My post content with the http://www.example.com/login string included';
    echo mySearchAndReplace($content);

    You’ll need to add filter that will use the function.
    If you want to apply filter on a post content when it’s saved, use content_save_pre filter.
    If you prefer not to manipulate any DB entries but just want to replace the sring before it’s printed to the screen, the_content filter may come in handy.

    More about WordPress filters here

    in your functions.php file add
    add_filter('content_save_pre', 'mySearchAndReplace');

    Luke Etheridge
    Member

    @luke-etheridge

    Hey x500.net,

    Thanks for this solution but is this not only for when the URL is within the post/page content? I want it to replace the URL no matter where it is on the site – in the header, a widget, in the footer, an excerpt etc…

    You’re correct about not wanting to manipulate any DB entries. I’d rather replace the string before it’s printed to the screen.

    Thanks.
    Luke

    Yes, you need to apply filter to every section you want to replace string within.
    Alternatively, you can preg_replace the entire html sent to the browser, but it’s not recommended if you care about performance at all.

    Luke Etheridge
    Member

    @luke-etheridge

    I placed

    function mySearchAndReplace($content) {
        $str='http://www.example.com/login';
        $replacement='http://www.example.com/login?redirect_to=http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    
        $content = preg_replace(
                '@' . preg_quote($str,'@') . '@',
                $replacement,
                $content
            );
        return $content;
    }

    Into my functions.php file then created a link using ‘http://www.example.com/login’ within a post and it didn’t replace it at all. Is that what the above code was supposed to be doing?

    Also how would I apply this to all sections? The header for example?

    Thanks,
    Luke

    You need to bind the function to a particular filter that way:

    add_filter('content_save_pre', 'mySearchAndReplace');

    Place it just below the function.

    You need to go through the WordPress filter list to find one responsible for filtering section you’re after.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Search entire site for URL and replace with a different URL?’ is closed to new replies.