• Without manually changing the content and without relying on .htaccess redirects (or the Redirection plugin), I’m trying to write a script that finds and alters specific URLs within the content of pages or posts.

    The hurdle I’m facing is that I want this to work with shortcodes and the like — for example I’d like it to work with Dagon Design’s sitemap generator.

    So it needs to filter URLs generated by those shortcodes before the content is rendered to the browser. Odd problem to create for myself and try to solve, I know.

    Any ideas?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter Kudama

    (@kudama)

    So I tried adding this to functions.php:

    add_filter('the_content', 'rewriteURL');
    
    function rewriteURL($URL) {
    $input = str_replace('http://domain.com/my-permalink', 'http://domain.com/', $URL);
    return $URL;
    }

    This seems like it should do what i want, but I really don’t know for sure.

    When I load the page with the sitemap, it just stops loading at the_content (so my sidebar and footer don’t render). This only happens on the page that has the URL I want to change, so it seems to be doing something.

    Any feedback or ideas? Thanks!

    Thread Starter Kudama

    (@kudama)

    Got it!

    I was just accidentally leaving in a variable from a sample I saw for a similar solution. So this works perfectly:

    add_filter('the_content', 'rewriteURL');
    
    function rewriteURL($URL) {
    $URL = str_replace('http://domain.com/my-permalink', 'http://domain.com/', $URL);
    return $URL;
    }
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘PHP to Filter URLs Within Content’ is closed to new replies.