Wordpress Inline Js - Cant Get Working January 31, 2024 Post a Comment I'm trying to implement a simple play button for youtube video on a WordPress page. Play VideoSolution 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'); } CopyYou 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: <scripttype="text/javascript">document.body.addEventListener('load', function() { //jQuery should almost definitely be accessible in this scopevar $ = jQuery; $('#play-video').on('click', function(ev) { $('#video')[0].src += "&autoplay=1"; ev.preventDefault(); }); }); </script>CopyThis 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.<scripttype="text/javascript"src="/scripts/updatepage.js"></script><scripttype="text/javascript"><!-- updatepage(); //--></script>CopyI 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"