• Resolved md_hedji

    (@md_hedji)


    Hi, please tell me how can I check if the YouTube component is already included?
    I have posts in which we insert iframe YouTube through the meta field (<iframe width="500" height="281" src="https://www.youtube.com/embed/ws0npD8T4mU" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>) and if the meta field is filled, then instead of the featured image of the post I display YouTube. The problem is that if there is no YouTube in the body of the news, but it is only in the meta field, then in the amp code of the amp-YouTube page, the component is not connected and the video is not visible on the page, if the body of the news is added to YouTube then it doesn’t work. I thought just to connect to all pages of the component YouTube like this

    add_action( 'amp_post_template_head', function() {
        ?>
        <script async custom-element="amp-youtube" src="https://cdn.ampproject.org/v0/amp-youtube-0.1.js"></script>
        <?php
    } );

    but in this case, we will have a YouTube component connected 2 times, it is probably not very normal…

    My featured-image.php
    https://pastebin.com/RNBV5dsr

    maybe you can tell me how to solve the issue so that it is correct? Thanks

    Live example:
    Case 1: https://kr.informator.ua/2018/12/03/v-krivom-roge-zaderzhali-grabitelya-v-maske-tigra-i-s-toporom/amp/

    YouTube video is added through the meta field and it is in the code page, the video does not display on the page (there is no video in the content of the post)

    Case 2:
    https://kr.informator.ua/2019/04/30/na-territorii-remontno-mehanicheskogo-zavoda-v-krivom-roge-gorel-tseh/amp/

    YouTube video added through the meta field and in the news content, everything works fine.

    • This topic was modified 5 years, 10 months ago by md_hedji.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Weston Ruter

    (@westonruter)

    In Reader mode, when a YouTube embed is added to content then the YouTube script is automatically added. In the next release we may expand that to automatically include the script for amp-youtube appearing outside of the content. But in the mean time, instead of using the amp_post_template_head action you should use this filter instead:

    add_filter( 'amp_post_template_data', function( $data ) {
    	$data['amp_component_scripts'] = array_merge(
    		$data['amp_component_scripts'],
    		array(
    			'amp-youtube' => true,
    		)
    	);
    	return $data;
    } );
    Thread Starter md_hedji

    (@md_hedji)

    Thanks for your answer, if I understood correctly in this case, the YouTube component will be connected on all pages, even where there is no video, is this not bad? Google will not swear on the markup?

    Plugin Author Weston Ruter

    (@westonruter)

    Only add that filter on pages where you have amp-youtube outside the content.

    Thread Starter md_hedji

    (@md_hedji)

    Thanks, I’ll try to figure it out, I do not know how to do it yet=) Please add support for YouTube outside the content to the next version of the plugin =)

    • This reply was modified 5 years, 10 months ago by md_hedji.
    Plugin Author Weston Ruter

    (@westonruter)

    For that, please follow: https://github.com/ampproject/amp-wp/issues/2202

    Note also that if you have a compatible theme, you can also switch to Transitional/Native modes. In these, the amp-youtube can appear anywhere in the body and the script will automatically be added.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘amp-youtube component check if exist?’ is closed to new replies.