Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/wp send json error

This page redirects to an external site: https://developer.wordpress.org/reference/functions/wp_send_json_error/

Description

Send a JSON response back to an Ajax request, indicating failure. The response object will always have a success key with the value false. If anything is passed to the function in the $data parameter, it will be encoded as the value for a data key.

Usage

<?php wp_send_json_error$data ); the response will look like (before it is encoded).
$response = array( 'success' => false );                   //if $data is empty
$response = array( 'success' => false, 'data' => $data );  //if $data is set

Parameters

$data
(mixed) (optional) Data to encode as JSON, then print and die.
Default: null

Examples

jQuery(document).ready(function(){

    jQuery('#btn_save').click(function(e){
       e.preventDefault();
                jQuery.post(pluginUrl+'ajax/save_field.php',jQuery('#my-form').serialize(), function(data) {                        
                alert(data.success);                
            });
    });

});

save_field.php

<?php
$nonce=$_POST['_wpnonce_name'];
if (empty($_POST) || !wp_verify_nonce($nonce, 'my-nonce') )
	wp_send_json_error(); // sends json_encoded success=false

Notes

Uses wp_send_json() to send response.

Change Log

Source File

wp_send_json_error() is located in wp-includes/functions.php

Related

wp_send_json(), wp_send_json_success()