0
votes

I need to surround Wordpress Shortcodes for translating the content. These are the pre-requisites of the issue:

  • I've to surrounding "the" shortcode, not the shortcode's content. For example <div class="notranslate">[shortcodename parameter="1"]</div>Shortcode content in italian<div class="notranslate">[/shortcodename]</div>
  • After that, I need to remove the .notranslate tag from the API response [shortcodename parameter="1"]Shortcode content in english[/shortcodename]

Can anyone help me? I'm not expert in RegEx. The plugin is writed in PHP and I'm trying to use Wordpress API for better integration (for example It should be better if the tool matches only the registered shortcodes and escaping the default shortcode's escaping syntax). But the core of the request is find the right regexes for find and replace.

References

Regards Mattia

1
Use an HTML parser to manipulate HTML content in this way. Using regex is a can of worms. - Tim Biegeleisen
@TimBiegeleisen Thank You, can You suggest one? I'm trying to not-insert too many or no dependencies in the plugin for maintanability and licensing. - Mattia Pontonio
@Tim Biegeleisen - Although it is true that regex cannot in general correctly parse all HTML since WordPress uses regex to parse the post_content, WordPress effectively restricts you to using HTML that can be parsed by regex. See function do_shortcodes_in_html_tags() in shortcodes.php. I think WordPress shouldn't do this and should use a real parser but from reading the WordPress source code the WordPress authors seem to love regex. - user8717003
I should qualify my previous comment as - with respect to shortcode processing WordPress restricts HTML content to be parseable by regex. If no shortcode is involved then the HTML can be as complex as usual. - user8717003
@B68C Thank You for your comment. For now I've used do_shortcode() so I've POST (GET really) the result to the Translator API and I've GET an HTML that is translated and "right". But I've lose the shortcodes... I'm looking to a solution to somithing like strip_shortcodes() and de_strip_shortcodes(). I think that WP uses more regex because it's really fast in Runtime but Yes, is a can of Worms.... - Mattia Pontonio

1 Answers

0
votes

It may be better for you to just register filter for post-processing results of shortcodes processing instead of trying to find and patch all possible places, they may occur.

add_filter('do_shortcode_tag', function($output, $tag, $attr, $m) {
  // Here is your code 
}, 9999, 4);

For list of arguments you can refer Wordpress sources