Can I insert short Wordpress text words that are the same code?

I know that you can assign shortcodes if they use the do_shortcode shell, however the code says:

"However, the parser will not work if the short code macro is used to add another macro with the same name:"

Is there any way around this?

for example, if I have a short code for creating a div, for example:

[div]some content in a div[/div] 

I want to be able to use:

 [div] [div]a nested div[/div] [/div] 

but this will crash with the standard do_shortcode wrapper.

My temporary solution is to duplicate the short code with _parent added to the name, but I can only nest 1 level in depth if I did not create div_parent1, div_parent2, etc ...

+4
source share
3 answers

If you write short code, there is a simple solution. You can write several short codes that call the same function. I have shortcodes for creating html blocks such as divs, and there are several with names like div, block1, block2, for example.

 add_shortcode('div', 'devondev_block'); add_shortcode('block', 'devondev_block'); add_shortcode('block2', 'devondev_block'); 

They all call the same function. They can be nested until you remember to use different short codes.

WordPress Short Code Support suffers from trying to parse only with a regular expression. Such parsing can be done with a mixture of regular expressions, a finite state machine, and a stack. This approach can handle nesting and can be very fast, especially when there are very few short codes. Every time I come across this, I have a desire to try.

+3
source

The API says this is so, therefore it is not possible:

This is a limitation of the context-free regexp parser used by do_shortcode() - it is very fast but does not count levels of nesting, so it can't match each opening tag with its correct closing tag in these cases.

Corresponding functions in the latest version (3.4.2):

 function do_shortcode($content) { global $shortcode_tags; if (empty($shortcode_tags) || !is_array($shortcode_tags)) return $content; $pattern = get_shortcode_regex(); return preg_replace_callback( "/$pattern/s", 'do_shortcode_tag', $content ); } function get_shortcode_regex() { global $shortcode_tags; $tagnames = array_keys($shortcode_tags); $tagregexp = join( '|', array_map('preg_quote', $tagnames) ); // WARNING! Do not change this regex without changing do_shortcode_tag() and strip_shortcode_tag() return '\\[' // Opening bracket . '(\\[?)' // 1: Optional second opening bracket for escaping shortcodes: [[tag]] . "($tagregexp)" // 2: Shortcode name . '\\b' // Word boundary . '(' // 3: Unroll the loop: Inside the opening shortcode tag . '[^\\]\\/]*' // Not a closing bracket or forward slash . '(?:' . '\\/(?!\\])' // A forward slash not followed by a closing bracket . '[^\\]\\/]*' // Not a closing bracket or forward slash . ')*?' . ')' . '(?:' . '(\\/)' // 4: Self closing tag ... . '\\]' // ... and closing bracket . '|' . '\\]' // Closing bracket . '(?:' . '(' // 5: Unroll the loop: Optionally, anything between the opening and closing shortcode tags . '[^\\[]*+' // Not an opening bracket . '(?:' . '\\[(?!\\/\\2\\])' // An opening bracket not followed by the closing shortcode tag . '[^\\[]*+' // Not an opening bracket . ')*+' . ')' . '\\[\\/\\2\\]' // Closing shortcode tag . ')?' . ')' . '(\\]?)'; // 6: Optional second closing brocket for escaping shortcodes: [[tag]] } 
+1
source

You need to make short codes for the content in your short code. Example:

 add_shortcode('div', function($attributes, $content, $tag) { ... do_shortcode($content); ... }); 

See also: What is the best way to include nested shortcodes? (Wordpress SE)

Just seeing: this does not solve the problem in Wordpress, therefore it does not answer your question. I thought this would be fixed so far.

0
source

Source: https://habr.com/ru/post/1443521/


All Articles