Nest shortcode in another shortcode

I want to nest a shortcode into the [map] shortcode that comes as part of the google maps plugin. It might be more appropriate to say this is a shortcode used as a parameter in another shortcode. I'll try to explain below.

Like so:
[map id="meta value for agm_map_created"]

Where the "meta value from agm_map_created" is represented by a shortcode like [meta data]

[map id="[meta data]"]

All i get is "] because the shortcode terminates on the wrong closing bracket.

I have looked and saw a curly bracket approach like

[map id="{meta value for agm_map_created}"]
sometimes works, but it did not work for me.

I hope this makes sense :slight_smile:

Perhaps there is easier way to reference meta data from within a post without using a shortcode?

Thank you,

Jamie

  • Michael Bissett
    • Recruit

    Hey @jamie, hope you're doing well today! :slight_smile:

    Could you elaborate on what you're looking to accomplish here, and why?

    I know that you're wanting to pull the metadata for a Google Map created, but I'm not sure why presently. Not to mention, nesting a shortcode like this would usually involve some element of PHP coding, but once again, it would help to know more about the "why" behind all of this. :slight_smile:

    Please advise,
    Michael

  • jamie
    • Site Builder, Child of Zeus

    Thanks for replying.

    The reason behind this is because the posts with the maps associated are created from user input. When the post is created I need to include the script to display the map. Since the posts are created on demand and I do not know the map id prior to the creation, I need to refer to the map id based on the meta data.

    Prior to this I used the [location] shortcode which is able to accept a shortcode that links directly to the value the user inputs, unlike map. Problem is using [lcoation] approach yielded two maps for each post. One created by the [location] shortcode and another by the custom field value.

    If you want further explanation, let me know.

  • jamie
    • Site Builder, Child of Zeus

    For the record:

    I was able to use [location address="[124]"] where [124] was a shortcode to a database field.

    I am unable to use [map id="[metadata]"] where metadata is a shortcode to the value in agm_map created.

  • Ash
    • WordPress Hacker

    Hello @jamie

    Please try to use the following code:

    add_filter( 'agm-shortcode-process', 'agm_shortcode_process_cb', 99, 1 );
    function agm_shortcode_process_cb( $atts ){
        foreach( $atts as $key => $val ){
            $atts[$key] = do_shortcode( $val );
        }
        return $atts;
    }

    You can add those codes in your functions.php in the theme, if you think your theme won’t be changed. Otherwise mu-plugins is the best solution. To use mu-plugins, go to /wp-content/ and find the folder with name 'mu-plugins'. If there is no folder in that name, then create a folder, name it 'mu-plugins', create a file inside that, give any name you like and paste the code in there. You don't need to activate that plugin. Mu-plugins means must use plugins, so it will be activated automatically always. If you use mu-plugins then add a php start tag at the beginning of the code.

    Hope it helps :slight_smile: Please feel free to ask more question if you have.

    Cheers
    Ash

  • jamie
    • Site Builder, Child of Zeus

    Ashok,

    That did not make a difference, unfortunately. All I can figure is that the shortcode I am using to address the meta data is not programmed in a way to accommodate this use.

    I am rethinking the design. I appreciate the suggestion nonetheless.

    Jamie

  • Ash
    • WordPress Hacker

    In my above code if you use like this:

    add_filter( 'agm-shortcode-process', 'agm_shortcode_process_cb', 99, 1 );
    function agm_shortcode_process_cb( $atts ){
        echo 100 . do_shortcode('[metadata]') . 100;
        foreach( $atts as $key => $val ){
            $atts[$key] =( $val );
        }
        return $atts;
    }

    Do you see any output in between 2 100s?

    Cheers
    Ash

  • jamie
    • Site Builder, Child of Zeus

    ASh,

    Thank you for the follow up.

    Are you suggesting I put the metadata key [agm_map_created] in place of the [metadata] in your code?

    Otherwise I am not sure I follow how you expect this code to address my situation.

    Jamie

  • jamie
    • Site Builder, Child of Zeus

    Ashok,

    I have had some additional thought on this. I may be beginning to see light at the end of the tunnel. I need to make a shortcode that simply gets the specific value for the agm_map_created meta value.

    The current plugin that taps into the metadata takes a second value that may be the reason for the failed parse of the shortcode arguments.

    Keep you posted!

    Jamie

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.