简体   繁体   中英

How to use wordpress function wp_enqueue_script() in php?

I have a php function as shown below. The following function is being used at many places.

function render_brightcove_player($active_feed, $poster_image = false)
{
    $poster = '';
    if ($poster_image) {
        $poster = 'poster=' . esc_url($poster_image);
    }
    ?>
    <div class="hsc-video" onclick="hscLogo()">
        <div class="hsc-video__inner">
            <script src="//players.brightcove.net/1242843915001/SJ3Tc5kb_default/index.min.js"></script>
        </div>
    </div>
    <?php
    wp_enqueue_script(                                      
    'miscellaneous-scripts',
    HSC_TEMPLATE_URL . "/assets/js/miscellaneous.js"
    );
}

In the php code, I have added wordpress function wp_enqueue_script() . Inside miscellaneous.js file I am using:

function hscLogo() {
    document.getElementsByClassName("hsc-tv-logo")[0].style.display = "none";
}

I am wondering if that is the right way to use wp_enqueue_script() function in php. Do I need to place wp_enqueue_script() somewhere else ?

This is the first time I am using wp_enqueue_script in wordpress. Here is the tree structure of javascript folders/files.

在此处输入图片说明

Is your php code in wordpress? And if it is called by any wordpress template or plugin, you can use wp_enqueue_script in your php file. You can check the following link to study wp_enqueue_script. https://developer.wordpress.org/reference/functions/wp_enqueue_script/

wp_enqueue_script(                                      
    'miscellaneous-scripts',
    HSC_TEMPLATE_URL . "/assets/js/miscellaneous.js",
    array(),
    '1.0.0', //Version
    true // Load in footer.
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM