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.

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?

share|improve this question
1  
have you read the Codex: codex.wordpress.org/Shortcode_API –  Michael Aug 28 '12 at 14:05
    
Of course. There was no mention of this issue. –  Force Flow Aug 28 '12 at 17:06
add comment

2 Answers

up vote 0 down vote accepted

I figured out the problem.

I had a function that was stripping the title attribute from the HTML, but couldn't differentiate between a shortcode and an HTML tag.

So, I converted over from regex to DOMDocument.

http://stackoverflow.com/questions/15252770/php-preg-replace-match-html-attribute

share|improve this answer
add comment

It appears that you are not extracting your $atts correctly or at all for that matter.

Use the correct procedure to get access to the passed attributes allong with setting up defaults as a fallback:

extract( shortcode_atts( array(
    'shortcode' => 'default_value',
    'title' => 'Default Title',
), $atts ) );

There is infact an extensive guideline on shortcode attributes in the Codex, http://codex.wordpress.org/Shortcode_API#Attributes

When in doubt just copy the WP examples and take if from there.

share|improve this answer
    
The problem is that I want to be able to tell the difference between [bhours title=""], [bhours title="my title"], and [bhours], so I don't necessarily want to set a default value. –  Force Flow Aug 28 '12 at 17:07
    
If I use the code as you suggest, the value of the title attribute does not get passed to the $title variable. However, the $shortcode variable works fine. Again, this brings us back to the original question--why does "title" not appear in the $atts array? After all, you still need it to appear in the array in order to access it with the extract function. –  Force Flow Aug 28 '12 at 17:17
    
Perhaps title is reserved somehow? Test you short code with more than just these two fields and see if 'title' after all is just not going to be an option. –  Foxinni Aug 29 '12 at 10:18
    
That was my question. I tried [bhours shortcode="test" compress="true" title="my title"], but the title attribute still didn't appear. –  Force Flow Aug 29 '12 at 12:39
    
I found another interesting thing. If I use [bhours show_title="true"], the show_title attribute doesn't appear. if I misspell "title" like this: [bhours 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. –  Force Flow Mar 6 '13 at 15:47
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.