Requirements:
- ACF
- Medta Text Block
- Php
- WordPress Hooks
In this tutorial I will teach you how to create an option where you can enable/disable a video from media text block.
Step 1: Go to your acf menu and create an acf option with “true/false” type, name it “Enable Autoplay” and “Enable Mute’ (note: we have included an enable mute here because in order for the autoplay to function some browsers requires it to be muted.)
Step 2: Assign the acf to a spicific post.
Step 3: Go to your functions.php file and add this code:
function custom_autoplay_block( $block_content, $block ) {
$enAutoplay = get_field('enable_autoplay', $post_id);
$enAutoplay = (!empty($enAutoplay)) ? "autoplay" : "";
$enMute = get_field('enable_mute', $post_id);
$enMute = (!empty($enMute)) ? "muted" : "";
if ( $block['blockName'] === 'core/media-text' ) {
$block_content = str_replace( '<video', '<video '.$enAutoplay.' loop '.$enMute.' playsline', $block_content );
}
return $block_content;
}
add_filter( 'render_block', 'custom_autoplay_block', 10, 2 );
note: we have the 2 variables in our code namely $enAutoplay and $enAutoplay, its value will be true or false and the value will determine if you want to enable or disable mute and autoplay.
Step 4: Go to the post the have assigned for your acf settings.
Step 5: Find the “Media & Text” Block and add the video and its text content.
Step 6: Make sure to enable autoplay and mute and then click save and you are done and can now test what you did.
That’s all for this tutorial, if you have any question, contact the author of this post. Thank you!