When using a shortcode, using "title" as an attribute doesn't work

In a post, I have this text:

[bhours shortcode="test" title="test"]

But the “title” attribute appears to be ignored. Here’s a var_dump of the $atts attribute array

array(1) {
  ["shortcode"]=>
  string(4) "test"
}

Here’s the PHP code:

function bhour_shortcode_handler($atts){
    global $post,$bhourdays;


    $output='';


    echo var_dump($atts);


    if(isset($atts['shortcode']) && !empty($atts['shortcode'])){




        if(isset($atts['title']) && empty($atts['title'])){
            $output.='';
        }
        elseif(isset($atts['title']) && !empty($atts['title'])){
            $output.='<h3>'.$atts['title'].'</h3>';
        }
        else{
            //get title from post           
        }


    }




    $output.='<p>this is a test</p>';


    return $output;
}

add_shortcode('bhours', 'bhour_shortcode_handler');

Is the “title” attribute some sort of reserved word?

I’m not 100% certain on this, but I think you need to call extract(shortcode_atts()) as shown in the Google Charts section on this site

Like cpradio said, when using attributes, you’d need something like this:

function my_shortcode_handler( $atts, $content = null ) {   

 extract( shortcode_atts( array(       

'attr_1' => 'attribute 1 default',       
'attr_2' => 'attribute 2 default',       

$atts ) ); 
}

See the codex: http://codex.wordpress.org/Shortcode_API

As far as I can tell, all the attributes are passed in as an array, so the most direct way to access them is through the $atts array. The extract and shortcode_atts functions just seem to prepare and create the variables.

The “title” attribute doesn’t appear in that array to begin with.

If I use the extract functions as indicated in the wordpress codex, the variable for the title always returns the default value, which says to me that it is never set.

Strange question, but have you tried adding the $content = null to your function as a second parameter so it matches the method definition that wordpress might expect?

All of the search results I have read never mention title as a reserved word, so I don’t think that is the issue… Have you tried changing it to header instead of title, to see if it comes across?

Adding the content variable doesn’t seem to make a difference.

It takes attributes named “header” and “name” without any problems. It’s only with “title” that seems to give me trouble. :confused:

This is very peculiar. As I looked at the shortcodes.php file included in WordPress and there isn’t any logic that should stop title from working. Do you have any other plugins activated that may be interfering? Maybe there is a overflow in shortcodes that if title is used in another shortcode by another plugin it adversely affects other shortcodes (doesn’t seem to make sense to me though)?

I just wrote a test shortcode with two attributes. I tested it and it works with “title”:

function my_link($atts, $content = "") {
    extract(shortcode_atts(array(
        "href" =>  'http://',
        "title" => ''
    ), $atts));
    
        $output= '<a href="'.$href.'" title="'.$title.'">'.$content.'</a>';
    
        return $output;
}

add_shortcode('link', 'my_link');

And the shortcode tag:


<p>Test this link[link href="example.com" title="test"]Example.com[/link]</p>

It didn’t work for me.

The title was an empty string.

I found another interesting thing on this unsolved issue. If I use [link show_title=“true”], the show_title attribute doesn’t appear. if I misspell “title” like this: [link show_ttitle=“true”], then the attribute show_ttitle appears. There is something strange happening whenever I use the word “title” in an attribute–it seems like it’s ignored every time for some reason. I don’t understand it.

I finally figured it out.

I had a function in the functions.php file that strips attributes from html tags. One of those attributes that was set to be stripped was “title”. The regex code I was using couldn’t tell the difference between [shortcode title=“something”] and <a href=“link” title=“”>, so the title attribute was removed in both cases. I’ve since converted this code to use a DOM parser to replace the regex.