Snippets Collections
<head>
    <meta charset="<?php bloginfo('charset'); ?>">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="preconnect" href="https://fonts.googleapis.com/" />
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap" rel="stylesheet" />
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
    <link rel="stylesheet" href="https://unpkg.com/tippy.js@6/dist/tippy.css" />
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/swiper@11.0.5/swiper-bundle.min.css" />
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jquery-range@1.0.0/jquery.range.min.css" />
    <link rel="stylesheet" href="https://unpkg.com/intl-tel-input@18.2.1/build/css/intlTelInput.css" />
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/flatpickr@4.6.13/flatpickr.min.css" />
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/lazyload@17.8.5/dist/lazyload.min.js" />
    <link rel="stylesheet" href="<?php echo get_stylesheet_directory_uri() . '/assets/css/tvh-custom.css?ver=' . filemtime(get_stylesheet_directory() . '/assets/css/tvh-custom.css') ?>" />
    <script defer src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
    <script defer src="https://unpkg.com/@popperjs/core@2"></script>
    <script defer src="https://cdn.jsdelivr.net/npm/swiper@11.0.5/swiper-bundle.min.js"></script>
    <script defer src="https://cdn.jsdelivr.net/npm/jquery-range@1.0.0/jquery.range-min.js"></script>
    <script defer src="https://unpkg.com/intl-tel-input@18.2.1/build/js/intlTelInput.min.js"></script>
    <script defer src="https://cdn.jsdelivr.net/npm/flatpickr@4.6.13/flatpickr.min.js"></script>
    <script defer src="https://cdn.jsdelivr.net/npm/vanilla-lazyload@17.8.5/dist/lazyload.min.js"></script>
    <link rel="profile" href="https://gmpg.org/xfn/11" />
    
    <?php if (is_page_template('template-home.php')): ?>
        <link rel="preload stylesheet" href="<?php echo get_stylesheet_directory_uri() . '/assets/css/home.css?ver=' . filemtime(get_stylesheet_directory() . '/assets/css/home.css') ?>" as="style">
        <link rel="preload" fetchpriority="high" href="<?php echo get_template_directory_uri() . '/assets/img/home-hero.webp'; ?>" type="image/webp">
    <?php endif; ?>
    
    <?php if (is_page_template('template-about.php')): ?>
        <link rel="preload stylesheet" href="<?php echo get_stylesheet_directory_uri() . '/assets/css/about-us.css?ver=' . filemtime(get_stylesheet_directory() . '/assets/css/about-us.css') ?>" as="style">
    <?php endif; ?>
    
    <?php if (is_page_template('template-services.php')): ?>
        <link rel="preload stylesheet" href="<?php echo get_stylesheet_directory_uri() . '/assets/css/about-us.css?ver=' . filemtime(get_stylesheet_directory() . '/assets/css/about-us.css') ?>" as="style">
    <?php endif; ?>
</head>
<?php 

class Elementor_Custom_Video_Widget extends \Elementor\Widget_Base {

    public function get_name() {
        return 'custom_video_widget';
    }

    public function get_title() {
        return __('Custom Video Widget', 'plugin-name');
    }

    public function get_icon() {
        return 'eicon-video-camera';
    }

    public function get_categories() {
        return ['basic'];
    }

