I've created a shortcode which outputs product schema in JSON-LD format.
1 schortcode = 1 product = 1 product schema output 2 schortcode = 2 product = 2 product schema output
The problem is that then I've realized that all the products in an article should create a single JSON-LD (see the image).
2 schortcode = 2 product = 1 product schema output
This means that the schema output should be outputted only by the last shortcode of this kind.
The question
Shortcodes output in wordpress are usually independent one from the others, they are "stateless" (the output depends only on the input parameters). But can I force them to a different behaviour? I've thought of using global variables inside the shortcode handler and output the aggregated JSON only after the last shortcode has been called).
Is this a good solution?
I've created a shortcode which outputs product schema in JSON-LD format.
1 schortcode = 1 product = 1 product schema output 2 schortcode = 2 product = 2 product schema output
The problem is that then I've realized that all the products in an article should create a single JSON-LD (see the image).
2 schortcode = 2 product = 1 product schema output
This means that the schema output should be outputted only by the last shortcode of this kind.
The question
Shortcodes output in wordpress are usually independent one from the others, they are "stateless" (the output depends only on the input parameters). But can I force them to a different behaviour? I've thought of using global variables inside the shortcode handler and output the aggregated JSON only after the last shortcode has been called).
Is this a good solution?
Share Improve this question edited Jun 15, 2020 at 8:21 CommunityBot 1 asked Jul 9, 2019 at 22:05 ReviousRevious 3233 silver badges16 bronze badges 2- I have no idea what you are talking about. – Justin Waulters Commented Jul 10, 2019 at 2:06
- @JustinWaulters: can you see if now the question is clear? – Revious Commented Jul 10, 2019 at 7:25
1 Answer
Reset to default 1If you have full control over the system, it would be best to get all of the data by some other means.
If you must concatenate the shortcodes, you can parse the content to get the shortcodes with get_shortcode_regex().
You can save all the matches, and their attributes, and remove them from the content. Then, you can either move them all to the end (or where ever you want them) or process them to get whatever data you need.
On the other hand, you could just use microdata schema markup in your html instead of the JSON-LD format. For an example, see Example 1 here and click the microdata tab.
Hope this helps