Home › Forums › ACF PRO › oEmbed/iFrame load options › Reply To: oEmbed/iFrame load options
This isn’t really an ACF issue, having a bunch of Embedded videos will tank any page regardless of how they’re implemented.
You should really be looking into loading them via AJAX, so when you first load the page there are NO videos loaded at all until someone clicks on an element.
Heres some pseudo code that should give a general idea of how it would work.
in template:
<?php
$args = array
(
'post_type' => 'team_members'
);
$team_member_query = new WP_Query($args);
while ($team_member_query->have_posts()): $team_member_query->the_post(); ?>
<div class="team-member-ajax-trigger" data-id="<?php echo get_the_id(); ?>"></div>
<?php endwhile; ?>
In JS:
var ajaxurl = 'http://' + window.location.host + '/wp-admin/admin-ajax.php';
$('.team-member-ajax-trigger').click(function ()
{
var id = $(this).data('id');
$.ajax
({
url: ajaxurl,
data:
{
action: 'ajax_make_team_member_video',
id: id
},
method: "POST",
error: function (data)
{
console.error("FAILURE on get Ajax Items", data);
}
}).done(function (html)
{
console.log(html);
if(html === 'no_id' || html === 'no_video')
{
//Error out or something
}
else
{
//do stuff here to make a video appear.
//html should contain your oembedded video
}
});
});
In functions.php:
add_action( 'wp_ajax_nopriv_ajax_make_team_member_video', 'ajax_make_team_member_video' );
add_action( 'wp_ajax_ajax_make_team_member_video','ajax_make_team_member_video' );
function ajax_make_team_member_video()
{
if(!isset($_POST['id']) || !$_POST['id'] || !is_numeric($_POST['id'])):
echo 'no_id';
wp_die();
endif;
$id = $_POST['id'];
$video = get_field('oembed_video',$id);
if(!$video):
echo 'no_video';
else:
echo '<div class="ajax-loaded-video">'.$video.'</div>';
endif;
wp_die(); //always end with wp_die when using ajax
}
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
ACF wouldn’t be so widely used in WordPress if it didn’t have some pretty amazing capabilities. In this article, we look at a few of the features we’ll discuss during “7 things you didn’t know you could do with ACF” at #WPEDecode later this month. https://t.co/5lnsTxp81j pic.twitter.com/Yf0ThPG1QG
— Advanced Custom Fields (@wp_acf) March 16, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.