WordPress.org

Ready to get started?Download WordPress

Forums

Search entire site for URL and replace with a different URL? (8 posts)

  1. Luke Etheridge
    Member
    Posted 11 months ago #

    Hi there,

    Is there something that I can place in my functions.php file that will search my ENTIRE site for all occurrences of a URL, and then replace it with an alternative URL (i.e. the original URL but with a redirect_to extension like below).

    In simpler terms...

    1) Search for all occurrences of

    http://www.mysite.com/login

    2) Replace with

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

    Is that possible? I basically don't want to manually code this into my template files and the above solution would be perfect.

    Thanks in advance!

  2. x500.net
    Member
    Posted 11 months ago #

    Something like this plugin?

  3. Luke Etheridge
    Member
    Posted 11 months ago #

    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.

  4. x500.net
    Member
    Posted 11 months ago #

    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');

  5. Luke Etheridge
    Member
    Posted 11 months ago #

    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

  6. x500.net
    Member
    Posted 11 months ago #

    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.

  7. Luke Etheridge
    Member
    Posted 11 months ago #

    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

  8. x500.net
    Member
    Posted 11 months ago #

    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.

Reply

You must log in to post.

About this Topic