How can I implement the target=”_blank” attribute into a PHP link so links open up in a new tab?

I have a link that is basically an ‘about’ page that has social icons pointing to my personal social media outlets. In PHP, the code is like this:

function wp_about_author_get_socials() {
$socials = array();
$socials['twitter'] = array('title'=>'Twitter', 'link'=>'http://www.twitter.com
/%%username%%', 'icon'=> WPAUTHORURL_URL .'/images/twitter.png');
$socials['facebook'] = array('title'=>'Facebook', 'link'=>'http://www.facebook.com
/%%username%%', 'icon'=> WPAUTHORURL_URL .'/images/facebook.png');

I’ve fiddled around with things, but just can’t seem to implement things so the link opens up in a new tab/page. Currently, the links take the user off the website (not ideal). Any guidance in this regard would be greatly appreciated!

Read More

What I have tried was changing the links to this:

$socials['twitter'] = array('title'=>'Twitter', 'link'=>'http://www.twitter.com
/%%username%%', 'target'=>'_blank', 'icon'=> WPAUTHORURL_URL .'/images/twitter.png');

But that didn’t work.

This may be the part that prints the links, from what I can tell:

// About Author Social
$wp_about_author_social .= wp_about_author_get_social_links($wp_about_author_settings);
if(isset($wp_about_author_settings['wp_author_social_images']) &&     
$wp_about_author_settings['wp_author_social_images']){
$wp_about_author_content .= "<p>"  .$wp_about_author_links . "</p>";
if($wp_about_author_social != ""){
$wp_about_author_content .= '<p class="wpa-nomargin">'.apply_filters( 
'wp_about_author_follow_me', "Follow Me:").'<br />' . $wp_about_author_social.'</p>';
}
} else {
$wp_about_author_content .= "<p class='wpa-nomargin'>";
$wp_about_author_content .= $wp_about_author_links;
if($wp_about_author_social != ""){
$wp_about_author_content .= apply_filters( 'wp_about_author_separator', " - ") . 
$wp_about_author_social;
}
$wp_about_author_content .= "</p>";
}

// Generate social icons
function wp_about_author_get_social_links($wp_about_author_settings){
$content="";
$socials = wp_about_author_get_socials();
foreach($socials as $social_key=>$social){
if (get_the_author_meta($social_key)){
if(isset($wp_about_author_settings['wp_author_social_images']) &&     
$wp_about_author_settings['wp_author_social_images']){
$content .= "<a class='wpa-social-icons' href='".str_replace('%%username%%', 
get_the_author_meta($social_key), $social['link'])."'><img src='". $social['icon']."' 
alt='".$social['title']."'/></a>";
} else {
if($content != "")
$content .= apply_filters( 'wp_about_author_separator', " - ");
$content .= "<a href='".str_replace('%%username%%', get_the_author_meta($social_key), 
$social['link'])."'>".$social['title']."</a>";
}
}
}
return $content;
}

Related posts

Leave a Reply

1 comment

  1. Within the code you posted, you can add target="_blank" to the links on lines 28 and 34.

    Line 28 becomes:

    $content .= "<a class='wpa-social-icons' target='_blank' href='".str_replace('%%username%%',  
    

    Line 34 becomes:

    $content .= "<a target='_blank' href='".str_replace('%%username%%', get_the_author_meta($social_key),