Quantcast
Channel: WordPress › Support » Forum: Hacks - Recent Topics
Viewing all articles
Browse latest Browse all 8245

aaronpatty on "shortcode_atts returning indexed arrays instead of associative"

$
0
0

Thanks in advance for your help!

Given the following shortcode function definition:

add_shortcode( 'bros_data', 'bros_data_shortcode' );
function bros_data_shortcode( $atts, $content = null ){
	echo var_dump( $atts ) . "<br>";

	echo var_dump(shortcode_atts( array(
		'stuff' => 'bad stuff id',
		'data' => 'bad data id',
		'max' => 'all'
	), $atts, 'bros_data' ) ) . "<br>";

	echo var_dump(shortcode_atts( array(
		'stuff',
		'data',
		'max'
	), $atts ) ) . "<br>";

	extract( shortcode_atts( array(
		'stuff',
		'data',
		'max'
	), $atts, 'bros_data' ) );

	echo var_dump( array('foo' => 'bar') ) . "<br>";

	echo serialize($stuff) . "<br>";
	echo serialize($data) . "<br>";
	echo serialize($max) . "<br>";
}

I execute this with the following shortcode:
[bros_data stuff='id_for_some_stuff' max="10"]

I get the following output:

array(2) { [0]=> string(25) "stuff='id_for_some_stuff'" [1]=> string(8) "max="10"" }
array(3) { ["stuff"]=> string(12) "bad stuff id" ["data"]=> string(11) "bad data id" ["max"]=> string(3) "all" }
array(3) { [0]=> string(25) "stuff='id_for_some_stuff'" [1]=> string(8) "max="10"" [2]=> string(3) "max" }
array(1) { ["foo"]=> string(3) "bar" }
N;
N;
N;

Notice that:

  1. I am getting indexed arrays returned from shortcode_atts() rather than associative arrays. Why?
  2. This means that the extract function doesn't work, so I don't get the variables created that I expect

How do I fix this?

Thanks!


Viewing all articles
Browse latest Browse all 8245

Trending Articles