Archived topic
custom shortcode
3 replies · Started by Jens on September 15, 2022
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.
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();
} );
Thank you very much. Great support.
You're welcome