Integrating YouTube Videos in WordPress Plugin

Posted on 16th June 2023

WordPress is a popular content management system (CMS) that helps you easily create and manage a website or blog.

One of the great things about WordPress is that it’s easy to extend its functionality with plugins. There are plugins available for just about anything you can think of, from social media integration to eCommerce.

In this article, we’re going to focus on how to integrate YouTube videos into a WordPress plugin.

Why Integrate YouTube Videos Into WordPress?

YouTube is the world’s second largest search engine, so it’s no surprise that many website owners want to take advantage of this by adding videos to their site.

There are a few ways to add YouTube videos to WordPress, but integrating them into a plugin has a few advantages:

  • It’s easier to manage all your videos in one place.
  • You can add extra features and functionality to your videos, such as video transcripts.
  • You can use a plugin to display your videos in a more attractive way, such as in a video gallery or slider.

How to Integrate YouTube Videos Into a WordPress Plugin

There are a few steps involved in integrating YouTube videos into a WordPress plugin:

  1. Enqueueing the necessary files
  2. Adding a shortcode
  3. Registering a new post type
  4. Adding a video metabox

We’ll go into more detail on each of these steps below.

1. Enqueueing the Necessary Files

The first thing you need to do is enqueue the necessary files. In your plugin’s main file, add the following code:


function my_plugin_scripts() {
wp_enqueue_script( 'youtube-api', 'https://apis.google.com/js/api.js', array(), '1.0', true );
wp_enqueue_script( 'my-plugin-script', plugins_url( '/js/my-plugin-script.js', __FILE__ ), array( 'youtube-api' ), '1.0', true );
}
add_action( 'wp_enqueue_scripts', 'my_plugin_scripts' );

This code will enqueue the necessary files from the Google API and your plugin.

2. Adding a Shortcode

Next, you need to add a shortcode. A shortcode is a WordPress-specific code that lets you do complex things with very little code. In your plugin’s main file, add the following code:


function my_plugin_shortcode( $atts ) {
extract( shortcode_atts(
array(
'id' => '',
), $atts )
);
return '<div id="yt-' . $id . '"></div>';
}
add_shortcode( 'my_plugin', 'my_plugin_shortcode' );

This shortcode will output a div with an ID that you can use to target your YouTube video.

3. Registering a New Post Type

The next step is to register a new post type. This is what will store your YouTube videos. In your plugin’s main file, add the following code:


function my_plugin_post_type() {
register_post_type( 'my_plugin',
array(
'labels' => array(
'name' => __( 'My Plugin Videos' ),
'singular_name' => __( 'My Plugin Video' )
),
'public' => true,
'has_archive' => true,
'supports' => array( 'title', 'editor', 'custom-fields' )
)
);
}
add_action( 'init', 'my_plugin_post_type' );

This code will register a new post type called “My Plugin Videos”.

4. Adding a Video Metabox

The final step is to add a video metabox. This is what will let you add your YouTube video to your post type. In your plugin’s main file, add the following code:


function my_plugin_add_meta_box() {
add_meta_box(
'my_plugin_meta_box',
__( 'My Plugin Meta Box', 'my-plugin' ),
'my_plugin_meta_box_callback',
'my_plugin',
'normal',
'high'
);
}
add_action( 'add_meta_boxes', 'my_plugin_add_meta_box' );

function my_plugin_meta_box_callback( $post ) {
wp_nonce_field( 'my_plugin_meta_box', 'my_plugin_meta_box_nonce' );
$value = get_post_meta( $post->ID, '_my_plugin_video_id', true );
echo '<label for="my_plugin_video_id">' . __( 'Video ID', 'my-plugin' ) . '</label> ';
echo '<input type="text" id="my_plugin_video_id" name="my_plugin_video_id" value="' . esc_attr( $value ) . '" size="25" />';
}

function my_plugin_save_meta_box_data( $post_id ) {
if ( ! isset( $_POST['my_plugin_meta_box_nonce'] ) ) {
return;
}
if ( ! wp_verify_nonce( $_POST['my_plugin_meta_box_nonce'], 'my_plugin_meta_box' ) ) {
return;
}
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_page', $post_id ) ) {
return;
}
} else {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
}
if ( ! isset( $_POST['my_plugin_video_id'] ) ) {
return;
}
$my_data = sanitize_text_field( $_POST['my_plugin_video_id'] );
update_post_meta( $post_id, '_my_plugin_video_id', $my_data );
}
add_action( 'save_post', 'my_plugin_save_meta_box_data' );

This code will add a metabox to your “My Plugin Videos” post type where you can enter a YouTube video ID.

Conclusion

Integrating YouTube videos into a WordPress plugin is a great way to add videos to your website or blog. It’s easy to do and it gives you more control over your videos.

If you want to learn more about WordPress plugin development, check out our other articles on the topic.

In order to integrate YouTube videos in your WordPress plugin, you will need to first install and activate the YouTube Integration Plugin. Once you have activated the plugin, you will need to visit the ‘Settings’ page and enter your YouTube API key.

You can then use the [youtube] shortcode to embed YouTube videos in your posts and pages. The shortcode accepts a number of parameters, which you can use to customize the video player. For example, you can use the ‘autoplay’ parameter to automatically play the video when the page is loaded.

You can also use the WordPress editor to embed YouTube videos in your posts and pages. Simply click on the ‘Add Media’ button and then click on the ‘Insert Video’ tab. From here, you can search for a video on YouTube and then insert it into your post or page.

If you want to display a video gallery on your WordPress site, you can use the [youtube_gallery] shortcode. This shortcode will fetch the latest videos from a specified YouTube channel and display them in a grid layout. You can use the ‘columns’ parameter to specify the number of columns in the grid.

If you want to give your users the ability to upload videos to your WordPress site, you can use the [youtube_uploader] shortcode. This shortcode will display a file upload form that your users can use to upload their videos. Once the videos have been uploaded, they will be automatically added to a specified YouTube channel.

The YouTube Integration Plugin for WordPress is a great way to add YouTube videos to your WordPress site. With this plugin, you can easily embed YouTube videos in your posts and pages, create video galleries, and even allow your users to upload videos to your site.