Support » Plugin: AMP » How to enable shortcodes & oEmbed for comments in amp pages?

  • Resolved luckyankit

    (@luckyankit)


    Hello,

    Using this code add_filter( 'comment_text', 'do_shortcode' ); in functions.php, I added shortcode support in my theme for comments. So, if I now embed a gallery in the comments, it shows up nicely.

    But the AMP plugin is stripping off that gallery and also there is no oEmbed support for comments too. Means if I embedded any youtube video in comments, that also got stripped off and the plugin not even show the urls rather.

    Can you please tell me a way to enable shortcodes and oEmbed both in comments in AMP pages for wordpress?

    • This topic was modified 7 months, 1 week ago by  luckyankit.
Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author Weston Ruter

    (@westonruter)

    Shortcodes are not handled differently in AMP. You’ll have to share more details, including URLs where the issue can be seen. What are the AMP validation errors the plugin is reporting?

    I just shared the example URL’s over Slack. Thanks

    Plugin Author Weston Ruter

    (@westonruter)

    What is the text of the comment that you are using for embedding the video and gallery? Share the sample comments.

    Plugin Author Weston Ruter

    (@westonruter)

    The raw comment text as it was input into the textarea I mean.

    luckyankit

    (@luckyankit)

    It’s the usual comment text with standard wordpress gallery shortcode:

    Comment text

    [gallery link="file" columns="2" size="medium" ids="5400,5399"]

    And to embed video, it’s just any youtube video url only, no embed code.

    https://www.youtube.com/watch?v=YhickJGHsqw

    Video automatically embeds using oEmbed in comments.

    Plugin Author Weston Ruter

    (@westonruter)

    OK, the first issue appears to be with comment handing in WordPress generally. When <amp-img … is added to the comment, it is getting converted into <amp -img …, with as space being added after amp. Because of this the sanitizer is reporting validation errors for an invalid element amp, and removing the images (and the amp-carousel).

    In particular, this code in core is the problem:

    add_filter( 'comment_text', 'force_balance_tags', 25 );

    It seems force_balance_tags is not aware of custom elements (which contain hyphens).

    The issue with the YouTube embed is different. Merely adding do_shortcode for comment_text doesn’t even work on the non-AMP version. You’ll need to add the filters that are added in WP_Embed, but you need to be careful for security (in particular for autodescovery).

    Here is a proof of concept plugin which does the embedding and fixes the force-balance-tags problem: https://gist.github.com/westonruter/3cf877833a8f94776536e3f90bb30ea2

    I do not recommend add_filter( 'comment_text', 'do_shortcode' ) because it allows any user to run shortcodes on your site.

    luckyankit

    (@luckyankit)

    Wow, thanks a lot for literally coding a plugin for solving the issue. I removed this line add_filter( 'comment_text', 'do_shortcode' ) as suggested.

    I thought this line $comment_text = $wp_embed->run_shortcode( $comment_text ) in the plugin would enable the shortcode support for comment_text but it didn’t work. Now the embed code is displaying in raw as a part of the comment content.

    So as now the gallery shortcode in the comments is not working. Would await a solution for that from you 🙂

    Youtube embeds are working now.

    Again, Thanks a lot for that.

    Plugin Author Weston Ruter

    (@westonruter)

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘How to enable shortcodes & oEmbed for comments in amp pages?’ is closed to new replies.