1. Home
  2. Docs
  3. Threme Setup
  4. WordPress Setup
  5. Add Videos

Add Videos

  1. Creating a custom video post type:

    • Add the code to functions.php file & show in the video and category fields.
function custom_post_type_video() {

 $labels = array(

  'name'                  => _x( 'Videos', 'Post Type General Name', 'text_domain' ),

  'singular_name'         => _x( 'Video', 'Post Type Singular Name', 'text_domain' ),

  'menu_name'             => __( 'Videos', 'text_domain' ),

  'name_admin_bar'        => __( 'Video', 'text_domain' ),

  'archives'              => __( 'Video Archives', 'text_domain' ),

  'attributes'            => __( 'Video Attributes', 'text_domain' ),

  'parent_item_colon'     => __( 'Parent Video:', 'text_domain' ),

  'all_items'             => __( 'All Videos', 'text_domain' ),

  'add_new_item'          => __( 'Add New Video', 'text_domain' ),

  'add_new'               => __( 'Add New', 'text_domain' ),

  'new_item'              => __( 'New Video', 'text_domain' ),

  'edit_item'             => __( 'Edit Video', 'text_domain' ),

  'update_item'           => __( 'Update Video', 'text_domain' ),

  'view_item'             => __( 'View Video', 'text_domain' ),

  'view_items'            => __( 'View Videos', 'text_domain' ),

  'search_items'          => __( 'Search Video', 'text_domain' ),

  'not_found'             => __( 'Not found', 'text_domain' ),

  'not_found_in_trash'    => __( 'Not found in Trash', 'text_domain' ),

  'featured_image'        => __( 'Featured Image', 'text_domain' ),

  'set_featured_image'    => __( 'Set featured image', 'text_domain' ),

  'remove_featured_image' => __( 'Remove featured image', 'text_domain' ),

  'use_featured_image'    => __( 'Use as featured image', 'text_domain' ),

  'insert_into_item'      => __( 'Insert into video', 'text_domain' ),

  'uploaded_to_this_item' => __( 'Uploaded to this video', 'text_domain' ),

  'items_list'            => __( 'Videos list', 'text_domain' ),

  'items_list_navigation' => __( 'Videos list navigation', 'text_domain' ),

  'filter_items_list'     => __( 'Filter videos list', 'text_domain' ),

);

$args = array(

 'label'                 => __( 'Video', 'text_domain' ),

 'description'           => __( 'Post Type Description', 'text_domain' ),

 'labels'                => $labels,

 'supports'          => array( 'title', 'editor', 'thumbnail','excerpt' ),

 'menu_icon'             =>'dashicons-format-video',

 'hierarchical'      => true,

 'public'                => true,

 'show_ui'            => true,

 'show_in_menu'          => true,

 'menu_position'         => 5,

 'show_in_admin_bar'     => true,

 'show_in_nav_menus'     => true,

 'can_export'            => true,

 'has_archive'           => true,

 'exclude_from_search'   => false,

 'publicly_queryable'    => true,

 'capability_type'       => 'post',

 'show_in_rest'          => true,

);

register_post_type( 'videos', $args );

 
register_taxonomy(

 'videos_category',

 'videos',

   array(

    'label' => __( 'Categories' ),

    'rewrite' => array( 'slug' => 'videos_category' ),

    'hierarchical' => true,

    'taxonomies' => array('videos_category'),

    'show_in_rest' => true,

   )

 );

}

add_action( 'init', 'custom_post_type_video', 0 );

 

2.Add this code in the function.php file and get the video API.

url:- https://wp-demo-news.apps.peanutsquare.com/wp-json/custom/v1/videos?_embed

(A).get the video url.

function add_video_url_to_rest_response() {

 register_rest_field(

  'videos',

  'video_url',

   array(

    'get_callback' => 'get_video_url',

    'schema'       => null,

  )

 );

}

add_action('rest_api_init', 'add_video_url_to_rest_response');

function get_video_url($object, $field_name, $request) {

  return get_post_meta($object['id'], 'short_video', true);

}

(B).add this code in access the video URL

add_action('rest_api_init', function () {

  register_rest_route('acf/v1', '/video/(?P<id>\d+)', array(

  'methods' => 'GET',

  'callback' => 'get_acf_video_link',

 ));

});

function get_acf_video_link($data) {

  $post_id = $data['id'];

  $video_link = get_field('short_video', $post_id);

  if (empty($video_link)) {

  return new WP_Error('no_video', 'Invalid post ID or no video link found', array('status' => 404));

}

return rest_ensure_response($video_link);

}

3.Add this code in functions.php file view the breaking-news Categories id.

url:- https://wp-demo-news.apps.peanutsquare.com/wp-json/custom/v1/breaking-news?_embed

function register_breaking_news_endpoint() {

  register_rest_route('custom/v1', '/breaking-news/', array(

  'methods' => 'GET',

  'callback' => 'get_breaking_news_posts',

 ));

}

add_action('rest_api_init', 'register_breaking_news_endpoint');

function get_breaking_news_posts($request) {

  $args = array(

  'category_name' => 'breaking-news',

  'post_type' => 'post',

  'posts_per_page' => -1,

 );

 $query = new WP_Query($args);

 $posts = [];

 if ($query->have_posts()) {

  while ($query->have_posts()) {

   $query->the_post();

   $categories = get_the_category();

   $category_names = array();

    foreach ($categories as $category) {

      $category_names[] = $category->name;

    }

$posts[] = array(

 'id' => get_the_ID(),

 'title' => get_the_title(),

 'slug' => get_post_field('post_name', get_the_ID()),

 'content' => get_the_content(),

 'excerpt' => get_the_excerpt(),

 'date' => get_the_date(),

 'link' => get_permalink(),

 'image' => get_the_post_thumbnail_url(get_the_ID(), 'full'),

 'categories' => $category_names,

 );

}

wp_reset_postdata();

}

if (empty($posts)) {

  return new WP_Error('no_posts', 'No breaking news found', array('status' => 404));

}

return rest_ensure_response($posts);

}

function custom_videos_endpoint( $data ) {

  $videos = get_posts( array(

  'post_type'      => 'videos',

  'posts_per_page' => -1,

) );

$formatted_videos = array();

foreach ( $videos as $video ) {

  $video_categories = wp_get_post_terms( $video->ID, 'videos_category', array( 'fields' => 'names' ) );

  $video_link = get_field( 'short_video', $video->ID );

  $video_data = array(

    'id'            => $video->ID,

    'title'         => $video->post_title,

    'image'         => get_the_post_thumbnail_url( $video->ID, 'full' ),

    'category'    => $video_categories,

    'video'    => $video_link,

  );

  $formatted_videos[] = $video_data;

}

  return $formatted_videos;

}

// Register custom REST API endpoint for videos

function register_custom_videos_endpoint() {

   register_rest_route( 'custom/v1', '/videos', array(

   'methods'  => 'GET',

   'callback' => 'custom_videos_endpoint',

 ) );

}

add_action( 'rest_api_init', 'register_custom_videos_endpoint' );

4.Add Breaking News Api key

  • Add this code in functions.php file to creating a custom API.
  • API:- /wp-json/custom/v1/breaking-news
function register_custom_videos_endpoint() {

  register_rest_route( 'custom/v1', '/videos', array(

  'methods'  => 'GET',

  'callback' => 'custom_videos_endpoint',

 ));

}

add_action( 'rest_api_init', 'register_custom_videos_endpoint' );

 

Notes :- Add this all code in the functions.php file.