Short code: How to enable embedding of the default WordPress gallery in the comments?

In fact, if you allow all kinds of short codes to be used in the comments, you do not know what effects you get. It could even become a security problem if you have powerful installed short codes (maybe even unknowingly, as a feature you do not use). Therefore, the trick is to selectively allow certain short codes. First, add a filter to get_comment_text (other than comment_text This will also affect your comment feed).

add_filter (& # 39; get_comment_text & # 39 ;, & # 39; wpse334485_filter_shortcodes & # 39 ;, 10.3);

Now we must make sure that this filter is applied. only The filter of the gallery. That is, we must remove all the short codes of the comment, except the short code of the gallery. Here we go:

function wpse334485_filter_shortcodes ($ comment_text, $ comment, $ args) {
$ comment_text = strip_shortcodes ($ comment_text);
return do_shortcode ($ comment_text);

The above code will delete all the short codes, so it is not complete. Fortunately the strip_shortcodes The function has a filter that allows you to influence which labels are removed. Here it is:

add_filter (& # 39; strip_shortcodes_tagnames & # 39 ;, & # 39; wpse334485_allow_gallery_shortcode & # 39 ;, 10.2);
function wpse334485_allow_gallery_shortcode ($ tags_to_remove, $ comment_text) {
return matrix (& # 39;& # 39;);

Note that I did not try this code, so it may be necessary to debug.