Support » Plugins » Hacks » Forcing WebM media to load first in wordpress.

  • Resolved sconfig

    (@sconfig)


    Hello,

    I am currently working on my site and I want to get WebM files loading locally as a first attempt for a browser before the other fall-back files like MP4 and OGV kick in.

    Now I know the code I need is in /wp-include/media.php and if I change it to the following.

    function wp_get_video_extensions() {
    	return apply_filters( 'wp_video_extensions', array( 'webm', 'mp4', 'm4v', 'ogv', 'wmv', 'flv' ) );
    }

    And it works perfectly. However, I know the problems with “Hacking the core” and I wish to correct this by inserting this bit of code into a child theme. I was unable to find any function.php examples on referring to the wp-includes folder.

    Is there a direction you can point me in to move that media.php file elsewhere so when WP updates I’ll be fine?

    Any help is appreciated. Thank you.

    – S

    • This topic was modified 3 years ago by sconfig.
Viewing 6 replies - 1 through 6 (of 6 total)
  • ThemeSumo

    (@themesumo)

    Have you referred to the wp_video_extensions filter?

    You can use your Child Theme functions.php file for code modifications, please don’t modify your core files.

    Hope this helps.

    Moderator Steve Stern

    (@sterndata)

    Support Team Volunteer

    As ThemeSumo says, the “apply_filter” indicates you want to use add_filter to hook your own function into that code.

    I can’t say that there’s a lot of documentation at http://hookr.io/filters/wp_video_extensions/, but it seems that you can easily re-order the array of supported types there.

    Moderator bcworkz

    (@bcworkz)

    Maybe this page from the Plugin Handbook on using filters will help you. Even though it’s oriented for plugin devs, the same code can be placed in functions.php of a child theme like ThemeSumo suggests.

    Your hook callback function just needs to return an array with elements in the desired order like Steve suggests.

    sconfig

    (@sconfig)

    Thank you everyone!

    Once I figured out what they mean by “filter magic”. Is the section where they are expecting me to manipulate the array. I was good to go!

        $array = apply_filters( 'wp_video_extensions', $array );
     
        function filter_wp_video_extensions( $array ) { 
            $array = str_replace ( 'webm' , 'blank' , $array );
            $array = str_replace ( 'mp4' , 'webm' , $array );
            $array = str_replace ( 'blank' , 'mp4' , $array );
            return $array; 
        }; 
                
         add_filter( 'wp_video_extensions', 'filter_wp_video_extensions', 10, 1);

    Not a professional coder so if there was a better way for me to swap strings within the array let me know. This is my first time making a child theme for WordPress. I inspected the code in FireFox and Google Chrome and it’s loading the source videos in the order I want now.

    Which means I no longer have to hack WordPress core files. Thank you everyone!

    • This reply was modified 3 years ago by sconfig. Reason: Spelling errors
    Moderator Steve Stern

    (@sterndata)

    Support Team Volunteer

    If you think you have to hack core, you are thinking about the problem incorrectly. 🙂

    Moderator bcworkz

    (@bcworkz)

    Yup, in 5 years of hacking WP, I think I found one instance where a core hack appeared to be the only solution. Two minor releases later and a filter was added to address the issue. Legitimate core enhancement requests are always considered in such cases. At this point in it’s maturity, I doubt you’ll find such a situation any more in WP core.

    What you have is fine, it works after all. Your swapping technique is a common way to address situations where there are unknowns in the array that you don’t want to disturb. It’s the safest approach if you need to accommodate what other plugins might do to the same array.

    If you don’t care what other plugins might want to do, and since you know the content of the array, and it’s not too large, you could just return a newly defined array.

    function filter_wp_video_extensions( $array ) {
       return array( 'webm', 'mp4', 'm4v', 'ogv', 'wmv', 'flv' );
    }

    There’s frequently many ways to do something. As long as it’s not hacking core, none of them would be considered “wrong”.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Forcing WebM media to load first in wordpress.’ is closed to new replies.