Support

Account

Home Forums ACF PRO oEmbed/iFrame load options Reply To: oEmbed/iFrame load options

  • @ashikai,

    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
    }