Open embedded links in comments in new window

wordpress148x140Last week I need to find out a solution for a my friend. He want to open all links embedded in comments in a new windows.

I searched on google for more time unti I found the solution, to use an hook.

Because I think other people will need a solution for this problem or maybe for a similar problem, I will describe it the solution, step by step. Are you ready? Go…

Step 1 Find the file named functions.php in your theme folder (every theme has this file) and open it for edit

Step 2 At the end of the file you can create the function to open the links in new windows, like this:

function comment_text_target_blank( $comment ) {
    $pos =  strrpos($comment,”_blank”);
    if ($pos===FALSE) {
        $theComment = str_replace(“<a”,”<a target=’_blank’ “, $comment);
    } else {
        $theComment = $comment;
    }

    return $theComment;
}

Step 3 create the hook to apply new function

add_filter(‘comment_text’, ‘comment_text_target_blank’, 1000);

This solution works for me from version 3.4.2 and newer of wordpress. I hope I helped you.

Advertisements
This entry was posted in wordpress and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s