Site logo

[Resolved] custom shortcode

Home Forums Support [Resolved] custom shortcode

Home Forums Support custom shortcode

  • This topic has 3 replies, 2 voices, and was last updated 3 years ago by David.
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #2343723
    Jens

    I have a custom post type “News” that contains three custom fields (video_id, video_start, video_end). These are used to display video snippets. This can be done with a hook and the attached code.

    <figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
    <iframe frameborder="0" scrolling="no" marginheight="0" marginwidth="0" type="text/html" allowfullscreen="allowfullscreen" src="https://www.youtube.com/embed/<?php echo get_post_meta( get_the_ID(), 'video_id', true ); ?>?autoplay=1&fs=0&controls=0&iv_load_policy=3&showinfo=0&rel=0&cc_load_policy=0&start=<?php echo get_post_meta( get_the_ID(), 'video_start', true ); ?>&end=<?php echo get_post_meta( get_the_ID(), 'video_ende', true ); ?>"></iframe></div></figure>

    How can I achieve the same with a custom shortcode [video_block_cpt_news] ? I need this shortcode to use with GenerateBlocks.

    Thank you for your suggestions.

    #2343987
    David
    Staff
    Customer Support

    Hi there,

    this doc explains creating a shortcode that uses object buffering:

    https://docs.generatepress.com/article/creating-a-shortcode/

    For your needs you should be able to do this:

    add_shortcode( 'video_block_cpt_news', function() {
        ob_start();
        
        ?>
        <figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio">
            <div class="wp-block-embed__wrapper">
                <iframe frameborder="0" scrolling="no" marginheight="0" marginwidth="0" type="text/html" allowfullscreen="allowfullscreen" src="https://www.youtube.com/embed/<?php echo get_post_meta( get_the_ID(), 'video_id', true ); ?>?autoplay=1&fs=0&controls=0&iv_load_policy=3&showinfo=0&rel=0&cc_load_policy=0&start=<?php echo get_post_meta( get_the_ID(), 'video_start', true ); ?>&end=<?php echo get_post_meta( get_the_ID(), 'video_ende', true ); ?>"></iframe>
            </div>
        </figure>
        <?php
    
        return ob_get_clean();
    } );
    #2344071
    Jens

    Thank you very much. Great support.

    #2344609
    David
    Staff
    Customer Support

    You’re welcome

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.