    protected function _register_controls() {
        $this->start_controls_section(
            'content_section',
            [
                'label' => __('Content', 'plugin-name'),
                'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
            ]
        );

        $this->add_control(
            'source',
            [
                'label' => __('Source', 'plugin-name'),
                'type' => \Elementor\Controls_Manager::SELECT,
                'options' => [
                    'self_hosted' => __('Self Hosted', 'plugin-name'),
                    'external' => __('External URL', 'plugin-name'),
                ],
                'default' => 'self_hosted',
            ]
        );

        $this->add_control(
            'video_url',
            [
                'label' => __('Choose Video File', 'plugin-name'),
                'type' => \Elementor\Controls_Manager::MEDIA,
                'media_type' => 'video',
                'condition' => [
                    'source' => 'self_hosted',
                ],
            ]
        );

        $this->add_control(
            'autoplay',
            [
                'label' => __('Autoplay', 'plugin-name'),
                'type' => \Elementor\Controls_Manager::SWITCHER,
                'label_on' => __('Yes', 'plugin-name'),
                'label_off' => __('No', 'plugin-name'),
                'return_value' => 'yes',
                'default' => 'yes', // Set default to 'yes'
            ]
        );

        $this->add_control(
            'play_on_mobile',
            [
                'label' => __('Play on Mobile', 'plugin-name'),
                'type' => \Elementor\Controls_Manager::SWITCHER,
                'label_on' => __('Yes', 'plugin-name'),
                'label_off' => __('No', 'plugin-name'),
                'return_value' => 'yes',
                'default' => 'yes',
            ]
        );

        $this->add_control(
            'mute',
            [
                'label' => __('Mute', 'plugin-name'),
                'type' => \Elementor\Controls_Manager::SWITCHER,
                'label_on' => __('Yes', 'plugin-name'),
                'label_off' => __('No', 'plugin-name'),
                'return_value' => 'yes',
                'default' => 'no',
            ]
        );

        $this->add_control(
            'loop',
            [
                'label' => __('Loop', 'plugin-name'),
                'type' => \Elementor\Controls_Manager::SWITCHER,
                'label_on' => __('Yes', 'plugin-name'),
                'label_off' => __('No', 'plugin-name'),
                'return_value' => 'yes',
                'default' => 'no',
            ]
        );

        $this->add_control(
            'player_controls',
            [
                'label' => __('Player Controls', 'plugin-name'),
                'type' => \Elementor\Controls_Manager::SWITCHER,
                'label_on' => __('Show', 'plugin-name'),
                'label_off' => __('Hide', 'plugin-name'),
                'return_value' => 'yes',
                'default' => 'yes',
            ]
        );

        $this->add_control(
            'download_button',
            [
                'label' => __('Download Button', 'plugin-name'),
                'type' => \Elementor\Controls_Manager::SWITCHER,
                'label_on' => __('Show', 'plugin-name'),
                'label_off' => __('Hide', 'plugin-name'),
                'return_value' => 'yes',
                'default' => 'no',
            ]
        );

        $this->add_control(
            'poster',
            [
                'label' => __('Poster', 'plugin-name'),
                'type' => \Elementor\Controls_Manager::MEDIA,
                'media_type' => 'image',
            ]
        );

        $this->add_control(
            'show_play_button',
            [
                'label' => __('Show Play/Pause Button', 'plugin-name'),
                'type' => \Elementor\Controls_Manager::SWITCHER,
                'label_on' => __('Show', 'plugin-name'),
                'label_off' => __('Hide', 'plugin-name'),
                'return_value' => 'yes',
                'default' => 'yes',
            ]
        );

        $this->end_controls_section();
    }

    protected function render() {
        $settings = $this->get_settings_for_display();

        // Video HTML
        echo '<video id="custom-video" src="' . $settings['video_url']['url'] . '" ' . ($settings['autoplay'] === 'yes' ? 'autoplay' : '') . ' ' . ($settings['mute'] === 'yes' ? 'muted' : '') . ' ' . ($settings['loop'] === 'yes' ? 'loop' : '') . ' ' . ($settings['player_controls'] === 'yes' ? 'controls' : '') . ' poster="' . $settings['poster']['url'] . '"></video>';

        // Play/Pause Button
        if ($settings['show_play_button'] === 'yes') {
            // Default to pause icon if autoplay is enabled
            $icon_class = $settings['autoplay'] === 'yes' ? 'fa-pause' : 'fa-play';
            echo '<button id="custom-play-pause" class="play-button"><i class="fas ' . $icon_class . '"></i></button>';
        }

        // JavaScript for Play/Pause Button
        echo '<script>
                document.addEventListener("DOMContentLoaded", function() {
                    var video = document.getElementById("custom-video");
                    var playPauseButton = document.getElementById("custom-play-pause");
                    var icon = playPauseButton.querySelector("i");

                    // Play video if autoplay is enabled and video is muted (required by some browsers)
                    video.addEventListener("loadedmetadata", function() {
                        if (video.hasAttribute("autoplay")) {
                            video.play().then(function() {
                                icon.classList.remove("fa-play");
                                icon.classList.add("fa-pause");
                            }).catch(function(error) {
                                console.log("Autoplay failed: ", error);
                                icon.classList.remove("fa-pause");
                                icon.classList.add("fa-play");
                            });
                        }
                    });

                    playPauseButton.addEventListener("click", function() {
                        if (video.paused) {
                            video.play();
                            icon.classList.remove("fa-play");
                            icon.classList.add("fa-pause");
                        } else {
                            video.pause();
                            icon.classList.remove("fa-pause");
                            icon.classList.add("fa-play");
                        }
                    });
                });
            </script>';
    }
}


//-------------------------------------------------END-------------------------------------//
Dưới đây là đoạn đăng ký widget 
function register_custom_widget($widgets_manager)
{
    // Custom video widget
    require_once(__DIR__ . '/widgets/custom-video.php');
    $widgets_manager->register(new \Elementor_Custom_Video_Widget());
}
add_action('elementor/widgets/register', 'register_custom_widget');
star

Fri Sep 13 2024 04:09:17 GMT+0000 (Coordinated Universal Time)

#php #elementor #wigetcustom #videocustom
star

Thu Aug 15 2024 08:44:16 GMT+0000 (Coordinated Universal Time)

#php #elementor #wigetcustom #videocustom

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension