fauzanjeg

JNews || Custom Meta for Video Format Post or have a Video in the Post Content

Feb 18th, 2026
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.73 KB | None | 0 0
  1. /**
  2.  * JNEWS || START CUSTOMIZATION
  3.  *
  4.  * Add video icon to meta for video format
  5.  * We use DOMDocument to safely manipulate the HTML output of the meta, and add a video icon if the post format is video or have a video in the post content.
  6.  * This way we don't have to override the entire meta rendering function, and we can keep compatibility with future updates of the theme.
  7.  * We hook into the filters for the meta output of the modules, and modify the output if
  8.  * the post is a video format or contains a video. The function will check the post format and also inspect the post content for common video indicators (blocks, shortcodes, tags, embeds).
  9.  * The code will look for the element with class "jeg_post_meta" and append a new div with class "jeg_meta_video" and a video camera icon inside it.
  10.  * You can customize the HTML inside the fragment to change the icon or add more information.
  11.  * Note: This code assumes that the meta output contains an element with class "jeg_post_meta". If the theme changes the structure of the meta output, this code may need to be updated accordingly.
  12.  */
  13.  
  14. add_filter( 'jnews_module_post_meta_1', 'jnews_custom_meta_for_video_format', 10, 3 );
  15. add_filter( 'jnews_module_post_meta_2', 'jnews_custom_meta_for_video_format', 10, 3 );
  16. add_filter( 'jnews_module_post_meta_3', 'jnews_custom_meta_for_video_format', 10, 3 );
  17.  
  18. /**
  19.  * Check whether a post or content contains a video.
  20.  * Accepts a `WP_Post`, `WP_Query`, post ID, or a raw content string.
  21.  * Returns bool.
  22.  */
  23. function jnews_post_has_video( $post_or_content ) {
  24.     // resolve content string
  25.     $content = '';
  26.  
  27.     if ( $post_or_content instanceof WP_Query ) {
  28.         $post_obj = $post_or_content->post ?: ( ! empty( $post_or_content->posts ) ? $post_or_content->posts[0] : null );
  29.         $content = $post_obj ? $post_obj->post_content : '';
  30.     } elseif ( $post_or_content instanceof WP_Post ) {
  31.         $content = isset( $post_or_content->post_content ) ? $post_or_content->post_content : '';
  32.     } elseif ( is_numeric( $post_or_content ) ) {
  33.         $content = get_post_field( 'post_content', (int) $post_or_content );
  34.     } else {
  35.         $content = (string) $post_or_content;
  36.     }
  37.  
  38.     if ( ! $content ) {
  39.         return false;
  40.     }
  41.  
  42.     // 1) Gutenberg core/video block
  43.     if ( function_exists( 'has_block' ) && has_block( 'core/video', $content ) ) {
  44.         return true;
  45.     }
  46.  
  47.     // 2) Serialized block comment or wp-block video markup
  48.     if ( strpos( $content, '<!-- wp:video' ) !== false || strpos( $content, 'wp-block-video' ) !== false ) {
  49.         return true;
  50.     }
  51.  
  52.     // 3) native HTML <video> tag
  53.     if ( preg_match( '#<video\b#i', $content ) ) {
  54.         return true;
  55.     }
  56.  
  57.     // 4) video shortcode [video]
  58.     if ( function_exists( 'has_shortcode' ) && has_shortcode( $content, 'video' ) ) {
  59.         return true;
  60.     }
  61.  
  62.     // 5) common iframe embeds (YouTube, Vimeo, Dailymotion, Wistia)
  63.     if ( preg_match( '#<iframe[^>]+(youtube|youtube-nocookie|vimeo|dailymotion|wistia|player.vimeo)#i', $content ) ) {
  64.         return true;
  65.     }
  66.  
  67.     return false;
  68. }
  69.  
  70. /**
  71.  * Add custom meta for video format
  72.  *
  73.  * @param string $output
  74.  * @param \WP_Post|\WP_Query $post
  75.  * @param \JNews\Module\ModuleViewAbstract $instance
  76.  *
  77.  * @return string
  78.  */
  79. function jnews_custom_meta_for_video_format( $output, $post, $instance ) {
  80.     // normalize $post into a WP_Post object when possible
  81.     $post_obj = null;
  82.     if ( $post instanceof WP_Query ) {
  83.         $post_obj = $post->post ?: ( ! empty( $post->posts ) ? $post->posts[0] : null );
  84.     } elseif ( is_object( $post ) && isset( $post->ID ) ) {
  85.         $post_obj = $post;
  86.     } elseif ( is_numeric( $post ) ) {
  87.         $post_obj = get_post( (int) $post );
  88.     }
  89.  
  90.     if ( ! $post_obj ) {
  91.         return $output;
  92.     }
  93.  
  94.     // use WP post format check OR content inspection helper
  95.     if ( has_post_format( 'video', $post_obj->ID ) || jnews_post_has_video( $post_obj ) ) {
  96.         libxml_use_internal_errors( true );
  97.         $doc = new DOMDocument();
  98.  
  99.         // Load as HTML fragment without adding <html>/<body>
  100.         $doc->loadHTML( '<?xml encoding="utf-8" ?>' . $output, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD );
  101.  
  102.         $xpath = new DOMXPath( $doc );
  103.         // find first element with class "jeg_post_meta"
  104.         $nodes = $xpath->query( "//*[contains(concat(' ', normalize-space(@class), ' '), ' jeg_post_meta ')]" );
  105.  
  106.         if ( $nodes->length ) {
  107.             $meta = $nodes->item( 0 );
  108.  
  109.             // create a fragment for arbitrary HTML (safe insertion)
  110.             $fragment = $doc->createDocumentFragment();
  111.             // change the HTML below to whatever element you want to insert
  112.             $fragment->appendXML( '<div class="jeg_meta_video"><i class="fa fa-video-camera"></i></div>' );
  113.  
  114.             $meta->appendChild( $fragment );
  115.         }
  116.  
  117.         // export modified HTML (will return the fragment/document)
  118.         $output = $doc->saveHTML();
  119.         libxml_clear_errors();
  120.     }
  121.  
  122.     return $output;
  123. }
  124.  
  125. /**
  126.  * JNEWS || END CUSTOMIZATION
  127.  */
  128.  
Advertisement
Add Comment
Please, Sign In to add comment