Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * JNEWS || START CUSTOMIZATION
- *
- * Add video icon to meta for video format
- * 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.
- * This way we don't have to override the entire meta rendering function, and we can keep compatibility with future updates of the theme.
- * We hook into the filters for the meta output of the modules, and modify the output if
- * 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).
- * 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.
- * You can customize the HTML inside the fragment to change the icon or add more information.
- * 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.
- */
- add_filter( 'jnews_module_post_meta_1', 'jnews_custom_meta_for_video_format', 10, 3 );
- add_filter( 'jnews_module_post_meta_2', 'jnews_custom_meta_for_video_format', 10, 3 );
- add_filter( 'jnews_module_post_meta_3', 'jnews_custom_meta_for_video_format', 10, 3 );
- /**
- * Check whether a post or content contains a video.
- * Accepts a `WP_Post`, `WP_Query`, post ID, or a raw content string.
- * Returns bool.
- */
- function jnews_post_has_video( $post_or_content ) {
- // resolve content string
- $content = '';
- if ( $post_or_content instanceof WP_Query ) {
- $post_obj = $post_or_content->post ?: ( ! empty( $post_or_content->posts ) ? $post_or_content->posts[0] : null );
- $content = $post_obj ? $post_obj->post_content : '';
- } elseif ( $post_or_content instanceof WP_Post ) {
- $content = isset( $post_or_content->post_content ) ? $post_or_content->post_content : '';
- } elseif ( is_numeric( $post_or_content ) ) {
- $content = get_post_field( 'post_content', (int) $post_or_content );
- } else {
- $content = (string) $post_or_content;
- }
- if ( ! $content ) {
- return false;
- }
- // 1) Gutenberg core/video block
- if ( function_exists( 'has_block' ) && has_block( 'core/video', $content ) ) {
- return true;
- }
- // 2) Serialized block comment or wp-block video markup
- if ( strpos( $content, '<!-- wp:video' ) !== false || strpos( $content, 'wp-block-video' ) !== false ) {
- return true;
- }
- // 3) native HTML <video> tag
- if ( preg_match( '#<video\b#i', $content ) ) {
- return true;
- }
- // 4) video shortcode [video]
- if ( function_exists( 'has_shortcode' ) && has_shortcode( $content, 'video' ) ) {
- return true;
- }
- // 5) common iframe embeds (YouTube, Vimeo, Dailymotion, Wistia)
- if ( preg_match( '#<iframe[^>]+(youtube|youtube-nocookie|vimeo|dailymotion|wistia|player.vimeo)#i', $content ) ) {
- return true;
- }
- return false;
- }
- /**
- * Add custom meta for video format
- *
- * @param string $output
- * @param \WP_Post|\WP_Query $post
- * @param \JNews\Module\ModuleViewAbstract $instance
- *
- * @return string
- */
- function jnews_custom_meta_for_video_format( $output, $post, $instance ) {
- // normalize $post into a WP_Post object when possible
- $post_obj = null;
- if ( $post instanceof WP_Query ) {
- $post_obj = $post->post ?: ( ! empty( $post->posts ) ? $post->posts[0] : null );
- } elseif ( is_object( $post ) && isset( $post->ID ) ) {
- $post_obj = $post;
- } elseif ( is_numeric( $post ) ) {
- $post_obj = get_post( (int) $post );
- }
- if ( ! $post_obj ) {
- return $output;
- }
- // use WP post format check OR content inspection helper
- if ( has_post_format( 'video', $post_obj->ID ) || jnews_post_has_video( $post_obj ) ) {
- libxml_use_internal_errors( true );
- $doc = new DOMDocument();
- // Load as HTML fragment without adding <html>/<body>
- $doc->loadHTML( '<?xml encoding="utf-8" ?>' . $output, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD );
- $xpath = new DOMXPath( $doc );
- // find first element with class "jeg_post_meta"
- $nodes = $xpath->query( "//*[contains(concat(' ', normalize-space(@class), ' '), ' jeg_post_meta ')]" );
- if ( $nodes->length ) {
- $meta = $nodes->item( 0 );
- // create a fragment for arbitrary HTML (safe insertion)
- $fragment = $doc->createDocumentFragment();
- // change the HTML below to whatever element you want to insert
- $fragment->appendXML( '<div class="jeg_meta_video"><i class="fa fa-video-camera"></i></div>' );
- $meta->appendChild( $fragment );
- }
- // export modified HTML (will return the fragment/document)
- $output = $doc->saveHTML();
- libxml_clear_errors();
- }
- return $output;
- }
- /**
- * JNEWS || END CUSTOMIZATION
- */
Advertisement
Add Comment
Please, Sign In to add comment