In order to make this work as you expect, you need to add the following lines in your constructor (depending on the requirements you have)
add_action( 'wp_ajax_myfunction', array($this, 'myfunction') );
add_action( 'wp_ajax_nopriv_myfunction', array($this, 'myfunction') );
Note that wp_ajax_nopriv_myfunction
executes for users that are not logged in. The other executes for logged in users only.
You also need to just check that in your JS, ajaxurl
returns your ajax URL.
If not, you’ll need to expose that URL for usage in your JS. You can do that by following the codex here: https://codex.wordpress.org/Function_Reference/wp_localize_script
You will need to call wp_die();
at the end of your PHP function as well.
Related Posts:
- Execute a function using ajax
- What’s the point of using WordPress’s built in admin-ajax.php?
- jQuery Plugin to use WordPress functions in AJAX request
- ajax front-end increment views on click
- WordPress Plugin Page is Loading in Admin Content Container Instead of Separate Page
- Shortcode button dosent work for all posts. Work for first post only
- Ajax : Call undefined function plugin_function() … can’t call any plugin function
- PHP script from functions php is loaded via admin-ajax to div…and the result is 0, not the desired content
- How to get specific string/value from an array? php [closed]
- Where Should i write the code for wordpress ajax voting?
- grab or load text on demand
- Cannot pass value variable to WP AJAX functions
- Update User Meta Via Ajax
- Compare Ajax Data Results
- My ajax request don´t work and return 0
- WordPress environment not loading properly
- AJAX login without a plugin does not work. when add a action to function.php
- Simple ajax request with Vanilla JS keeps given 400 Bad Request
- wordpress plugin is not activating from widget
- Theme my Login plugin, how to update fields
- Edit Yoast SEO breadcrumbs output [closed]
- What’s the best way to implement AJAX in WordPress?
- How to Create a Custom Panel and Fields in Post Page [Plugin]
- Adding Shortcode to Text Widget
- Date calculations from 2 custom fields
- How to put JQuery/Ajax inside shortcode?
- AJAX search posts and pages
- Snippets: is it better to add them in functions.php or make site-specific plugins?
- WordPress Ajax Posting from App
- Does WordPress validate inputs to all functions? (such as get_user_meta and insert_user_meta)
- Custom random quote widget breaks when used in multiple sidebars
- __callStatic method handler passed to add_action causes bug in PHP
- Using a function to change favorites listing
- Using a custom plugin to capture input data via Ajax and PHP
- Display post lists in 2nd paragraph
- How to debug error message: Cannot modify header information
- Using a post-signup hook to get user details
- How to enable specific plugin only based around shop manager role?
- How to change constants in WordPress wp-config.php programmatically?
- Restrict media upload size by format
- How to use a class from another plugin
- Ajax contact form widget plugin data not insert in database
- Override Plugin Script Fucnction in WordPress
- How to display custom sidebar in wordpress 5.5.2
- woocommerce_package_rates not fired when wordpress woocommerce accessed as non ajax
- Theme is Causing Ajax Conflicts for a plugin I cant identify it
- Calling plugin function inside custom plugin for onclick event
- Why WordPress plugin url ajax doesn’t work?
- Show function to super admin
- wordpress, search form using ajax
- creating html reusable blocks via shortcodes
- jquery not calling php function
- Search for categories
- get current date + 90 days and checking every day
- Adding a new field to the address field type in gravity forms
- AJAX in plugin wp_send_json() sending html
- Replacing a plugin function with a custom renamed function doesn’t work
- How to create an input field, and base the output on spreadsheet data? [closed]
- Facebook Messager Plugin
- 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
- Nested shortcode functions accessing variable
- Calling an attribute from a plugin shortcode
- Fatal error: Call to undefined function cmsms_theme_page_layout_scheme()
- How to call WordPress function other files
- Ajax call returning 0
- Ajax with plugins returns 0
- All sites themes functions.php have been changed
- WordPress + JavaScipt + AJAX + MySQL: insert query for form
- Ajax fail and get 504 error
- Customize permalink wordpress category id
- Ajax Search Lite: Customize Autoscroll
- Conditional attributes and logic per product category
- admin-ajax.php warning max input vars exceeded on layered pop plugins [closed]
- Disqus deleted comments are syncing with wordpress but active comments do not
- Admin style for just one plugin in dashboard
- WordPress Integration with Google Groups
- Best place to load wp_ajax_the_ajax_hook action in plugin
- jQuery.post not working within shortcode – WordPress Ajax
- Create tabs in admin options page from custom post type loop
- 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
- WP Query. Is there a maximum size?
- Display wordpress post’s in popup?
- How to remove custom post type and add category and post name
- Using AJAX to generate front end / viewer end pages
- Using color picker in plugin, does input attribute order matter?
- How to handle cookies from a WordPress plugin on a cached page?
- Grab WordPress Salt Data From URL
- wordpress file upload with ajax when site is ajaxyfi
- Post Comments with Ajax in WordPress
- WordPress get_avatar function not correct working
- AJAX search as you type? [duplicate]
- WordPress metaboxes – textfield suggestion automatically populated
- bbPress plugin: forum lists not showing in correct order? [closed]
- Which file of wordpress manage plugins functionalities?
- WordPress api call using wp-ajax returns error 400
- How to create an option page for this simple plugin
- Difference and examples of esc_attr__() and esc_attr_e()