• 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

    https://www.mysite.com/login

    2) Replace with

    https://www.mysite.com/login?redirect_to=<?php echo "https://" . $_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!

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

    Thread Starter Luke Etheridge

    (@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

    https://www.mysite.com/login

    in it, then it will automatically be swapped for

    https://www.mysite.com/login?redirect_to=<?php echo "https://" . $_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='https://www.example.com/login';
        $replacement='https://www.example.com/login?redirect_to=https://' . $_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 https://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');

    Thread Starter Luke Etheridge

    (@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.

    Thread Starter Luke Etheridge

    (@luke-etheridge)

    I placed

    function mySearchAndReplace($content) {
        $str='https://www.example.com/login';
        $replacement='https://www.example.com/login?redirect_to=https://' . $_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 ‘https://www.example.com/login&#8217; 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.