I am using Sender.net for email subscription lists. This site give me some information to add user's emails to subscription list. I want to use WordPress ajax to for this. but it return error 400 bad request. My codes is:

File ajax-news-script.js:


    // Perform AJAX send news on form submit
    $('form#fnews').on('submit', function(e){

        $('form#fnews #fsubmit').text(ajax_news_object.loadingmessage);
            type: 'POST',
            dataType: 'json',
            url: ajax_news_object.ajaxurl,
            data: { 
                'action': 'ajaxnews', //calls wp_ajax_nopriv_ajaxnews
                'email': $('form#fnews #femail').val(),  
                'security': $('form#fnews #security').val() },

            success: function(data){
                $('form#fnews #fsubmit').text(data.message);

And in ajax-news.php file:


add_action( 'init', 'ajax_news_init' );

function ajax_news_init() {
        global $rf_data;
        wp_register_script( 'ajax-news-script', get_stylesheet_directory_uri() . '/js/ajax-news-script.js', array('jquery'), '1.0', true );
        wp_enqueue_script( 'ajax-news-script' );

        wp_localize_script( 'ajax-news-script', 'ajax_news_object', array(
            'ajaxurl' => admin_url( 'admin-ajax.php' ),
            'loadingmessage' => __( 'لطفا صبر کنید ...' )

        add_action( 'wp_ajax_nopriv_ajax_news', 'ajax_news' );
        add_action( 'wp_ajax_ajax_news', 'ajax_news' );

// Check if users input information is valid
    function ajax_news() {

        global $rf_data;

        // First check the nonce, if it fails the function will break
        check_ajax_referer( 'ajax-news-nonce', 'security' );

    $info = array();
    $info['email'] = $_POST['email'];

    $api_key = "my api key";
    $list_id = my list id;
    $url = 'https://app.sender.net/api'; 

    $data = array(
        "method" => "listSubscribe",
        "params" => array(
            "api_key" => $api_key,
            "list_id" => $list_id,
            "emails" => array($info['email']) 

$options = array(
    'http' => array( 
        'method' => 'POST',
        'content' => http_build_query(array('data' => json_encode($data))) 

$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
$response = json_decode($result);

    if (!empty($response['success'])  ){
            echo json_encode( array( 'send'=>true, 'message'=>__( 'Yes!' )));
        } else {
            echo json_encode( array( 'send'=>false, 'message'=>__( 'No!' ) ));


Your action name and hooks don't match. You have hooked to _ajax_news in the hook name:

add_action( 'wp_ajax_nopriv_ajax_news', 'ajax_news' );
add_action( 'wp_ajax_ajax_news', 'ajax_news' );

But the action you are sending in JavaScript is ajaxnews, without the underscore:

'action': 'ajaxnews', //calls wp_ajax_nopriv_ajaxnews

See how your own comment has a different name than the actual hooks you've used?

Either the JavaScript needs to send ajax_news as the action name, or the hooks need to be wp_ajax_nopriv_ajaxnews. They just need to be the same.

| improve this answer | |
  • If I've answered your question could you accept the answer? It helps let other users know that the question has an answer, if they're experiencing a similar problem. – Jacob Peattie Nov 13 '18 at 11:41

Not the answer you're looking for? Browse other questions tagged or ask your own question.