fauzanjeg

JNews || Custom Meta for Video Format Post

Feb 16th, 2026
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.36 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.
  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 format is video.
  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', 'custom_meta_for_video_format', 10, 3 );
  15. add_filter( 'jnews_module_post_meta_2', 'custom_meta_for_video_format', 10, 3 );
  16. add_filter( 'jnews_module_post_meta_3', 'custom_meta_for_video_format', 10, 3 );
  17.  
  18. /**
  19.  * Add custom meta for video format
  20.  *
  21.  * @param $output
  22.  * @param $post
  23.  * @param $instance
  24.  *
  25.  * @return string
  26.  */
  27. function custom_meta_for_video_format( $output, $post, $instance ) {
  28.     if ( has_post_format( 'video', $post->ID ) ) {
  29.         libxml_use_internal_errors( true );
  30.         $doc = new DOMDocument();
  31.  
  32.         // Load as HTML fragment without adding <html>/<body>
  33.         $doc->loadHTML( '<?xml encoding="utf-8" ?>' . $output, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD );
  34.  
  35.         $xpath = new DOMXPath( $doc );
  36.         // find first element with class "jeg_post_meta"
  37.         $nodes = $xpath->query( "//*[contains(concat(' ', normalize-space(@class), ' '), ' jeg_post_meta ')]" );
  38.  
  39.         if ( $nodes->length ) {
  40.             $meta = $nodes->item( 0 );
  41.  
  42.             // create a fragment for arbitrary HTML (safe insertion)
  43.             $fragment = $doc->createDocumentFragment();
  44.             // change the HTML below to whatever element you want to insert
  45.             $fragment->appendXML( '<div class="jeg_meta_video"><i class="fa fa-video-camera"></i></div>' );
  46.  
  47.             $meta->appendChild( $fragment );
  48.         }
  49.  
  50.         // export modified HTML (will return the fragment/document)
  51.         $output = $doc->saveHTML();
  52.         libxml_clear_errors();
  53.     }
  54.  
  55.     return $output;
  56. }
  57.  
  58. /**
  59.  * JNEWS || END CUSTOMIZATION
  60.  */
Advertisement
Add Comment
Please, Sign In to add comment