Is it possible to pass an associative array with a shortcode attribute?
-
Let's say there is a shortcode like this [my_shortcode arg1="foo" arg2="bar" ] OnĀ the PHP side, the associated shortcode callback function will receiveĀ the parameter as an associative array and it will be like this. array( 'arg1' => 'foo', 'arg2' => 'bar' ); I'm wondering if it is possible to pass an associative array from the shortcode. [my_shortcode arg[a]="foo" arg[b]="bar"] will receive array( 'arg' => array( 'a' => 'foo', 'b' => 'bar' ) );
-
Answer:
To the best my knowledge, the answer to your question is no. However, you can pass a string, delimited by a comma, pipe (|) or similar, and then use PHP explode() function (http://php.net/manual/en/function.explode.php) to get that into an array. You can fake the associative array: arg1|foo,arg2|bar,args|etc. You can also serialize the array so it's a string.
Mark Simchock at Quora Visit the source
Other answers
Unfortunately the answer is no to your proposed syntax. With your syntax WordPress passes in a single argument of "arg[a" to your shortcode function. The problem is shortcodes are "dumb" and end with the first closing square bracket. However, if the less appealing syntax of using parenthesis is acceptable: [pass_array arg(a)="foo" arg(b)='bar' arg(c)=baz] Then pass_array_parse_atts() is a function you can use in your shortcode function to parse the attributes to get what you are looking for: function pass_array_parse_atts( $atts, $expected ) { $args = array(); foreach( $atts as $index => $att ) { if ( preg_match( '#^(.+)\((.+)\)=["\']?(.+)$#', $att, $match ) ) { // We have an arrray attribute where $att is something like: foo(1)="bar" $args[$match[1]][$match[2]] = rtrim( rtrim( $match[3], '"' ), "'" ); } else { // We have a simple attribute where $att is something like: foo="bar" list( $key, $value ) = explode( '=', $att ); $args[$key] = $value; } } return wp_parse_args( $args, $expected ); } Here is the code for a shortcode that shows how to use it; the shortcode outputs a table showing the keys and values passed into the shortcode as "arg": function pass_array_shortcode( $atts ) { $args = pass_array_parse_atts( $atts, array( 'arg' => array() ) ); echo '<table>'; foreach( $args['arg'] as $key => $value ) { echo "<tr><th>{$key}</th><td>{$value}</td></tr>"; } echo '</table>'; } add_shortcode( 'pass_array', 'pass_array_shortcode' ); Hope this helps.
Mike Schinkel
You can pass a JSON object in a short code: [myshortcode values='{"a":"foo","b":"bar"}'] Later, in your shortcode function: public static function mytheme_myshortcode( $atts, $content = null ) { extract( shortcode_atts( array( "values" = "", ), $atts ) ); $values = json_decode( $values, true ); if( !is_array( $values ) or count( $values )<1 ) $values = array(); // do something... }
Marco Panichi
Related Q & A:
- How do I add to an array from a static method?Best solution by stackoverflow.com
- How do you pass the last level in a new Super Mario on ds?Best solution by Yahoo! Answers
- Can I pass Ohio drivers test in a rental car?Best solution by Yahoo! Answers
- Is it possible to play back up games from a SD card on a DSi?Best solution by wiki.answers.com
- Is it possible to double major and get both a BFA and a BA?Best solution by quora.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.