0

I'm creating a shortcode as part of a plugin that I'm calling in a template like this:

do_shortcode('[shortcodename title="monkeys"]');

The function looks like this to begin with:

function shortcodename_function($atts = []) {
// stuff
}

Inside the function, I'm trying to print_f the $atts out before running shortcode_atts to confirm they're there, but I'm getting nothing. It's like it's flat out ignoring the attributes I apply. Once I run shortcode_atts, I get my defaults in and these appears correctly in a print_f.

If I pop the shortcode into the content of a post with attributes, it outputs these fine. Is there some element of do_shortcode that is ignoring my attributes? Or is there something to do with the order of things being initialised? Attributes aren't essential for this, but they'd be nice and I'd like to know why it's not working the way I'm thinking it should.

The whole sanitised thing looks like this:

function shortcodename_function($atts = []) {

    $pairs = array(
        'title' => 'bananas'
    );

    $a = shortcode_atts($pairs, $atts);

}

and again, in the template:

echo do_shortcode('[shortcodename title="monkeys"]');

this shortcode is set up within a plugin, so not sure if that makes a difference.

edited: added an 'echo' to better represent how it's being displayed

2
  • Assuming you want this in a template, echo do_shortcode('[shortcodename title="monkeys"]'); isn't working? ...you have to use echo which I don't see/read you are using?! Take a quick look in the Codex please.
    – Charles
    Commented May 30, 2017 at 14:46
  • Sorry yes, I didn't specify it in the question but in my template I am echoing the do_shortcode([shortcode attribute="value"])
    – efreeman
    Commented May 30, 2017 at 14:59

1 Answer 1

1

You maybe need to return the output at the end of the function, then do't forget to echo the do_shortcode().

function shortcodename_function($atts) {

    $pairs = array(
        'title' => 'bananas'
    );

    $a = shortcode_atts($pairs, $atts);

    return $a['title'];
}

echo do_shortcode('[shortcodename title="monkeys"]');

Of course you can echo directly in the function, but it's a bad pratice for positioning the data in many situation (do_shortcode embed in an action callback function...).

2
  • I've tried commenting everything out and simply returning $atts, again I get nothing when I echo it in the template but get 'Array' when it's used on the content block of a post. Good call on not doing echo directly in the function, a bad habit of mine! I'll adjust that for now
    – efreeman
    Commented May 30, 2017 at 15:04
  • yes array that because function parameter is [] and it must be "" or $atts, as I paste your code I didn't mention this. I correct my answer now...
    – Benoti
    Commented May 30, 2017 at 15:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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