I don’t know if it is the ‘best’ place, but get_header is early enough to still enqueue scripts:
function at_set_header () {
global $post, $has_the shortcode;
if ( is_object( $post ) && has_shortcode( $post->post_content, 'shortcode_name') ) {
$has_the_shortcode = true;
wp_enqueue_script( 'my-ajax-handle', FAU_PLUGIN_URL . 'js/ajax.js', array( 'jquery' ) );
wp_localize_script( 'my-ajax-handle', 'the_ajax_script', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
}
}
add_action( 'get_header', 'at_set_header');
The $has_the_shortcode global might be usefull to decide about other resources at later stages.
Related Posts:
- What’s the preferred method of writing AJAX-enabled plugins?
- Help with 4.6. Attachment response object in JSON API plugin
- Loading shortcode with ajax
- AJAX Call Via Vanilla JavaScript In WordPress Plugin Development
- Custom plugin giving: wp-admin/admin-ajax.php 400 (Bad Request)
- How to save Clicks of a download link button while it doesn’t matter if we refresh the page or again login/logout
- ‘Lazy loading’ list of posts with pagination
- How to include admin-ajax when loading external javascript
- Ajax call doesn’t work in frontend but it’s working in backend (when I’m logged in)
- Why is my ajax call refreshing the page?
- Admin-ajax.php is dying “0” without processing function
- How to remove an Ajax action
- How to create popup same as wordpress popup feature
- Get Plugin Information from Multiple Sites Externally
- How to add pagination to wpbakery grid?
- ajax front-end increment views on click
- Check if email address exists front end with AJAX in a plugin
- Why plugin ajax response is -1?
- WordPress Ajax Callback
- plugin ajax to external php file always return null
- WooCommerce get_price returning wrong price when used via ajax
- When to use action hooks and plugins
- Form isn’t inserting data into database with ajax plugin
- How to Create a Custom Panel and Fields in Post Page [Plugin]
- AJAX search posts and pages
- wordpress, search form using ajax
- jquery not calling php function
- Best place for if/else piece of code related to custom plugin?
- WordPress + JavaScipt + AJAX + MySQL: insert query for form
- WordPress Integration with Google Groups
- jQuery.post not working within shortcode – WordPress Ajax
- How to load wp_editor via AJAX
- How can I inject options into an select tag inside the widget form?
- Init plugin again after ajax call finish
- Display wordpress post’s in popup?
- Using color picker in plugin, does input attribute order matter?
- How to handle cookies from a WordPress plugin on a cached page?
- wordpress file upload with ajax when site is ajaxyfi
- AJAX search as you type? [duplicate]
- WordPress api call using wp-ajax returns error 400
- AJAX call – failling to call do_action
- how to insert data in wordpress table usnig jquery ajax
- Using wpdb without loading all plug-ins via wp-load.php
- Admin AJAX doesn’t work in plugin admin page – Even though code is copied verbatim from WordPress Codex
- AJAX fileupload – TypeError: not a function ajaxSubmit()
- Call plugin with php function
- Is it safe to use admin-ajax.php in the frontend?
- Create WordPress posts from JSON array using plugin in admin
- Adding inside wp-plugin jQuery script that receives JSON-formatted data, generated by php-function inside this plugin
- Ajax save data to database on document ready, no data being saved
- Using WordPress to build membership Page
- How to get specific string/value from an array? php [closed]
- Proper way to handle admin-ajax calls
- Using custom php file for ajax url inside plugin
- Ajax object not defined – JS file is in plugin folder
- How to get CPT category checkbox list and show post of selected(multiply) checkboxes via ajax?
- WordPress subfolder installation AJAX Problem
- Call Ajax URL in Plugin
- WordPress API functions not working at AJAX functions.php call
- wp_ajax action responds with 0
- Calling a class method instantiated by ajax call in wordpress [closed]
- Why AJAX response 0!
- Loading by Ajax a plugin that also uses Ajax
- AJAX call inside plugin class is not getting to it’s response function
- How do I reinit WordPress plugins dynamically using jS?
- How can I adjust my function so the page editor still loads in the backend?
- Error datatable whit ajax
- Trouble with AJAX using GET
- Calling PHP function with AJAX
- Adding LOAD MORE on a page
- Ajax call to php function doesn’t work PHP code
- admin-ajax.php returning 0
- Update User Meta Via Ajax
- Compare Ajax Data Results
- User Following System
- Frontend AJAX Request causes Error: ‘Call to undefined function add_action’
- Ajax Load More on Hierarchical Categories
- cURL do not mimic click
- admin-ajax.php not working properly on subdomains
- My ajax request don´t work and return 0
- Plugin with AJAX on subdomains causes 404 in console
- Creating new option (add_option) from AJAX – Settings API
- How can I use wp_ajax_response for front-end error reporting?
- How to limit each front-end user to view just his own uploaded files on Amazon S3?
- Not getting result using ajax on wordpress
- AJAX login without a plugin does not work. when add a action to function.php
- AJAX doesn’t pass variables to the php function in the plugin
- ajax request not returning the result
- display user input using ajax in wordpress
- Plugin constructor called multiple times
- WordPress plugin ajax post parameter
- Trouble After Update, Removing Plugin
- Response from Php File to Ajax is not getting sent
- Plugin Admin Page Ajax-Admin call returning 0, URL set correctly. Implemented localized scripts but did not fix it
- Load css classes after using ajax calls
- Ajax button “Load more” is not loading correct language version posts for logged out users
- How do I call an action hook into wp_ajax hook callback function
- Select posts from list and add them in a new list
- Ajax response from Media Selection does not update ALL information more than once
- Simple ajax request with Vanilla JS keeps given 400 Bad Request