trying to extract values from a shortcode using a worpdress native function. No dice

I’m trying to figure out how to extract value-attributes pairs from a shortcode. Clearly I only care for the attributes that are there and that the user has entered.

I have it nailed with regex, but I’m told (by a laconic user of SO) that I should be able to do that with a native wordpress function, shortcode_atts, which is described here.

Read More

However for the life of me I cannot get it to return the values.

First off, it is odd to me that I should feed to this function the default values every time I need to use it. It is clearly not designed to extract the values in the first place. But whatever.

$defaults_atts = array(
                    'width'    => 640,
                    'height'   => 360,
                    'mp4'   => '',
                    'autoplay' => '',
                    'poster'   => '',
                    'src'      => '',
                    'loop'     => '',
                    'preload'  => 'metadata',
                    'webm'   => ''
            );
            $rest = substr($post->post_content, 0, -8);  // remove the closing [/video]
            $videoattr = shortcode_atts( $defaults_atts , $rest, 'video' );

notice that I’m using $post->post_content since all the post contains is the shortcode for a video. I only remove the closing shortcode tag I don’t need. (Before you tell that’s wrong, I tried not doing that and nothing changes.)
The shortcode in post_content typically contains attributes ranging from width and height to the source files, mp4 or webm. In a simple case, it may look like this:


Now, when I test the above with print_r($videoattr) all I get is the array with the default values.

What am I doing wrong??

Here’s more testing which fails, following suggestion:

first I modify the my_shortcode function:

function my_shortcode( $atts=array(), $content=null) {
        $attribute = shortcode_atts( array(
                    'width'    => '640',
                    'height'   => '360',
                    'mp4'   => '',
                    'autoplay' => '',
                    'poster'   => '',
                    'src'      => '',
                    'loop'     => '',
                    'preload'  => 'metadata',
                    'webm'   => '',
            ), $atts);

    /*echo '<pre>', print_r($attribute, 1),'</pre>';*/
    echo '<pre>', print_r($atts, 1),'</pre>';
    /*echo '<pre>', print_r($content, 1),'</pre>';*/
}

then I call it using… what I have, which is post_content.

my_shortcode($post->post_content);

This returns, as $atts, the same shortcode I originally fed to the function.

I understand that according to the function above, I should have fed an array of attributes, which is exactly what I don’t have.

Related posts

2 comments

  1. the attributes that are pass on the shortcode are stored in $atts variable, or the first argument on your function

    to give you an idea, here’s how it works,

    lets say you have a shortcode like this

    [mys hello="world" print="no"]Content[/mys]

    and your php function looks like this,

    function my_shortcode( $atts=array(), $content=null) {
        $attribute = shortcode_atts( array(
            'that' => 'is',
            'this' => 'no',
        ), $atts);
        echo '<pre>', print_r($attribute, 1),'</pre>';
        echo '<pre>', print_r($atts, 1),'</pre>';
        echo '<pre>', print_r($content, 1),'</pre>';
    }
    

    the output will be

    #echo '<pre>', print_r($attribute, 1),'</pre>';
    Array (
        [that] => is
        [this] => no
    )
    #echo '<pre>', print_r($atts, 1),'</pre>';
    Array (
        [hello] => world
        [print] => no
    )
    #echo '<pre>', print_r($content, 1),'</pre>';
    Content
    

    $attribute is the default attributes pass into the function,

    $atts – attributes assigned and found on the shortcode tag, it will override the defaults if key matches,

    $content – content that are enclosed inside a shortcode,

Comments are closed.