Wordpress Inline JS - Cant Get Working January 28, 2023 Post a Comment I'm trying to implement a simple play button for youtube video on a WordPress page. Play Video Solution 1: Almost guaranteed you are not loading jQuery in this project, and your code that you are adding relies on jQuery. In your functions.php file (in your theme folder), add the below code (be sure it is between php open / close tags, not before / after them. php open tag look like <?php and closing tag ?>): add_action('wp_enqueue_scripts', 'enqueue_my_jquery'); function enqueue_my_jquery() { wp_enqueue_script('jquery'); } Copy You may find other resources that show you how to add jQuery directly to the header.php file - do not do that. it can (and will) cause a variety of problems. Solution 2: <script type="text/javascript"> document.body.addEventListener('load', function() { //jQuery should almost definitely be accessible in this scope var $ = jQuery; $('#play-video').on('click', function(ev) { $('#video')[0].src += "&autoplay=1"; ev.preventDefault(); }); }); </script> Copy This is what I would consider a hacky way of adding javascript to Wordpress. I would recommend always having your scripts in separate files and utilizing Wordpress's PHP function wp_enqueue_script in order to ensure script dependency order. Solution 3: I found this at: https://codex.wordpress.org/Using_Javascript Under the JavaScript in Posts section: To include Javascript inside a post, you need to combine the call to the script file with the call to the JavaScript itself. <script type="text/javascript" src="/scripts/updatepage.js"></script> <script type="text/javascript"> <!-- updatepage(); //--></script> Copy I needed to put the script into a .js file and call for that file as well as the script itself. Thanks so much for the help! Share Post a Comment for "Wordpress Inline JS - Cant Get Working"
Post a Comment for "Wordpress Inline JS - Cant Get Working"