WordPress.org

Forums

PHP to Filter URLs Within Content (3 posts)

  1. Kudama
    Member
    Posted 5 years ago #

    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?

  2. Kudama
    Member
    Posted 5 years ago #

    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!

  3. Kudama
    Member
    Posted 5 years ago #

    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;
    }

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.