Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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"]
share|improve this question
    
Alex can you edit your question and provide your full code example. Maybe if we see the whole thing we can be more helpful. –  Stephen Nov 25 '12 at 4:18
add comment

3 Answers

First of all, you're using extract / shortcode_atts in correctly:

The line:

   extract(shortcode_atts(array(
        "li" => get_option('li'),
    ), $liatts));

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:

function lishortcode($liatts) {

  //Merge provide attributes with defaults. Whitelist attributes
  $atts = shortcode_atts(array(
             "url" => get_option('li'), 
             "foo" => "bar"
           ), $liatts));

  /* Url is in $atts['url']. Do something and then return something */

  return $atts['url'];
}

add_filter('widget_text', 'do_shortcode');
add_shortcode('LI', 'lishortcode');

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.

share|improve this answer
add comment

If you turn on debugging-- define('WP_DEBUG',true); in your wp-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:

IMPORTANT TIP - Don't use camelCase or UPPER-CASE for your $atts attribute names $atts values are lower-cased during shortcode_atts( array( 'attr_1' => 'attr_1 default', // ...etc ), $atts ) processing, so you might want to just use lower-case.

What I am guessing you want there is $lib=$li['url'];

share|improve this answer
    
just needed to change to lowercase! thanks (although another variable with uppercase works fine. odd) –  manc Nov 23 '12 at 16:55
add comment
function lishortcode($liatts) {
// $liatts here is an array( 'URL' => "http://www.differenturl.com/somewhere" )

    extract(shortcode_atts(array("li" => get_option('li'),), $liatts));
    // the extract() would convert the $liatts array to variables;
    // in this case $URL with a value of "http://www.differenturl.com/somewhere"


    // shortcode_atts() here filters the array $liatts before the extract
    // in this case you're telling it to filter out any key other than 'li'
    // if no li key exists use the default value retrieved with the get_option() 
    // since your shortcode example doesn't contain a 'li' you'll end up with a single 
    // value of $li with the default value from the get_option() call

    foreach ($liatts as $key => $option) {
            $li[$key] = $option;
    }

    // With the example provided there is no point to this foreach loop
    $lib=$li[URL];
  return $lib;
  // return the default value of $li[URL], assuming that it had a value.

}

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.

share|improve this answer
    
Nothing is output at all with $URL. get_option is used as there are more variables in an array stored within the admin backend using defaults, then the user can overwrite the defaults within the shortcode. All other variables work fine with the same pattern –  manc Nov 23 '12 at 16:33
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.