admin管理员组

文章数量:1287272

One of my sites is used as a personal diary with >5000 videos uploaded within the media library.

Each post uses the following shortcode, disabling pre-load and assigning a manual poster. This reduces server load and speed for the default preload='metadata', should the post contain lots of videos. The posters are manually uploaded.

[video preload="none" mp4="/wp-content/Videos/001.mp4" poster="/wp-content/Pictures/Posters/001.mp4.jpg"][/video]

This works great for posts, however; should I view videos via their tags through the archive, it displays via the default settings as found in /wp-includes/media.php which uses the standard preload='metadata'.

My question: "Is there a way to set the default 'poster' to "/wp-content/Pictures/Posters/filename.mp4.jpg" So, taking the name of the video, prepending with "/wp-content/Pictures/Posters/" and then appending with ".jpg" ?

Any help would be greatly appreciated.

One of my sites is used as a personal diary with >5000 videos uploaded within the media library.

Each post uses the following shortcode, disabling pre-load and assigning a manual poster. This reduces server load and speed for the default preload='metadata', should the post contain lots of videos. The posters are manually uploaded.

[video preload="none" mp4="/wp-content/Videos/001.mp4" poster="/wp-content/Pictures/Posters/001.mp4.jpg"][/video]

This works great for posts, however; should I view videos via their tags through the archive, it displays via the default settings as found in /wp-includes/media.php which uses the standard preload='metadata'.

My question: "Is there a way to set the default 'poster' to "/wp-content/Pictures/Posters/filename.mp4.jpg" So, taking the name of the video, prepending with "/wp-content/Pictures/Posters/" and then appending with ".jpg" ?

Any help would be greatly appreciated.

Share Improve this question asked Oct 7, 2021 at 18:18 helpless_foolhelpless_fool 111 bronze badge 1
  • Welcome to WPSE. Interesting problem. The tag archive should fire like any other and run a taxonomy query that can be filtered (pre_get_posts?) but the video output is what you wish to modify, not the query. I am not familiar with how WP outputs video but I would start with reading through the WP core to see how that happens. My guess is you will find a filter or action that allows you to modify attributes. – jdm2112 Commented Oct 7, 2021 at 19:04
Add a comment  | 

2 Answers 2

Reset to default 0

Thanks for your help. Managed to figure out a temporary solution, should anyone else have a similar query.

Temporarily edited media.php

$filename = basename( wp_get_attachment_url( $attachment_id ) );

    $default_types = wp_get_video_extensions();
    $defaults_atts = array(
        'src'      => '',
        'poster'   => "diary/wp-content/Pictures/posters/".$filename.".jpg'",
        'loop'     => '',
        'autoplay' => '',
        'preload'  => 'none',
        'width'    => 640,
        'height'   => 360,
        'class'    => 'wp-video-shortcode',
    );

I've stripped the url from the wp_get_attachment_url() attachment and plopped it directly into the 'poster' attribute.

Next, I'll have to figure out a way of writing it as a function, rather than edit media.php on every update :)

My question: "Is there a way to set the default 'poster' to "/wp-content/Pictures/Posters/filename.mp4.jpg" So, taking the name of the video, prepending with "/wp-content/Pictures/Posters/" and then appending with ".jpg" ?

To avoid editing core files, we can override the empty video shortcode poster attribute with e.g. this filter:

add_filter( 'shortcode_atts_video', function( $out, $pairs, $atts ){
        // Do nothing for non-empty poster.
        if ( ! empty( $atts['poster'] ) ) {
            return $out;
        }

        // Override empty poster.
        $filename = basename( $atts['src'] );
        if ( ! empty( $filename ) ) {
            $out['poster'] = sprintf( 
               content_url ( 'Pictures/Posters/%s.jpg' ), // <-- Adjust to your needs!
               $filename 
            ); 
        }

        return $out;
}, 10, 3 );

One could also try adding a file_exists check to make sure the default one exists.

本文标签: functionsAutomatically assign video 39poster39 value to 39filename39 for archive listing