I have devved a small plugin based on a shortcode. Here’s a stripped down version of the code for the shortcode itself:
function lishortcode($liatts) {
extract(shortcode_atts(array(
"li" => get_option('li'),
), $liatts));
foreach ($liatts as $key => $option) {
$li[$key] = $option;
}
$lib=$li[URL];
return $lib;
}
add_filter('widget_text', 'do_shortcode');
add_shortcode('LI', 'LIshortcode');
The URL parameter is one of 5 variables called in the shortcode. All the others work except for this one. Is there a reason for this? The following 2 shortcodes output the same URL:
[LI]
[LI URL="http://www.differenturl.com/somewhere"]
First of all, you’re using
extract
/shortcode_atts
in correctly:The line:
uses
shortcode_atts
and then extracts turns each key in the returnarray into its own variable: e.g.$li
in this example. But you then go onto use$liatts
anyway – which is just the attributes passed from the shortcode and can contain anything. Nor does it has any defaults provided.Judging from your example you want to do something like:
See Codex, – note
shortcode_atts()
not only provides defaults for missing values, but also strips out any attributes you don’t specify. In the above example, only the ‘url’ and ‘foo’ attribute will be in present in$atts
.If you turn on debugging–
define('WP_DEBUG',true);
in yourwp-config.php
— you will see that there are several things wrong with that shortcode.I should start by saying that nothing is returned when I try your code. I am guessing that is because I have no ‘li’ key in my
*_options
table, and you did not explain what values are in that key. (That is a very bad key name by the way– much too short and generic)I will venture a guess that the problem is here:
$lib=$li[URL];
You are not setting the “URL” constant anywhere, so PHP starts guessing. If you expect that to be the URL attribute of your shortcode, you’d be wrong, mostly because your attributes are all lowercase. This happens whether you want it to or not:What I am guessing you want there is
$lib=$li['url'];
I understand that you abbreviated your code here, but with the example you’ve provided I’m hard put to understand what you’re trying to accomplish.
Also take a good look at the other answers for some pointers on writing shortcode functions.