add_action(‘wp_ajax_[action name]’, myfunction) problem

There are only three explanations for why this isn’t working: First, something could be unregistering your action. That doesn’t seem very likely, though. Second, your code never runs in the ajax initialization. Third, the wp_ajax_my_function_call hook never runs. I wouldn’t worry about the possibility of the first. As far as the second, just make sure … Read more

Ajax returning correct data BUT at wrong place

You are specifying the data type returned from the AJAX call to be JSON. However, the string you’re returning (“batman begins”) is not in JSON-format. Thus, the jQuery ajax-call will fail. Try passing your data as JSON using json_encode: function locationContent() { echo json_encode( ‘batman returns’ ); die(); } This will fix your issue. Furthermore, … Read more

Class WP_Rewrite not found

I can fixed the problem including this files: require( ABSPATH . WPINC . ‘/class-wp-rewrite.php’ ); require( ABSPATH . WPINC . ‘/class-wp-user.php’ ); require( ABSPATH . WPINC . ‘/class-wp-tax-query.php’ ); require( ABSPATH . WPINC . ‘/class-wp-meta-query.php’ ); require( ABSPATH . WPINC . ‘/class-wp-term.php’ ); require( ABSPATH . WPINC . ‘/class-wp-post.php’ ); require( ABSPATH . WPINC . … Read more

How to use wp_send_json_error?

wp_send_json_error( ‘Error: Invalid data!’ ) Will echoes a JSON string: {“success”:false,”data”:”Error: Invalid data!”} The nice thing about wp_send_json_error() is, the parameter could also be a WP_Error object. As opposed to wp_send_json_success( ‘Everything okay.’ ) which echoes this JSON string: {“success”:true,”data”:”Everything okay.”} Both rely internally on wp_send_json() to echo the JSON data properly and die() afterwards. … Read more

Ajax requests with different WordPress Address and Site Address setup

Depending on how you’re mapping somewebsite.com, you can probably just define ajaxurl differently: var ajaxurl=”http://www.somewebsite.com/admin-ajax.php”; If you need something more generalizable, you could filter admin_url, sniff out whether it’s asking for admin-ajax.php, and if so, then rewrite it using home_url() or something like that. Getting cross-domain ajax to work is possible (with a proxy approach, … Read more

ajax and nonce when JavaScript is in a seperate file

You can localize your script and pass the nonce and ajax url to the script.Learn more about script localization. function wpse_206839() { // Register our script just like we would enqueue it – for WordPress references wp_register_script( ‘my-special-script’, ‘directory/my-special-script.js’, array( ‘jquery’ ), false, true ); // Create any data in PHP that we may need … Read more

How to make ajax content indexable by search engines?

Any reasons you couldn’t put an anchor link to the actual page in your tab control? Then use jQuery to make sure it returns false and doesn’t “work” for normal visitors. In fact, that’s what the jQuery Tabs control page suggests: Tabs supports loading tab content via Ajax in an unobtrusive manner. The HTML you … Read more

Ajax call in wordpress not working for subscriber user in front site

For non-admin users to be able to use an ajax function, you need to also include a second hook using wp_ajax_nopriv So right after this: add_action(‘wp_ajax_add_category_bookly’, ‘add_category_bookly_callback’); you should include this: add_action(‘wp_ajax_nopriv_add_category_bookly’, ‘add_category_bookly_callback’);

Ajax is not working for logged out users

function add_ajax_actions() { add_action( ‘wp_ajax_nopriv_get_blog_items’, ‘get_blog_items’ ); } add_action( ‘admin_init’, ‘add_ajax_actions’ ); admin_init only runs when the admin area is initialised. Logged out people can’t access wp-admin, so add_action( ‘wp_ajax_nopriv_get_blog_items’, ‘get_blog_items’ ); never runs. Move the add_action call out of that function and remove the add_ajax_actions function and it should work. Have you considered using … Read more