WP plugin: Add Link Attribute
- plugin:
- Add Link Attribute
- version:
- 0.3
- download:
- add-link-attr.zip
- source:
- add-link-attr.php
Add Link Attribute is a WordPress plugin that lets you insert your own HTML tag attributes into (assumedly any) template function-generated links, without the need to rewrite those functions directly. Just use it in place of — or rather, along with — the specific template function.
The purpose of Add Link Attribute is to avoid having to edit the core source code just to add an attribute into an HTML link of say your link or archive list. You could even use it to drop attributes into links you’ve placed in your blog posts, if you’re so inclined. To install, download the zip, extract add-link-attr.php, upload this to your wp-content/plugins/ directory, and activate the plugin.
Usage:
<?php add_link_attr('function', 'arguments', 'attribute', imgtag); ?>
Parameters:
- function
- (string) Name of the WordPress function to call, stripped of it’s PHP syntax (i.e. ‘list_cats’).
- arguments
- (string) The parameters you normally pass to the WordPress function.
- attribute
- (string) The HTML tag attribute(s) to insert in the link tag. To include more than one attribute, separate them with a space.
- imgtag
- (boolean) Insert attribute(s) into img/image tag (
TRUE), or into a/anchor tag (FALSE). Defaults toFALSE.
Examples:
<?php add_link_attr('get_calendar', '2',
'target="content_frame" style="font-weight: bold;"'); ?>
<?php add_link_attr('the_content', '',
'onclick="window.open(this.href,'_blank');return false;"'); ?>
<?php add_link_attr('get_links_list', '',
'width="20" height="30"', true); ?>
Update [Mar-27-2005]: Version 0.3 corrects a problem with multiple arguments (parameters) and WordPress functions using the PHP-function style format. The following are the correct structures for multiple arguments:
‘string, int, bool’
<?php add_link_attr('get_linksbyname', 'Blog, <li>, </li>,, FALSE, name'),
'onclick="window.open(this.href,'_blank');return false;"'); ?>
array(‘string’, int, bool)
<?php add_link_attr('get_linksbyname', array('Blog', '<li>', '</li>', '', FALSE, 'name'),
'onclick="window.open(this.href,'_blank');return false;"'); ?>
With the first, just don’t use single or double-quotes as delimiters for individual string values, unless they’re actually a part of that string.
imgtag parameter, which lets you pass attributes to the <img> HTML tag; useful when using images in the link-related template tags.
Author: Kaf Oseo
Categories: WordPress
Comments: (37)
I’m trying to use your plug-in to do something fairly simple (break XHTML rules and add a target=”_blank” to links, specifically the quicklinks button). I think I’ve figured out how to do it manually (edit quicklinks.js), but I’d like to use your plugin to avoid editing source.
I’ve got it installed and activated, but I’m somewhat confused (common) about what to do next. Where would I insert the php code?
Thanks,
Roddie
Roddie, sounds like you want to add
target="_blank"to links in your posts (which I do not condone, of course). All you need do is edit your index.php template (with 1.5, this also could include a theme’s single.php, archive.php, etc.) and change the reference of:<?php the_content(); ?>to:
<?php add_link_attr('the_content', '', 'target="_blank"'); ?>If you’re passing an argument to
the_content()(i.e. more link text), add it as the second parameter:<?php add_link_attr('the_content', 'Read on...', 'target="_blank"'); ?>Thanks, Kaf – Everything worked eventually (once I got around privoxy).
My next step is to blog about it, haha.
Oh – BTW, the new theme is nice, but it shows the comment forms as closed. I had to go back to the old theme.
R.
Good to hear, Roddie. And thanks for the comment on the theme—as for closed comments, that’s only because I’m doing some custom moderation behind the scenes the theme is ignorant of.
Me again, Kaf :-)
So I’ve messed a bit with CSS to add a tooltip and a dashed underline to my links using your plugin, but I’m still struggling with the non-external links getting treated the same (ie. trackbacks to my own posts, the “Read more…” links, etc.
If one wanted to check to see if a link was internal or external (ie. based on the domain name in the HREF), and then decide whether or not to add the attributes (in my case, the “target=_blank”), where would be the best place to make the decision?
At first I thought the index.php/single.php/archive.php etc files, but then I realized that it might be better to do it in the plugin instead, and since I know next to nothing about php, I figured I’d come back here and ask.
Thanks for your time,
Roddie
Hi,Kafkaesquí, thanks a lot for the plugin, it’s really handy.But I have one question. If the called function needs more than one argument to operate, what should we pass in the add_link_attr() function. Like below:
list_cats(0, ”, ‘name’, ‘asc’, ”, 1, 0, 1, 1, 1, 1, 0,”,”,”,”,”). If it happens to call such a function,what we do with the arguments in the add_link_attr()?
Br,
Jia
I use call_user_func_array() instead of call_user_func() in your codes, and it works fine. I can now pass an array parameters to the WP’s function. Like the example above, I can use add_link_attr(‘list_cats’, array(0, ”, ‘name’, ‘asc’, ”, 1, 0, 1, 1, 1, 1, 0,”,”,”,”,”), ‘onclick=’return checkLink(this.href)”, ”) to make it work. It’s really good.
hejia, I thought I’d taken care of multiple arguments, but…anyway, thanks for catching it.
I’ve updated Get Link Attribute so it will now accept them both ways: a string of text with parameters separated by commas, or within the PHP
array()construct (functions that accept parameters in query-string format work the same). I’m all about making things easy. Well, easier.Hi, I’ve been trying to use your plugin to add a target=_blank to the commentor link, but
doens’t work… Am I missing anything?
My WP version is 1.5.1. Thanks!
“doens’t work… Am I missing anything?”
Possibly. It would help to know what’s meant by “commentor link.” If this is the one generated by the comment_author_url() tag, you only need modify your comments template (i.e. replace comment_author_url()) with:
<?php add_link_attr('comment_author_url', '', 'target="_blank"'); ?>Just installed your plug-in, not ready to mess things up yet. What I would like to know is can I use this plug-in to move a link that is in my meta section to my pages section (it’s a photo gallery that I would like visitors to see w/ my other pages rather than hidden under the meta heading). Any ideas? Thanks,
-Bryce
I am a newbie in wordpress. My question is not related to the topic above — the plug in. Just wondering if you have some solutions on how to make and let wp run the cycling banner (java) scripts in WordPress.
This script works well with html based page. Is there something i can do to make it run.
Rolly
I am a newbie in WordPress. My question is not related to the topic above — the plug in. Just wondering if you have some solutions on how to make and let wp run the cycling banner (java) scripts in WordPress.
This script works well with html based page. Is there something i can do to make it run.
What I need is to have an ability to attach a string to the beginning of the url itself. I.E. add http://www.mydomain.com/whatever.php?file= the actual url, so that I can run the link through my scripts. Right now I have to hack includes/links.php.
AWESOME plugin. I have been trying on my own to do something similar but wasn’t failing. Now I can just use this plugin. Dude do you need your grass cut or something? Thank you!
Thanks guff. this plugin was just the thing I was looking for to sort out the feedwordpress plugin. I’ll paypal you if i ever make any money. thanks
How about a plugin to show that links have recently been updated?
yes How about a plugin to show that links have recently been updated?
hi daf: hi folks:
thankyou for your time.
i wish use your plugin to make all external links open in new window.
i am very experienced in uploading and activating wp plugins – yet i need assistance to make your plugin open all links in new windows ….
i have 2 questions:
QUESTION 1
what else do i need to do
- i have already
uploaded plugin
activated plugin
put below code in header.php
QUESTION 2
how and where do i add these windows features? = “toolbar=no,location=no,directories=no,menubar=no, scrollbars=yes, status=yes, resizable=yes, width=600,height=400″;
altoyes
PS i downloaded the plugin from
http://guff.szub.net/2005/01/27/add-link-attribute/
That was exactly the plugin I was looking for. Thanks. Now I don’t have to edit the source code anymore. That’s really an improvement and will speed up writing articles.
Superb. A very useful plugin that adds a still missing feature in WP2.1. Thank you very much for your work.
Hey, great Plugin. Thanks a lot from Jürgen :-)
Is there any chance you could port this over to Drupal? Please don’t flame me :)
Wow! Thanks for this great plugin for my wordpress site! No need to rewrite funtions..yipee! :D
Wow. Very cool plugin.
Thanks so much.
Hi
I just downloaded Add Link Attribute to my arsenal of weapons.
Thanks for making this available. Is it okay if I forward my
list here to download this awesome tool?
Thanks,
Kelley Brown
http://www.thenewbiecentre.com
Thanks Kaf for this great plugin,
i’m goint to install it right now
;)
Bruce
Can you put wordpress variables in the new attribute string?
For example:
onClick=”popinfo(‘/outgoing/**linkname**’);”
Where **linkname** would be some template tag perhaps?
Something to make it unique to each link?
Hi, I’ve tried to use this plugin to add the rel=”nofollow” to the “Comments >>” link on the blog home page with this complex code (still haven’t figure it out yet =):
From:
To:
I’m just wondering if that’s correct, since it has the messy localization function in it (for translation) of the theme to another language =P.
Hope you can give an example for functions with localization codes in them.
–M.T.–
Hi
can i use this plugin for search .
eg:any link present in my post should lead to wordpress search
thx for your pretty cool plugin!
Even though it looks like this was last updated almost 3 years ago – it still works great.
Thank you for saving us a bunch of headaches and time!
Having problems passing arguments to the category list function. I’ve tried this, but it doesn’t work:
add_link_attr(‘list_cats’, ‘sort_column=name, optioncount=1′, ‘rel=”nofollow”‘)
Any suggestions? Thank you for the plugin.
Hi Kaf et al. Great plugin. I got it working fine to add rel=”nofollow” to single.php category links, but I’ve run into an odd problem.
When editing a published post, clicking Save on the post results in this error message:
Warning: Cannot modify header information - headers already sent by (output started at /path-information/blog/wp-content/plugins/add-link-attr.php:59) in /path-information/blog/wp-includes/pluggable.php on line 391Here’s the relevant code from pluggable.php:
// Cookie safe redirect. Works around IIS Set-Cookie bug.// http://support.microsoft.com/kb/q176113/
if ( !function_exists('wp_redirect') ) :
function wp_redirect($location, $status = 302) {
global $is_IIS;
$location = apply_filters('wp_redirect', $location, $status);
if ( !$location ) // allows the wp_redirect filter to cancel a redirect
return false;
$location = wp_sanitize_redirect($location);
if ( $is_IIS ) {
header("Refresh: 0;url=$location");
} else {
if ( php_sapi_name() != 'cgi-fcgi' )
status_header($status); // This causes problems on IIS and some FastCGI setups
header("Location: $location");
}
}
endif;
Line 391 in the code above is:
header("Location: $location");The only call to add-link-attr.php is in single.php in my template. Don’t know why I’d be getting an error about headers already being sent since the plug-in doesn’t change anything there.
Hmmm. I just tried deactivating the plug-in my WordPress admin, and I’m getting the same error…
Any help would be appreciated. It would be a shame if I had to bin this plug-in…
Thanks!
Sorry, comments have been closed for this post.