0

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?

enter image description here

  • I have no idea what you are talking about. – Justin Waulters Jul 10 at 2:06
  • @JustinWaulters: can you see if now the question is clear? – Revious Jul 10 at 7:25
1

If 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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