I have created a plugin that adds a dashboard widget to word press. I have been told that this plugin needs to enqueue all css and javascript using the enqueue methods.

I can easily move the css/js files to be enqueued easily, but most of the script is dynamically generated by pulling data from the database. I also have some Ajax code that is also dynamically generated and included in wp_footer.

How can i queue javascript strings? I cant serialize it as it needs to be uncached.

Caching will cause this to work for one page and then not work correctly for any other pages.

  • have you looked into wp_localize_script to add the data to the enqueueing? – majick Aug 23 '16 at 9:52
  • this will include it across the board though. For the dashboard widget in particular i only want the code included on this page. If i put it in the admin enqueue it will throw errors on all other pages as the getelementbyid function in the js wont be able to find the node, or even worse it might find an element with the same name and screw things up. – Dan Hastings Aug 23 '16 at 10:22
  • 1
    You can enqueue only for the main dash: if ( "index.php" == $GLOBALS['pagenow'] ) { # enqueue } or even wrap your JS in if ( null !== getElementById(.. ).. – Samuel Elh Aug 23 '16 at 16:37

Your Answer

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

Browse other questions tagged or ask your own question.