How to stop being directed to admin.php after sending request to admin-ajax.php
As suspected, WordPress was treating the button as type=”submit” fixed by doing: <button type=”button” onClick=”addProperty()”>Add property</button>
As suspected, WordPress was treating the button as type=”submit” fixed by doing: <button type=”button” onClick=”addProperty()”>Add property</button>
if the request is from an unauthenticated user, you should use wp_ajax_nopriv_(action) to call the function, which in your case is: add_action(‘wp_ajax_nopriv_cpm_add_update’, array($this,’edit_added_people’));
The JavaScript should be: <script> jQuery( document ).ready(function ( $ ) { $( “#searchform” ).on( “submit”, function ( ev ) { ev.preventDefault(); $.post( “<?php echo admin_url( ‘admin-ajax.php’ ) ?>”, { action: “wpa56343_search”, search: $( “#s” ).val() }, function ( response ) { $( “body” ).append( response ); } ); }); }); </script> And the AJAX … Read more
It can be done just following regular way to use Ajax in WordPress. https://codex.wordpress.org/AJAX_in_Plugins First register a script file and also create an AJAX Call. wp_enqueue_script( ‘meta-ajaxscript’, get_stylesheet_directory_uri() . ‘/js/ajax-init.js’, array( ‘jquery’ ), ”, true ); wp_localize_script( ‘meta-ajaxscript’, ‘ajaxMeta’, array( ‘ajaxurl’ => admin_url( ‘admin-ajax.php’ ) )); Second, Wrap your metadata with a parent element by … Read more
I’ve tested your code and it works perfectly. I’ve just declared the dependencies for the JavaScript, so it is loaded correctly after jQuery. This is how I’ve tested it: functions.php function my_resource() { wp_enqueue_script(‘my-jquery’,get_template_directory_uri().’/jqfunctions.js’, array(‘jquery’)); wp_localize_script( ‘my-jquery’, ‘myback’, array(‘ajax_url’ => admin_url( ‘admin-ajax.php’ ))); } add_action(‘wp_enqueue_scripts’, ‘my_resource’); function getsomething(){ wp_send_json_error(‘hey’); } add_action(‘wp_ajax_nopriv_getsomething’, ‘getsomething’); add_action(‘wp_ajax_getsomething’, ‘getsomething’); jqfunctions.js … Read more
The correct answer is to just do AJAX the right way. Enqueue your script and localize it to add the path to admin-ajax.php: function wpd209588(){ wp_enqueue_script( ‘wpd209588_script’, get_template_directory_uri() . ‘/js/your-script.js?ver=1.0’, array( ‘jquery’ ) ); wp_localize_script( ‘wpd209588_script’, ‘WPaAjax’, array( ‘ajaxurl’ => admin_url( ‘admin-ajax.php’ ) ) ); } add_action( ‘wp_enqueue_scripts’, ‘wpd209588_scripts’ ); In your-script.js, do your AJAX … Read more
Transients should be used for short (for varying definition of it) time caching, and they are by design not reliable. From your description it sound like you are using them in a manner they were not designed to support and therefor probably the “best” for you is to not use transients at all.
This is a simple example of rendering some items on the front-end that use AJAX to trigger changes in the back-end and update the UI from that response. There are other examples that enqueue scripts so just read up on AJAX and consider this a high level view. REGISTER AJAX/SCRIPTS/CSS add_action(‘init’, function () { // … Read more
In WP REST API v2 use the permission_callback found in the Adding Custom Endpoints Docs. <?php add_action( ‘rest_api_init’, function () { register_rest_route( ‘myplugin/v2’, ‘/author/(?P<id>\d+)’, array( ‘methods’ => ‘GET’, ‘callback’ => ‘my_awesome_func’, ‘args’ => array( ‘id’ => array( ‘validate_callback’ => ‘is_numeric’ ), ), ‘permission_callback’ => function (WP_REST_Request $request) { if ( current_user_can( ‘edit_others_posts’ ) ) { … Read more
To clarify your doubt you localized variable will contains the same values until it is overriden. In your case EMX.ajaxurl will be available throught the script. Ajax not happening at your intended event might be due to not executing the ajax function but definetly not due to EMX.ajaxurl. EMX.ajaxurl will contain the ajax url. EMX.ajaxurl … Read more