WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

When you've got your shortcodes defined like this:

    Foo: [mytextbox name="foo"]

And in your php:

function shortcode_myform($atts, $content = null) {
   return '<form action="#" method="post">' . do_shortcode($content) . '</form>';

function shortcode_mytextbox($atts) {
   return '<input type="text" name="'$atts['name']'" />';

What would be the most elegant way to get the name attribute of any children of myform attribute inside the shortcode_myform function?

I figured I could use sessions or globals but that doesn't feel like the right thing to do.

share|improve this question
My solution to this question should be what you need. – s_ha_dum Nov 7 '13 at 14:23
@s_ha_dum That's actually kind of smooth. Thanks! – Joren Nov 7 '13 at 14:27
up vote 1 down vote accepted

Since nobody posted a complete answer, I used the method s_ha_dum described and written a function that returns a shortcode => attributes array/map.

function attribute_map($str, $att = null) {
    $res = array();
    $reg = get_shortcode_regex();
    preg_match_all('~'.$reg.'~',$str, $matches);
    foreach($matches[2] as $key => $name) {
        $parsed = shortcode_parse_atts($matches[3][$key]);
        $parsed = is_array($parsed) ? $parsed : array();

        if(array_key_exists($name, $res)) {
            $arr = array();
            if(is_array($res[$name])) {
                $arr = $res[$name];
            } else {
                $arr[] = $res[$name];

            $arr[] = array_key_exists($att, $parsed) ? $parsed[$att] : $parsed;
            $res[$name] = $arr;

        } else {
            $res[$name] = array_key_exists($att, $parsed) ? $parsed[$att] : $parsed;

    return $res;

Example usage:

Content of the page:

    [inner_code url="#" title="Hello"]

Shortcode implementations:

add_shortcode('outer_shortcode',function($atts,$content) {
    return attribute_map($content);

add_shortcode('inner_shortcode',function($atts,$content) {
    return '';

This will render to:

    [inner_shortcode] => Array
            [url] => #
            [title] => Hello

share|improve this answer
Great Joren.Working perfectly.You saved my time.Accepted your answer. Good luck and Thanks a lot!! – Sumith Harshan May 25 '14 at 9:49

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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