You can wp_enqueue_script in your shourtcode. Here are the code I fixed.
Register your JS file
function wh_enqueue() {
wp_register_script( 'wh-ajax-app', WH_PATH . '/js/write-here-ajax.js', array('jquery'), '1.0.0', true );
}
add_action( 'wp_enqueue_scripts', 'wh_enqueue' );
and enqueue in your shortcode function
function ajax_write_here(){
wp_enqueue_script( 'wh-ajax-app' ); // Enqueue your script here
$output="<div id="write_here_ajax_wrap"></div>";
return $output;
}
add_shortcode('write-here-ajax', 'ajax_write_here');
This will only load your JS file on the page has shortcode inserted.
Related Posts:
- Custom plugin giving: wp-admin/admin-ajax.php 400 (Bad Request)
- Why is my ajax call refreshing the page?
- Admin-ajax.php is dying “0” without processing function
- How to create popup same as wordpress popup feature
- Check if email address exists front end with AJAX in a plugin
- How to put JQuery/Ajax inside shortcode?
- Ajax contact form widget plugin data not insert in database
- Create shortcode to echo javascript
- How to add a dvi tag to a shortcode then change a generated text using jQuery
- Ajax request sends url rather than data
- wc_get_template_part doesnt display the content [duplicate]
- JS working when used normally but not in wordpress
- WordPress Ajax request “Failed to load resource: the server responded with a status of 400 ()”
- Custom plugin giving: wp-admin/admin-ajax.php 400 (Bad Request)
- Overwriting Plugin’s Ajax callback function from theme
- WordPress ajax not working in registration
- Ajax, jQuery and WordPress
- How to get site homepage from plugin?
- Load css classes after using ajax calls
- Jquery post responses 500 error after some time and lastly an 503 error
- Ajax response from Media Selection does not update ALL information more than once
- What’s the preferred method of writing AJAX-enabled plugins?
- How to add a shortcode button to the TinyMCE editor?
- Pass PHP variable to javascript
- Custom media upload content for inserting custom post shortcode
- Checking if an attribute exists in a shortcode
- Help making my pagination plugin better
- enqueue script only if it is not already enqueue
- Handling jQuery Component Collision
- Loading shortcode with ajax
- Enqueue style inside shortcode but its loaded at the bottom of page (before footer scripts)
- How to pass JavaScript variable to PHP in wordpress widget?
- Is dynamic forms/entries possible in Widget?
- How To Ignore a Filter On Applying Filter the Content In a Function
- AJAX Call Via Vanilla JavaScript In WordPress Plugin Development
- Shortcode display outside the div
- Editor not displaying dynamically after clicking on the button
- jQuery AJAX call not executing in Plugin
- How to fetch serialized data from wordpress options
- Execute a function using ajax
- Pass $this to function nested in another public function of the same class
- Errors while using ajax from external wordpress page
- shortcode doesn’t work
- Built in admin ajax hooks?
- Ajax call doesn’t work in frontend but it’s working in backend (when I’m logged in)
- Calling shortcode in wp_localize_script strips result
- How do I use WordPress PHP functions in my Javascript code?
- What’s the point of using WordPress’s built in admin-ajax.php?
- how to use ajax to display from database and load more in wordpress plugin
- How to add inline css/js inside a shortcode
- How to remove an Ajax action
- Get image URL from media library in input
- Ajax with jQuery UI dialog not working
- Demystifying and understanding shortcode nomenclature
- Confused about shortcode and settings values
- Running jquery script after shortcode [closed]
- WordPress Plugin Page is Loading in Admin Content Container Instead of Separate Page
- Any Short code Availble for Get Post List With Thumbnail Plugin?
- Where to call add_shortcode function in WordPress Plugin Boilerplate?
- WooCommerce get_price returning wrong price when used via ajax
- Simple ajax call not working in wordpress plugin
- Input with spaces in Shortcode attributes overwritten by defaults
- Form isn’t inserting data into database with ajax plugin
- Best approach to fetch data from wp options to js file or php file
- Allow users of my plugin to define their own shortcode rather than use mine?
- AJAX search posts and pages
- Can someone please tell me what is wrong with my plugin?
- __callStatic method handler passed to add_action causes bug in PHP
- Using a custom plugin to capture input data via Ajax and PHP
- WordPress function not being called from jQuery method
- Making a Template for a CPT created by a plugin
- creating html reusable blocks via shortcodes
- Single API call exposed via shortcode with params
- Best place for if/else piece of code related to custom plugin?
- Making sure that a plugin’s scripts and stylesheets are included for dynamicaly loaded content
- jQuery for custom plugin not working with Divi theme
- Ajax call returning 0
- Create tabs in admin options page from custom post type loop
- Creating shortcodes in plugin
- How can I inject options into an select tag inside the widget form?
- Display wordpress post’s in popup?
- How to create a custom shortcode based on the layout?
- How to prevent plugins from loading jQuery
- Using color picker in plugin, does input attribute order matter?
- Post Comments with Ajax in WordPress
- Inject HTML meta tag inside wordpress tag using add_shortcode
- how to insert data in wordpress table usnig jquery ajax
- What are the Best Practises When Using AJAX in Plugin Development?
- Redirect to another page using contact form 7? [closed]
- Namespaced shortcode?
- 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()
- Render Modula Plugin Shortcode On Ajax Request
- How can I run a custom shortcode function on a live site and only run if the viewer is a specific machine?
- Submit Form data to another page via Ajax (WordPress Way)
- Why am I unable to load scripts in head in plugin?
- How to add an element right after the article using jQuery?
- plugin shortcode not working on ajax request call
- wordpress add_action() issue in ajax call
- How to display checked posts on another page over AJAX? (like comparasion style)