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 |1 Answer
Reset to default 1You 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...).
echo do_shortcode('[shortcodename title="monkeys"]');
isn't working? ...you have to useecho
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