Home  »  Website Management Tips & Services   »   Add YouTube Video Schema in WordPress without Plugin

Add YouTube Video Schema in WordPress without Plugin

Adding YouTube Video Schema to your WordPress website can boost video visibility in search results, improve SEO, and increase click-through rates (CTR). While many rely on heavy SEO plugins, you can achieve the same results manually with a lightweight approach—no plugin required. In this guide, I’ll show you how to add YouTube VideoObject schema in WordPress using simple PHP code, ensuring Google properly indexes your embedded videos.

Video of Adding YouTube Video Schema in WordPress

PHP Code to Add YouTube Video Schema in WordPress

To implement YouTube Video Schema in WordPress without a plugin, simply add the following PHP code to your single.php file, which controls the layout of your individual blog posts. This schema helps search engines recognize and index your embedded YouTube videos, increasing their chances of appearing in Google’s video search results.

If your WordPress site already includes other JSON-LD structured data, such as BlogPosting or Article schema (as we do on LELB Society), it’s important to place the YouTube VideoObject schema after those existing snippets. This ensures a proper hierarchical structure and prevents conflicts between multiple schema types, allowing search engines to process your rich snippets correctly.

<?php if (is_single()) :
global $post;
$content = $post->post_content;
$json_ld = [];

if (preg_match_all('/(?:youtube\.com\/embed\/|youtube\.com\/watch\?v=|youtu\.be\/)([a-zA-Z0-9_-]+)/', $content, $matches)) {
    foreach ($matches[1] as $index => $video_id) {
        $json_ld[] = [
            "@context" => "https://schema.org",
            "@type" => "VideoObject",
            "name" => get_the_title($post->ID) . " - Video " . ($index + 1),
            "description" => wp_strip_all_tags(get_the_excerpt($post->ID)),
            "thumbnailUrl" => "https://img.youtube.com/vi/{$video_id}/maxresdefault.jpg",
            "uploadDate" => get_the_date('c', $post->ID),
            "embedUrl" => "https://www.youtube.com/embed/{$video_id}",
            "contentUrl" => "https://www.youtube.com/watch?v={$video_id}",
            "publisher" => [
                "@type" => "Organization",
                "name" => "LELB Society",
                "logo" => ["@type" => "ImageObject", "url" => "https://lelb.net/wp-content/uploads/your-logo.png"]
            ]
        ];
    }
}

if (!empty($json_ld)) echo '<script type="application/ld+json">' . json_encode($json_ld, JSON_UNESCAPED_SLASHES) . '</script>';
endif; ?>

Explanation of the PHP Code

This above PHP code automatically generates YouTube Video Schema (VideoObject) for embedded YouTube videos in your WordPress posts (in our case, LELB Society’s English and Persian lessons). It scans the post content for YouTube video URLs, extracts the video IDs, and creates structured data in JSON-LD format. This helps Google recognize and index your videos, improving SEO. The script runs only on single posts and outputs the schema inside a <script> tag. Since it’s lightweight and works without a plugin, it ensures better performance and faster page speed.

Create a Dedicated Video Sitemap

To speed up the process of getting your embedded YouTube videos indexed by Google, you need to create a dedicated video sitemap. A video sitemap helps search engines discover and index your videos more efficiently by providing structured information about each embedded video, including its title, description, thumbnail, and URL. Without a video sitemap, Google may take longer to recognize and index your video content, reducing its visibility in search results.

Master Farsi with Engaging Video Lessons & Podcasts
Master Farsi with Engaging Video Lessons & Podcasts

To create your video sitemap, start by creating a PHP file and naming it video-sitemap.php. Copy and paste the following PHP code into this file. Once done, upload the file to the root directory of your WordPress website (e.g., /var/www/html/). This ensures that the file is accessible from your domain. After uploading, visit yourdomain.com/video-sitemap.php in your browser to verify that the sitemap is correctly generated and displays the expected XML structure.

PHP Code for Video Sitemap

<?php
header("Content-Type: application/xml; charset=utf-8");
echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
        xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">
<?php
require_once("wp-load.php");

$posts = get_posts([
    'post_type' => 'post',
    'post_status' => 'publish',
    'posts_per_page' => -1
]);

