cbc allow auto import

Description

Filter that can prevent all automatic imports from running.

Parameters

$allow
(boolean)(required) Allow automatic imports to run (true) or prevent them (false).

Default:true

Examples

Prevent all automatic imports from running on your website.

add_filter( 'cbc_allow_auto_import', '__return_false' );

cbc allow video import

Description

Using this filter you can prevent a certain video from being imported into a WordPress post.

Parameters

$allow
(boolean)(required) Allow video to be imported (true) or prevent the post creation (false).

Default:true
$video
(array)(optional) Array containing the video details retrieved from YouTube.

Default:array
$post_type
(string)(optional) The post type that will be created for this video.

Default:None
$theme_import
(array)(optional) If importing is done for compatible WP theme, the array will contain the theme details.

Default:None

Examples

Prevent videos shorter than 10 minutes to be imported.

/**
 * Prevent videos shorter than 10 minutes to be imported
 * 
 * @param boolean $allow
 * @param array $video
 * @param string $post_type
 * @param array $theme_import
 * @return boolean
 */
function prevent_video_import( $allow, $video, $post_type, $theme_import ){
	$ten_minutes = 10 * MINUTE_IN_SECONDS;
	$allow = ( $ten_minutes > $video['duration'] );	
	return $allow;
}
add_filter( 'cbc_allow_video_import', 'prevent_video_import', 10, 4 );

cbc before post insert

Description

Action triggered before a new video post is inserted into the database.

Parameters

$video
(array)(optional) Video details array

Default:None
$theme_import
(array)(optional) If WP theme is compatible with the plugin and the user chose to import video as theme post the variable will hold the theme details.

Default:array

cbc post insert

Description

Action that runs right after a new video post was successfully created.

Parameters

$post_id
(integer)(optional) The post id of the newly created post

Default:None
$video
(array)(optional) Video details retrieved from YouTube

Default:None
$theme_import
(array)(optional) If WP theme is compatible with the plugin and the user chose to import video as theme post the variable will hold the theme details.

Default:None
$post_type
(string)(optional) The post type of the newly created post.

Default:None

Examples

Store video likes and views in custom fields.

/**
 * Add post views and stats in custom fields
 * @param integer $post
 * @param array $video
 * @param array $theme_import
 * @param string $post_type
 */
function add_video_stats( $post_id, $video, $theme_import, $post_type ){
	// update views
	update_post_meta( $post_id, 'views', $video['stats']['views'] );
	// update likes
	update_post_meta( $post_id, 'likes', $video['stats']['likes'] );
}
add_action( 'cbc_post_insert', 'add_video_stats', 10, 4 );

cbc skipped posts category

Description

Triggers when trying to import an already existing video post. In this case, the plugin will skip importing the video in order to prevent duplicate posts. The action is useful if you want to assign extra categories to existing videos.

Parameters

$posts
(array)(optional) Existing posts that were found when trying to import videos.

Default:None
$taxonomy
(string)(optional) The category taxonomy

Default:None
$category
(string)(optional) The category that should be added if missing

Default:None

Examples

For video posts that were skipped because they were duplicates, add any extra categories that might be set by imports other than the original one.

cbc video status check

Description

This action is conditioned by option “Check video status after import” from plugin Settings. If the option is on and after checking the video status YouTube API returns a successful result, this action is triggered.

Parameters

$post
(WP_Object)(optional) The current WP_Post object

Default:None
$video
(array)(optional) The video details array returned by YouTube API

Default:None

Examples

Refresh video post views and likes count.

/**
 * Update post views and stats
 * @param WP_Post $post
 * @param array $video
 */
function refresh_video_stats( $post, $video ){
	// update views
	update_post_meta( $post->ID, 'views', $video['stats']['views'] );
	// update likes
	update_post_meta( $post->ID, 'likes', $video['stats']['likes'] );
}
add_action( 'cbc_video_status_check', 'refresh_video_stats', 10, 2 );

cbc unpublish video

Description

This action is conditioned by option “Check video status after import” from plugin Settings. If the option is on and after checking the video status YouTube API returns anything other than a public video response (ie. video was deleted or embed privacy settings were change to disallow embeds), the plugin will set post status to pending and trigger this action.

Parameters

$post
(WP_Object)(optional) The current WP_Post object

Default:None
$video
(array)(optional) The video details array returned by YouTube API

Default:None

Examples

Send an e-mail to blog admin each time a video gets unpublished because YouTube API returns the video as non-public or deleted.

/**
 * E-mail administrator every time a video gets unpublished
 * @param WP_Post $post
 * @param array $video
 */
function video_unpublish_manager( $post, $video ){
	wp_mail( 
		get_bloginfo( 'admin_email' ), 
		__('Unpublished video'), 
		sprintf( __('Post ID %d was unpublished.'), $post->ID ) 
	);
}
add_action( 'cbc_unpublish_video', 'video_unpublish_manager', 10, 2 );

The automatic import explained

Importing YouTube videos automatically from a given source (YouTube channel, playlist or user uploads) is an easy task with WordPress YouTube Hub. For more details on how to create automatically importing playlists please see Automatic video import.

Setting up the import options

The first step that you should take before starting your automatic importing is to set the import options. These options can be found in plugin Settings page, tab Import options.

The import frequency

The import frequency (named Automatic import in options) determines how many videos will be imported at once and how often. You can select to import from 1 up to 50 videos every 1 minute up to 24 hours.

The selection made here will dictate how much stress you will put on your system. Basically, when a new video is imported as a WordPress post, the plugin will ask WordPress to do the following, in this particular order:

  1. Insert the post details into the database;
  2. Import the featured image from YouTube (which is quite resource consuming) into your website Media Gallery and assign the featured image to this new post. This means that WordPress will actually create the image and then insert the details into the database;
  3. create a new WordPress category and assign the newly created post to it;
  4. create the YouTube tags that should be imported and assign them to the new post.
    1. The process is pretty intensive so you should test different settings for frequency and number of videos until you find the optimal combination.

      Enabling conditional imports

      Before explaining this feature you must understand how the automatic import process works. Basically, this process is triggered by your website visitors. This means that when the timer expires, the plugin will wait for the first visitor that visits your website and it will trigger the import.

      For example, if you set automatic imports to happen every 5 minutes, after 5 minutes have passed, the first visitor that sees your website will trigger the import. Once this happens, the plugin will import the videos it has in queue and will wait for another 5 minutes. After the next 5 minutes have passed, it will repeat the process.

      As you have probably noticed, this is somehow unreliable because there’s no guarantee that your website will receive a visitor every 5 minutes so you won’t reach the optimal import frequency unless you own a high traffic website.
      Even on a high traffic website there can be problems, like duplicated videos caused by concurrent imports that can happen when two or more visitors visit your website at the same time and trigger the automatic import process.

      To solve both issues mentioned above you can use conditional importing. When this feature is enabled, automatic imports will be made only if, at the given time interval, a hit is received on a certain link on your website (you will see the link after you enable this option). This link can be used to set a cron job on your server that will open your website every X minutes and will ensure that automatic import will run consistently. Besides this, it also eliminates concurrent imports because the hit will be received only once.

      Legacy automatic import

      When doing automatic imports that are triggered by your website visitors, once the timer is expired and an import can be made, the plugin will use WordPress’s shutdown action to import the videos in queue. Until all importing is done, the page will show as loading in your visitor’s browser, which can be a bit frustrating for them, especially if you’re importing large quantities of videos.

      To eliminate this, we implemented a different functionality. When videos must be imported, the plugin will make a remote call to your website, having some variables set on the link that will trigger the import. The maximum execution time of this remote call is 1 second, which means that your page won’t be displayed as loading for long and the plugin will do all its work in background. This is the default functionality of the plugin but if you experience problems, you can go back to the old way of functioning by enabling this option.

      Conclusion

      To conclude all this, always set the number of videos imported at once at a realistic number, tuned with your server performance. Don’t go straight for 50 videos every minute; try to find the numbers that work for you. If you don’t know how to do this, go for maximum 10-15 videos every 5 minutes, it should work on most websites.

      For better performance, choose conditional importing and use server cron job to trigger the imports. While this is an advanced feature since not everyone knows how to set a cron job, you can always ask your hosting provider for help or search for information on the internet.

Storing video statistics in custom fields

In some cases, when importing a video from YouTube you might also want to import its views, likes and dislikes. By default, the plugin won’t store these values in a custom field but you can easily do this by hooking to one of the many actions implemented by the plugin.

Let’s assume that for every video that we import from YouTube we want to store its views, likes and dislikes into custom fields named just like that: views, likes, dislikes. This scenario applies mainly for videos imported as part of the theme compatibility feature so we’ll make sure that the custom fields get set only for posts that are created for the theme.

To do this we need to hook to plugin action cbc_post_insert which can pass up to 3 parameters to the callback: the post ID, the video details and the theme import details. This action is triggered by the plugin right after importing a new video and successfully creating a new post in your WordPress website.

The code should be placed into your WP theme functions.php file. We recommend using a child theme for this in order to make sure you don’t lose your modifications when you update the theme.

Following the same scenario you can save any information in any custom field you need. All you need to do is hook to the cbc_post_insert action and save your information on the currently imported post.

Third party compatibility

By default the plugin is compatible with several WordPress themes and can import videos from YouTube into posts that those themes can handle and embed videos. This is done by automatically filling post custom fields that are required by the themes to embed videos.

In case your theme isn’t in the compatible themes list (you can find the list in plugin menu, page Info & Help) you can follow this tutorial and make your theme compatible with the plugin. All the code needed for theme compatibility must be placed into your theme functions.php file.

We recommend using a child theme for this purpose otherwise you risk losing your changed when you update your theme.

Declare your theme as compatible (required)

First step is to tell the plugin that your theme is compatible with the plugin. To do this, use filter cbc_compatibility like this:

This tells the plugin that your theme is compatible with the plugin and that it should start listening to other filters.

Set the post type (required)

The next step is to tell the plugin what post type you want to use to import YouTube videos. To do this, use filter cbc_compatibility_post_type and return your post type name.

For example, if your theme uses regular post type to display videos, put this in your theme functions.php, right after the code from above:

Set post type category taxonomy

Next, you need to tell the plugin which taxonomy to use when importing videos. This is used for both setting existing categories and for importing categories retrieved from YouTube. If, at the previous step, you used post type post and you want to use default taxonomy category, you can skip this step.

To set the category taxonomy you can use filter cbc_compatibility_taxonomy and return the taxonomy name that you want to use. For our example, we will set this to category, even if it’s not necessary.

Set post type tag taxonomy

To import YouTube tags along with the videos you must set the tag taxonomy where these tags will be created. To do this, use filter cbc_compatibility_tag_taxonomy. Example below uses regular post post_tag taxonomy.

Autofill video URL into post custom field

If your theme needs the video URL to be provided into a custom field in order to embed the video, you must set the field using filter cbc_compatibility_url_meta. This will fill the video URL into the custom field that you specify and will allow your theme to embed the video.

Example below assumes that the theme needs a custom field named youtube_video_url to be filled with the video URL.

Autofill embed code into custom field

If instead of video URL your theme needs the video embed code to be placed into a custom field in order to be able to display the video, you can use filter cbc_compatibility_embed_meta.

Example below assumes that your theme needs custom field named youtube_embed_code to be filled with video embed code automatically.

Autofill video image into custom field

Some themes may require that you provide an URL to video image. To do this, use filter cbc_compatibility_image_meta and give the plugin the custom field name that you want the plugin to fill with the video image.

Example below assumes that image must be stored in custom field youtube_thumbnail.

Set post format

By default the plugin will set post format for imported videos to video. If you need a different post format for videos imported for your WordPress theme you can use filter cbc_compatibility_post_format.

Set theme name

When using theme compatibility, the plugin will display the theme name in various places. By default, the actual theme name of your theme (from styles.css) will be displayed. If you want to change the name displayed in plugin pages to anything else, use filter cbc_compatibility_theme_name.

Set theme URL

The plugin will also display links to your theme in various places in plugin. By default, the URL is taken from your theme details (from styles.css) but if you want to change that simply use filter cbc_compatibility_theme_url.


Example 1

First example assumes the following:

  1. import as post type post
  2. use default post category taxonomy
  3. use default post_tag taxonomy
  4. import video URL in custom field video_url

You may have noticed that we didn’t specify any category or tag taxonomies. The reason we didn’t do that is because for regular post type post, unless you specify a different category and/or tag taxonomy, the plugin will use default category and post_tag taxonomies.

Example 2

In this example we assume the following:

  1. import as post type portfolio
  2. use category taxonomy categories registered for post type portfolio
  3. use taxonomy category technologies registered for post type portfolio
  4. import video URL in custom field video_url