The easiest way in WP to write php into JS is using wp_footer
function sse246626_js() {
if(is_single()) {
echo '<script type="text/javascript">';
echo 'jQuery(document).ready(function($) {';
echo '$("#mydiv").hide();';
echo '});';
echo '</script>';
}
}
add_action('wp_footer', 'sse246626_js');
This way gives you access to the WP functions as you need, there are probably better ways of doing it if you have lots of JS (php file as a JS file) but it gets slightly complicated as you need to load the WP functions to be used in the file.
Related Posts:
- Pass PHP variable to javascript
- Handling jQuery Component Collision
- Is dynamic forms/entries possible in Widget?
- Custom plugin giving: wp-admin/admin-ajax.php 400 (Bad Request)
- Editor not displaying dynamically after clicking on the button
- How to fetch serialized data from wordpress options
- Why is my ajax call refreshing the page?
- How do I use WordPress PHP functions in my Javascript code?
- Admin-ajax.php is dying “0” without processing function
- Get image URL from media library in input
- How to create popup same as wordpress popup feature
- Only execute jQuery function(on document ready) on the page has shortcode from plugin [duplicate]
- jQuery function didn’t work in my plugin
- Handling jQuery Component Collision
- Check if email address exists front end with AJAX in a plugin
- Best approach to fetch data from wp options to js file or php file
- Ajax contact form widget plugin data not insert in database
- jQuery for custom plugin not working with Divi theme
- How to prevent plugins from loading jQuery
- Create shortcode to echo javascript
- How to add an element right after the article using jQuery?
- How to add a dvi tag to a shortcode then change a generated text using jQuery
- Save user total active time after login in wordpress [closed]
- How do I make sure that my plugin only runs on article detail pages?
- jQuery code didn’t run on my plugin
- The Events Calendar Plugin – List View Background Colour Issue [closed]
- Ajax request sends url rather than data
- Adding a jQuery modal dialog to the admin area
- Pass PHP variable to javascript
- wc_get_template_part doesnt display the content [duplicate]
- How to properly enqueue jQuery knob on WordPress without conflict?
- update_post_meta is not working for me when I use e.preventDefault for update button
- WordPress Ajax request “Failed to load resource: the server responded with a status of 400 ()”
- WordPress Thickbox Navigation Issue in Gallery
- Dynamic input fields in WordPress Plugin
- Overwrite js code using WP_Footer
- Not displaying Woocommerce product page in lightbox
- Custom plugin giving: wp-admin/admin-ajax.php 400 (Bad Request)
- Overwriting Plugin’s Ajax callback function from theme
- Storing Array of values into wordpress options
- Javascript in plugin not working
- How to Add Jquery FullCalendar in wp plugin in admin
- WordPress ajax not working in registration
- How to add jquery to my custom post type wp plugin
- Ajax, jQuery and WordPress
- How to get site homepage from plugin?
- get_option include in my adsense
- Ajax in Plugins: returns the whole page
- Load css classes after using ajax calls
- Integrating WordPress Content into a jQuery Slider
- Jquery post responses 500 error after some time and lastly an 503 error
- How to change a user’s password programatically
- How to delete custom taxonomy terms in plugin’s uninstall.php?
- Redirect to settings page after install
- What is the correct way for a theme to support plugin UIs?
- How does WordPress determine the primary plugin PHP file?
- translation does not work with text_domain
- Custom url structure for custom template
- flush_rewrite_rules() not working on plugin activation
- Determine plugin name from within plugin_action_links filter
- Having separate plugins and themes folder for multi-site setup
- Many plugins making WordPress run slow by design?
- Add keywords meta to index.php page
- How to create ‘import dummy data’ option in custom wordpress plugin
- What are the specifics of WordPress development I need to know? [closed]
- Insert data from .sql file in wordpress plugin
- How can i get the post’s full html source by its ID?
- Force file download on plugin activation
- Redux Framework: Page freezes on typing in Editor
- How can I inject options into an select tag inside the widget form?
- How to change the name of the “edit my profile” link in the WordPress admin backend
- Jquery NoConflict Problem
- How can I add IP address to my post?
- add seperate suffix for low price and high price in woocommerce
- How to add/change (woocommerce) product tab icon
- Best way to retrieve data in custom api extended from wp-api
- How to set the default value as the saved value once a form of widget is saved?
- force jQuery to load in the head
- Where do I put the code snippets I found here or somewhere else on the web?
- Is it possible to pin a post in second position from top
- admin_notices show after load completed
- Activate Plugin Automatically After Set Time
- function post to trash problem
- How to call function in WordPress on button click?
- How can I see $post object in frontend from functions.php?
- Move plugin pages from dashboard to front-end
- Allow members to create groups on my site
- Get post content inside plugin class method
- Ubermenu 3 Nav Bar Customization
- A more elegant way to handle notices/warnings
- Redirect default login page to a custom page [duplicate]
- appointment plugin doesn’t display all appointments
- Doing action based on input from options menu
- Create New Admin Menu Section – Like how custom post type works, in a way
- “The Events Calendar” Issues in WP 3.2
- Combining JS files to one script
- What are the downsides of using bootstrap in plugin development?
- How to generate a responsive output from plugin?
- The plugin generated xxx characters of unexpected output during activation
- Display Any Field fromAdmin Panel in Frontend via Shortcode?