Tutorials / 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.