SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    Barefoot on the Moon! silver trophy Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,615
    Mentioned
    56 Post(s)
    Tagged
    1 Thread(s)

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

    In a post, I have this text:

    Code:
    [bhours shortcode="test" title="test"]
    But the "title" attribute appears to be ignored. Here's a var_dump of the $atts attribute array

    Code:
    array(1) {
      ["shortcode"]=>
      string(4) "test"
    }
    Here's the PHP code:

    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?
    Visit The Blog | Follow On Twitter
    301tool 1.1.5 - URL redirector & shortener (PHP/MySQL)
    Can be hosted on and utilize your own domain

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,234
    Mentioned
    154 Post(s)
    Tagged
    0 Thread(s)
    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

  3. #3
    Mazel tov! bronze trophy kohoutek's Avatar
    Join Date
    Aug 2004
    Location
    Hamburg, Germany
    Posts
    4,248
    Mentioned
    30 Post(s)
    Tagged
    0 Thread(s)
    Like cpradio said, when using attributes, you'd need something like this:

    PHP Code:
    function my_shortcode_handler$atts$content null ) {   

     
    extractshortcode_atts( array(       

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

    $atts ) ); 

    See the codex: http://codex.wordpress.org/Shortcode_API
    Maleika E. A. | Rockatee | Twitter | Dribbble



  4. #4
    Barefoot on the Moon! silver trophy Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,615
    Mentioned
    56 Post(s)
    Tagged
    1 Thread(s)
    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.
    Visit The Blog | Follow On Twitter
    301tool 1.1.5 - URL redirector & shortener (PHP/MySQL)
    Can be hosted on and utilize your own domain

  5. #5
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,234
    Mentioned
    154 Post(s)
    Tagged
    0 Thread(s)
    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?

  6. #6
    Barefoot on the Moon! silver trophy Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,615
    Mentioned
    56 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by cpradio View Post
    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.
    Visit The Blog | Follow On Twitter
    301tool 1.1.5 - URL redirector & shortener (PHP/MySQL)
    Can be hosted on and utilize your own domain

  7. #7
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,234
    Mentioned
    154 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Force Flow View Post
    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.
    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)?

  8. #8
    Mazel tov! bronze trophy kohoutek's Avatar
    Join Date
    Aug 2004
    Location
    Hamburg, Germany
    Posts
    4,248
    Mentioned
    30 Post(s)
    Tagged
    0 Thread(s)
    I just wrote a test shortcode with two attributes. I tested it and it works with "title":


    PHP Code:
    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:

    Code:
    <p>Test this link[link href="example.com" title="test"]Example.com[/link]</p>
    Last edited by Force Flow; Sep 4, 2012 at 14:11. Reason: fixed syntax error
    Maleika E. A. | Rockatee | Twitter | Dribbble



  9. #9
    Barefoot on the Moon! silver trophy Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,615
    Mentioned
    56 Post(s)
    Tagged
    1 Thread(s)
    It didn't work for me.

    The title was an empty string.
    Visit The Blog | Follow On Twitter
    301tool 1.1.5 - URL redirector & shortener (PHP/MySQL)
    Can be hosted on and utilize your own domain

  10. #10
    Barefoot on the Moon! silver trophy Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,615
    Mentioned
    56 Post(s)
    Tagged
    1 Thread(s)
    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.
    Visit The Blog | Follow On Twitter
    301tool 1.1.5 - URL redirector & shortener (PHP/MySQL)
    Can be hosted on and utilize your own domain

  11. #11
    Barefoot on the Moon! silver trophy Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,615
    Mentioned
    56 Post(s)
    Tagged
    1 Thread(s)
    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.
    Visit The Blog | Follow On Twitter
    301tool 1.1.5 - URL redirector & shortener (PHP/MySQL)
    Can be hosted on and utilize your own domain


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •