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" ); }
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.
render_brightcove_player
and when is it called? Is there a reason you don't enqueue it always then check in JS if the thing you want to hide is showing/exists? You're meant to enqueue scripts in a particular hook, doing it outside that hook sometimes works but it depends on when you do it rather than how you do itrender_brightcove_player
is called at many places. I am wondering if its not a good practice if I enqueue inside a php function ? Where we usually enqueue our scripts ?render_brightcove_player
is being used on the frontend in various templates, called directly? Or is it a shortcode? Or is it on a hook? In the header? Footer?