0

In my php files, I would like to remove all my hardcoded URLs (ex: http://mysite.com/) and replace them with the get_site_url() function.

My problem is with the php code of one of my functions (in functions.php) that adds a _blank attribute to all the links of my website except for the internal URLs.

function autoblank($text) {
    $return = str_replace('href=', 'target="_blank" href=', $text);
    $return = str_replace(
        'target="_blank" href="http://localhost.com/test/', 
        'href="http://localhost.com/test/', 
        $return
    );
    $return = str_replace('target="_blank" href="#', 'href="#', $return);
    return $return;
}
add_filter('the_content', 'autoblank');
add_filter('comment_text', 'autoblank');

How can I replace http://localhost.com/test/ using get_site_url() so this code would work for any website? (not just http://localhost.com/test/ )

0

You actually already were pretty close:

function autoblank($text) {
    $return = str_replace('href=', 'target="_blank" href=', $text);
    $return = str_replace(
        'target="_blank" href="' . get_site_url(), 
        'href="' . get_site_url(), 
        $return
    );
    $return = str_replace('target="_blank" href="#', 'href="#', $return);
    return $return;
}
add_filter('the_content', 'autoblank');
add_filter('comment_text', 'autoblank');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.