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
- Woocommerce attatch files and custom input meta to cart items and then to order
- Ajax response from Media Selection does not update ALL information more than once
- is_plugin_active function doesn’t exist
- How to call a plugin function from index.php
- How can I make my custom shortcode work in a Custom HTML Widget?
- How Does WordPress Uninclude/Deactivate A Plugin?
- Ajax call doesn’t work in frontend but it’s working in backend (when I’m logged in)
- How can I add a custom meta value on file upload?
- Add a Custom Permalink option in the Permalink Admin Screen?
- publish_post plugin hook doesn’t always pass $post->post_content
- Let user subscribe to specific categories
- How to cancel WordPress’ action/filter when using OOP with anonymous callback
- What is the best way to mimic a search query within a plugin
- Programatically download a plugin
- Do action only on certain front end pages?
- Archive – same title for the first two posts
- Creating plugin using simple_html_dom parser?
- Create a navbar filter that filters by a custom field
- wp.media javascript issue with on select
- Creating shortcodes in plugin
- how to insert data in wordpress table usnig jquery ajax
- Redirect to another page using contact form 7? [closed]
- Separate database for a wordpress plugin
- remove all submenus from plugin
- How to prevent plugins from sniffing/stealing other plugins’ options?
- Hook for page Request?
- Is there a function to list all uploaded images? How can I add one?
- How to render a template file using shortcode inside a plugin
- Separate jQuery for Individual Comments?
- Does WordPress check for updates of a plugin via plugins root folder name?
- Delete data from custom table when deleting a post
- Need to add/remove group of options and display them as rows
- How to deactivate my plugin upon deactivation of NextGen
- Paypal Framework
- Problems with jQuery and is_admin()
- Fatal error “Call to undefined function is_plugin_active” each time the plugin is activated
- Custom Plugin: How to Include Install Buttons of other 3rd Party Plugins?
- How to make WooCommerce multiple filter with custom meta fields
- jquery plugin won’t fire when hamburger class element clicked
- Modify Plugin PHP Class in Child Theme – Correct Method
- How to create and use Custom hooks
- Custom Function for SEO by Yoast plugin
- Custom feed parameters / Template overriding
- Shortcode return is printing a 1 afterward
- wordpress select multiple options and illegal string offset ‘timeslot’ [closed]
- Creating Event Builder Plugin [closed]
- How to get custom setting from get_option and pass it in getEntityRecords in gutenberg block?
- When to store store plugin options as a single database record?
- WordPress hack code issue, help required