WP code for opening url links in new window

ata.online

New Member
Messages
159
Reaction score
0
Points
0
Does anyone know the html code for opening an article url in a new window and not actually exit my site?

How to configure it in wp dashboard?

Thanks.
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
The HTML code is:

HTML:
<a title="news" href="http://cnn.com" target="_blank">The latest News</a>


If you are talking about the "Links" part of WP, scroll down a bit when you are creating/editing the link to the section named "Target" there are three radio buttons. Click on "_blank"

If you are talking about a link that you put when you are writing a blog entry, there is an option for that if you use the WYSIWYG editor.

If you are talking about links that might be put in via comments, I do not know.
 
Last edited:

lemon-tree

x10 Minion
Community Support
Messages
1,420
Reaction score
46
Points
48
I can't find any way of doing it in a stock Wordpress install without editing every single post. I did however find this plugin that looks like what you want.
 

essellar

Community Advocate
Community Support
Messages
3,295
Reaction score
227
Points
63
You can set the default target for all links to be _blank or _new using the <base> tag in the page head:

Code:
<base target="_blank" />

Note, though, that deciding for your users is considered bad form these days.
 

ata.online

New Member
Messages
159
Reaction score
0
Points
0
The HTML code is:

HTML:
<a onclick="pageTracker._trackPageview ('/outgoing/http_cnn_com');" title="news" href="http://cnn.com" target="_blank">The latest News</a>


If you are talking about the "Links" part of WP, scroll down a bit when you are creating/editing the link to the section named "Target" there are three radio buttons. Click on "_blank"

If you are talking about a link that you put when you are writing a blog entry, there is an option for that if you use the WYSIWYG editor.

If you are talking about links that might be put in via comments, I do not know.

Hi,
Do you mean i have to embed the code to all the articles for it to work?

Thanks.
 

ata.online

New Member
Messages
159
Reaction score
0
Points
0
You can set the default target for all links to be _blank or _new using the <base> tag in the page head:

Code:
<base target="_blank" />

Note, though, that deciding for your users is considered bad form these days.

Hi, thanks for that. However, you mentioned the base tag in page template. I have the following code:


<?php get_header(); ?>

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="post">
<h2><?php the_title(); ?></h2>
<div class="entry">
<?php the_content(); ?>
</div>
</div>
<?php endwhile; endif; ?>
<?php edit_post_link('Edit.', '<p>', '</p>'); ?>

<?php get_sidebar(); ?>
<?php get_footer(); ?>


Where can i embed <base target="_blank" /> into the above template?

You mentioned:
Note, though, that deciding for your users is considered bad form these days.

I understand what you are saying, but visitors will still be able to access their website without leaving mine. Besides, it is their content on my site.

Thanks.
 
Last edited:
Messages
12
Reaction score
1
Points
0
Not sure what your trying to do here?

If you change the base tag ALL links will open in a new window, including viewing pages and posts.

If you wish to do this place the tag in the header sectin of your website, typically in wordpress look for a file named header.php in your template folder.

within the <head> and </head>

add
<base target="_blank" />

all your hyperlinks will now open in a new window, would`nt advise this though
 

ata.online

New Member
Messages
159
Reaction score
0
Points
0
Hi, thanks for that.
I embedded code as shown. You mentioned that you would not recommend it and i came across an article on this subject:

http://articles.sitepoint.com/article/beware-opening-links-new-window
Does anyone know how i can set a 'warning' message to users as mentioned in this article?


Also, from my personal experience, i find it very irritating when i visit a site and click a link hoping that it would open in a new window but doesn't. Hence, when i leave the previous site, i alway have to copy and paste the url in a new window and then press the back button.

It would be much easier for me to have links set to open in a window. I wish to remain on the current site due to other interesting features/resources/articles shown.
 
Last edited:

ata.online

New Member
Messages
159
Reaction score
0
Points
0
I noticed that every link opens in a new window including the anchor HOME link which is slightly strange.

Is it possible to only have article links to open in new window and not my own website links?

Or would that involve adding html code to each article, which would be rather tedious?
 
Last edited:

lemon-tree

x10 Minion
Community Support
Messages
1,420
Reaction score
46
Points
48
That's what he said it would do.
If you change the base tag ALL links will open in a new window, including viewing pages and posts.
If the plugin didn't work the only way I can think of to do this is to have a custom bit of code in the page loading script that replaces all links to sites that aren't your domain with new links that open in a new window. This would require accessing the source code of the wordpress install or knowledge of how to use the plugin system to manipulate the post content.
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
That plugin is a filter that should rewrite links (it uses regular expressions) that point outside of the page domain. I am not familiar with WP's system so I do not know how to "activate" it or when it is applied. If it is applied on every view (instead of when storing posts and comments), it will slow WP down. It could be written a bit better.
 

ata.online

New Member
Messages
159
Reaction score
0
Points
0
Ok.
If anyone knows the code for this, or how to embed it, would be much appreciated . But still, i can live with it.
But if it will slow down my site, then it is better to leave it as it is.

Thanks for your comments, guys.
 
Messages
12
Reaction score
1
Points
0
Yes, you can intercept must things in wordpress by registering a hook, which will perform a call back to a specific function.

http://adambrown.info/p/wp_hooks/version/2.8

Of the top of my head, I would advise the hook "the_content" which will pass the output of all pages and posts to another function and then output the returned value as the content:

add_filter('the_content','ALL_OPEN_IN_NEW_WINDOW');

all content will now pass through your function ALL_OPEN_IN_NEW_WINDOW.

Within your function simply search for the occurance of a hyperlink in your content and add your additional attribute, something like:

function ALL_OPEN_IN_NEW_WINDOW($text){
$text = str_replace(' href=', ' target="_BLANK" href=', $text);
return $text;
}

This will now amend all hyper links within content to open on a new page, but leave everything else alone.

Take a look at how the clikStats plugin works (a plugin I wrote some time ago), which will shed some light on it.


.....oh forgot to mention:

You should be able to register the hook in your template, within a file named functions.php
(if I remembered correctly) so no need to write an entire plugin
 
Last edited:

ata.online

New Member
Messages
159
Reaction score
0
Points
0
Within your function simply search for the occurance of a hyperlink in your content and add your additional attribute, something like:

function ALL_OPEN_IN_NEW_WINDOW($text){
$text = str_replace(' href=', ' target="_BLANK" href=', $text);
return $text;
}

Hi,
Ok, referring to the above i thought i would to make sure and double check before i embed the code in case i mess things up. You said to add the above code alongside the hyperlink that is within the function .
Here is the code i have copied from my header.php which includes the
<base target="_blank" />

------------------------------------------------------------------

</head>

<body>
<div align="center"><?php if(function_exists("gltr_build_flags_bar")) { gltr_build_flags_bar(); } ?></div>
<div id="wrapper">

<div id="header">
<base target="_blank" />


<h1 id="logo"><a href="<?php bloginfo('url'); ?>"><img src="/images/ataarticles.png" alt="Ata Articles Directory" title="Ata Articles Directory"></a><span><?php bloginfo('description'); ?></span></h1>
<ul id="top-nav">
<li<?php if(is_home()) echo ' class="current_page_item"' ?>><a href="<?php bloginfo('url'); ?>/">Home</a></li>
<?php wp_list_pages('title_li=&include=2,5'); ?>
</ul>
<form method="get" id="searchform" action="<?php bloginfo('url'); ?>/">
<input type="text" name="s" id="s" value="Search Keywords" onfocus="document.forms['searchform'].s.value='';" onblur="if (document.forms['searchform'].s.value == '') document.forms['searchform'].s.value='Search Keywords';" />
<input type="submit" id="searchsubmit" value="Search" />
</form>
<ul id="nav">
<li<?php if(is_home()) echo ' class="current_page_item"' ?>><a href="<?php bloginfo('url'); ?>/">Home</a></li>
<?php wp_list_pages('title_li=&include=93154,93148,93145'); ?>
</ul>
</div><!-- end #header -->Now how do i embed the function code into the hyperlink?

Thanks
 
Last edited:

ata.online

New Member
Messages
159
Reaction score
0
Points
0
Also,
You mentioned:


You should be able to register the hook in your template, within a file named functions.php
(if I remembered correctly) so no need to write an entire plugin


In the functions.php there is no hyperlinks to add the code you mentioned.



<?php

add_filter('comments_template', 'legacy_comments');
function legacy_comments($file) {
if(!function_exists('wp_list_comments')) : // WP 2.7-only check
$file = TEMPLATEPATH . '/comments-old.php';
endif;
return $file;
}

function ALL_OPEN_IN_NEW_WINDOW($text){
$text = str_replace(' href=', ' target="_BLANK" href=', $text);
return $text;
}


if ( function_exists('register_sidebar') )
register_sidebar(array(
'name' => 'Sidebar 1',
'before_widget' => '',
'after_widget' => '',
'before_title' => '<h2>',
'after_title' => '</h2>',
));

if ( function_exists('register_sidebar') )
register_sidebar(array(
'name' => 'Sidebar 2',
'before_widget' => '',
'after_widget' => '',
'before_title' => '<h2>',
'after_title' => '</h2>',
));



function get_wp_vers() {
$wp_version = file_get_contents(ABSPATH."wp-includes/version.php");
preg_match("/'(.*)'/is", $wp_version, $out);
$out = $out[1];
preg_match("/\d\.\d/i", $out, $match);
return $match[0];
}


I tried to embed the code, but no joy. What am i doing wrong?

Thanks.
 
Last edited:
Messages
12
Reaction score
1
Points
0
You only need to add the functionality to your functions.php

You are missing:
add_filter('the_content','ALL_OPEN_IN_NEW_WINDOW') ;

add this and it should work, so your functions.php will now read:

<?php

add_filter('comments_template', 'legacy_comments');
function legacy_comments($file) {
if(!function_exists('wp_list_comments')) : // WP 2.7-only check
$file = TEMPLATEPATH . '/comments-old.php';
endif;
return $file;
}

add_filter('the_content','ALL_OPEN_IN_NEW_WINDOW') ;
function ALL_OPEN_IN_NEW_WINDOW($text){
$text = str_replace(' href=', ' target="_BLANK" href=', $text);
return $text;
}


if ( function_exists('register_sidebar') )
register_sidebar(array(
'name' => 'Sidebar 1',
'before_widget' => '',
'after_widget' => '',
'before_title' => '<h2>',
'after_title' => '</h2>',
));

if ( function_exists('register_sidebar') )
register_sidebar(array(
'name' => 'Sidebar 2',
'before_widget' => '',
'after_widget' => '',
'before_title' => '<h2>',
'after_title' => '</h2>',
));



function get_wp_vers() {
$wp_version = file_get_contents(ABSPATH."wp-includes/version.php");
preg_match("/'(.*)'/is", $wp_version, $out);
$out = $out[1];
preg_match("/\d\.\d/i", $out, $match);
return $match[0];
}

?>

That should be all it takes
 
Last edited:

ata.online

New Member
Messages
159
Reaction score
0
Points
0
Hi,
I added the extra code you mentioned, but it still did not work:

add_filter('the_content','ALL_OPEN_IN_NEW_WINDOW') ;
function ALL_OPEN_IN_NEW_WINDOW($text){
$text = str_replace(' href=', ' target="_BLANK" href=', $text);
return $text;
}

I cleared the browser cookies just to double check.
Anything else that maybe missing here?

Thanks.
 

ata.online

New Member
Messages
159
Reaction score
0
Points
0
Yes, you can intercept must things in wordpress by registering a hook, which will perform a call back to a specific function.

http://adambrown.info/p/wp_hooks/version/2.8

Of the top of my head, I would advise the hook "the_content" which will pass the output of all pages and posts to another function and then output the returned value as the content:

add_filter('the_content','ALL_OPEN_IN_NEW_WINDOW');

all content will now pass through your function ALL_OPEN_IN_NEW_WINDOW.

Within your function simply search for the occurance of a hyperlink in your content and add your additional attribute, something like:

function ALL_OPEN_IN_NEW_WINDOW($text){
$text = str_replace(' href=', ' target="_BLANK" href=', $text);
return $text;
}

This will now amend all hyper links within content to open on a new page, but leave everything else alone.

Take a look at how the clikStats plugin works (a plugin I wrote some time ago), which will shed some light on it.


.....oh forgot to mention:

You should be able to register the hook in your template, within a file named functions.php
(if I remembered correctly) so no need to write an entire plugin

Ok, i was somewhat baffled (and i assume you were) as to why the code does not work. So what you forgot to mention is to delete
base target="_blank" />

from the header template.
I thought let me try that and it worked. Thanks for your help.
 
Last edited:

stardom

New Member
Messages
158
Reaction score
2
Points
0
Hi,
I added the extra code you mentioned, but it still did not work:

add_filter('the_content','ALL_OPEN_IN_NEW_WINDOW') ;
function ALL_OPEN_IN_NEW_WINDOW($text){
$text = str_replace(' href=', ' target="_BLANK" href=', $text);
return $text;
}

I cleared the browser cookies just to double check.
Anything else that maybe missing here?

Thanks.

you are using " you need to be using ' to match the rest of your code.

" = html
' = php
as far as i know of.
 
Top