foreach ($posts as $post) {
    if (preg_match_all('/(?:youtube\.com\/embed\/|youtube\.com\/watch\?v=|youtu\.be\/)([a-zA-Z0-9_-]+)/', $post->post_content, $matches)) {
        foreach ($matches[1] as $video_id) { ?>
            <url>
                <loc><?= esc_url(get_permalink($post->ID)); ?></loc>
                <video:video>
                    <video:thumbnail_loc><?= esc_url("https://img.youtube.com/vi/{$video_id}/maxresdefault.jpg"); ?></video:thumbnail_loc>
                    <video:title><![CDATA[<?= get_the_title($post->ID); ?>]]></video:title>
                    <video:description><![CDATA[<?= wp_strip_all_tags(get_the_excerpt($post->ID)); ?>]]></video:description>
                    <video:content_loc><?= esc_url("https://www.youtube.com/watch?v={$video_id}"); ?></video:content_loc>
                    <video:player_loc><?= esc_url("https://www.youtube.com/embed/{$video_id}"); ?></video:player_loc>
                    <video:upload_date><?= get_the_date('c', $post->ID); ?></video:upload_date>
                    <video:publisher>
                        <video:publisher_name>LELB Society</video:publisher_name>
                        <video:publisher_logo>https://lelb.net/wp-content/uploads/your-logo.png</video:publisher_logo>
                    </video:publisher>
                </video:video>
            </url>
        <?php }
    }
} ?>
</urlset>

Once you confirm that the video sitemap is accessible, submit it to Google Search Console and Bing Webmaster Tools. In Google Search Console, navigate to the Sitemaps section, enter the URL of your video sitemap, and click Submit. Do the same in Bing Webmaster Tools. These steps will notify search engines about your videos, accelerating the indexing process.

How to write great rich snippets to improve your SEO ranking?
How to write great rich snippets to improve your SEO ranking?

After following this method, Google successfully identified 1,345 embedded YouTube videos on LELB Society, whereas before adding the video schema and sitemap, the number was zero. This demonstrates how combining VideoObject schema with a dedicated video sitemap can significantly enhance video indexing and improve search engine visibility for your content.

Boost Your Organic Traffic with Video Indexing

By implementing YouTube Video Schema and a dedicated video sitemap, more of your site’s content will be indexed under the “Videos” tab in Google and Bing search results. This increases your chances of appearing in video-specific search queries, making your content more discoverable to users looking for video-based information. Unlike regular search results, the video tab provides rich snippets with video thumbnails, titles, and descriptions, which can attract more clicks and engagement.

What is SEO? Best Search English Optimization Explanation with a video
What is SEO? Best Search Engine Optimization Explanation with a Video

As a result, this method can significantly boost your organic traffic, especially if your videos are relevant to high-traffic search queries. Since video content is highly engaging, users are more likely to stay on your website longer, improving dwell time and reducing bounce rate, both of which are positive SEO signals. This enhanced visibility in video search results will help establish your site as an authority in your niche while driving more traffic from search engines.

Optimizing YouTube Embeds for Speed and Performance

Embedding 1,345 YouTube videos from the LELB Society YouTube channel into WordPress posts is essential for supporting flipped learning and asynchronous learning for English and Persian students. However, directly embedding so many videos could negatively impact website speed and performance due to the heavy external JavaScript loaded from YouTube. Slow-loading pages can hurt user experience and SEO rankings, making optimization a crucial step.

10 Proven Ways to Speed Up Your WordPress Site Effectively with a video tutorial
10 Proven Ways to Speed Up Your WordPress Site Effectively with a video tutorial

To address this challenge, Dr. Mohammad Hossein Hariri Asl has been using the WP YouTube Lyte plugin, which lazy loads embedded videos. Instead of loading YouTube’s bulky scripts immediately, the videos are only fully loaded when a user interacts with them. Additionally, this plugin, developed by the creators of Autoptimize, allows local hosting of video thumbnails on the server. This reduces the number of external requests, improving page speed and ensuring a faster, more efficient learning experience for students.

Flipped Learning with Video-Based Lessons at LELB Society

At LELB Society, a bilingual academy for English and Persian learners, students have access to a vast library of over 4,500 lessons, with 1,345 video-based lessons designed to support flipped learning. This approach allows students to study video lectures and educational materials at their own pace, preparing them for deeper discussions and practice. By integrating video content into lessons, learners can enhance comprehension and reinforce their skills more effectively.

Discover the Power of Flipped Learning on LELB Society
Discover the Power of Flipped Learning on LELB Society

One of the key benefits of this system is the asynchronous learning environment that LELB Society provides. After watching the videos, students can interact with teachers and ask questions through comment forms, discussion boards, and forums. This dynamic learning model encourages self-paced education while ensuring that students receive guidance and feedback from experienced bilingual instructors. Through this innovative approach, LELB Society fosters a highly interactive and engaging learning experience for its students worldwide.

About the Author

Dr. Mohammad Hossein Hariri Asl

Dr. Mohammad Hossein Hariri Asl is an English and Persian instructor, educator, researcher, inventor, published author, blogger, SEO expert, website developer, entrepreneur, and the creator of LELB Society. He's got a PhD in TEFL (Teaching English as a Foreign Language).

Number of Posts: 4157

Leave a Comment