最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugin development - shortcode tags not working in do_shortcode

programmeradmin0浏览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

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

Share Improve this question edited May 30, 2017 at 15:00 efreeman asked May 30, 2017 at 14:34 efreemanefreeman 1486 bronze badges 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
Add a comment  | 

1 Answer 1

Reset to default 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...).

发布评论

评论列表(0)

  1. 暂无评论