2

After noodling around on the web to figure out how to add additional jquery features to a WP page, a question follows. After 'enqueue-ing' a js/jquery file in the theme's function.php file and then adding some js/jquery (see example below) directly onto a page via the edit text feature in the control panel to call the extended jquery feature in the enqueue-ed file, the desired feature works on the page. Is it considered bad practice to add js/jquery scripts in their tags directly to a page?

Example:

<script>
    jQuery(document).ready(function( $ ) {
        $("#someID").somePlugin(); 
    });
</script>
2

You shouldn't add any sort of code directly inside the text editor. It will most likely be stripped, and even if not, it's not a good idea to permit a code being run directly from a text editor output.

Instead, use wp_add_inline_script(). Put this piece of code inside your theme's functions.php file:

$my_plugin = '
    jQuery(document).ready(function( $ ) {
        $("#someID").somePlugin(); 
    });';
wp_add_inline_script('my-script' , $my_plugin);

This will add inline scripts after your my-script enqueued file. You have to have a JS file enqueued, and attach this inline script to it, which means you have to change my-script to whatever the ID of your enqueued script file is.

  • to be clear on your points. 1 Only use html in the page text edit window? 2 In my case, the added script (see above) in the page text edit window does not appear to be stripped since the new feature/plugin is functional when viewing the page outside of the editing mode. 3 I'll have to experiment with the procedure you proposed, Jack. Is that standard practice or a working hack? Thanks for the input. – stumpjumper64 May 18 '17 at 1:42
  • It should be stripped. If it's not, then probably some plugin/function is messing around with some hooks. Adding a code directly inside the editor is a security issue. The solution i provided is the standard way to include an inline script. You can find more information here. – Jack Johansson May 18 '17 at 1:59
  • Also if you want to filter your code only to be printed on some specific pages or post types, you can add a conditional to the code. If so, add the details to the question and i will update the answer. – Jack Johansson May 18 '17 at 2:05
1

Yes, it is typically considered bad form to do that. The only reason you might do that is if the script you are including will be used on the page you are editing and ONLY that page.

  • Ahhh..... this resonates. So for adding modified jquery features to ONE page, this is minimally acceptable but perhaps not best practice. But if one want the modified jquery features to traverse one's entire site, follow other procedures like Jack above. Do I have your point, Eric. – stumpjumper64 May 18 '17 at 1:48
  • Yep. You've got it. @stumpjumper64 – Eric B. May 18 '17 at 2:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.