Ajax Success Message returns HTML on my homepage

Using ajax and wordpress (trying to do it right). This is new to me, but I worked with him even more. It seemed that I was having problems, so I broke my code to the most basic level, and I can’t even make it work !!! I feel like I'm just upset, and it makes me miss a simple mistake. Is there something wrong with this?

My jQuery:

$.post(
    ajaxurl, // http://localhost/mysite/wp-admin/admin-ajax.php
    {action: "post-save"},
    function(response){
      alert(response);
    }
);

My PHP:

function update_post(){
    echo json_encode(array("success" => "all systems go"), JSON_FORCE_OBJECT ); 
    exit;
}
add_action( 'wp_ajax_post-save', 'update_post' );

The end result is an alert, which means $ .post is successful (right?), But the returned variable responseis the html source of my home page ...

+5
source share
2 answers

I get it...

, , , :)

, , . , , . , , Ajax, :

function block_users()
{
    if( !current_user_can( 'delete_pages' ) ) {  
        wp_redirect( get_home_url(), 301 );
        exit;
    }
}
add_action('admin_init','block_users');

(, ), , , :)

, : , - ajax , wp_ajax , wp_ajax_nopriv.

add_action( 'wp_ajax_nopriv_action', 'function' );

add_action( 'wp_ajax_action', 'function' );

, .. , ajax.

+3

, , DOING_AJAX

function block_users()
{
    if( !current_user_can( 'delete_pages' ) && ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) ) {
        wp_redirect( get_home_url(), 301 );
        exit;
    }
}
add_action('admin_init','block_users');
+2

All Articles