函数文档

wp_send_json_success()

💡 云策文档标注

概述

wp_send_json_success() 函数用于向 Ajax 请求发送一个表示成功的 JSON 响应。它自动构建包含 success 键的响应数组,并可选择性地包含数据、HTTP 状态码和 JSON 编码选项。

关键要点

  • 函数用于处理 Ajax 请求的成功响应,确保返回的 JSON 对象始终包含 success: true。
  • 接受三个可选参数:$value(要编码的数据,默认为 null)、$status_code(HTTP 状态码,默认为 null)和 $flags(json_encode() 选项,默认为 0)。
  • 如果提供了 $value,它将被编码为 data 键的值;否则响应仅包含 success 键。
  • 内部调用 wp_send_json() 来输出 JSON 并终止脚本执行。
  • 广泛用于 WordPress 核心的 Ajax 处理函数中,如插件激活、主题安装、健康检查等场景。

代码示例

// 基本用法:发送成功响应,无额外数据
wp_send_json_success();

// 发送包含数据的成功响应
$data = array('name' => 'Andrew', 'call' => 'From API');
wp_send_json_success($data, 200);

// 函数源码示例
function wp_send_json_success( $value = null, $status_code = null, $flags = 0 ) {
    $response = array( 'success' => true );
    if ( isset( $value ) ) {
        $response['data'] = $value;
    }
    wp_send_json( $response, $status_code, $flags );
}

注意事项

  • 调用此函数后,脚本将自动终止(通过 wp_send_json() 的 die() 调用),确保不会输出额外内容。
  • 参数 $flags 从 WordPress 5.6.0 开始引入,用于传递 json_encode() 的选项,如 JSON_PRETTY_PRINT。
  • 在 Ajax 处理程序中应优先使用此函数而非直接输出 JSON,以保持响应格式一致性和安全性。

📄 原文内容

Sends a JSON response back to an Ajax request, indicating success.

Parameters

$valuemixedoptional
Data to encode as JSON, then print and die.

Default:null

$status_codeintoptional
The HTTP status code to output.

Default:null

$flagsintoptional
Options to be passed to json_encode(). Default 0.

More Information

The response object will always have a success key with the value true. If anything is passed to the function it will be encoded as the value for a data key.

Example arrays such as the following are converted to JSON:

$response = array'success' => true );                   //if $data is empty
$response = array'success' => true'data' => $data );  //if $data is set

Source

function wp_send_json_success( $value = null, $status_code = null, $flags = 0 ) {
	$response = array( 'success' => true );

	if ( isset( $value ) ) {
		$response['data'] = $value;
	}

	wp_send_json( $response, $status_code, $flags );
}

Changelog

Version Description
5.6.0 The $flags parameter was added.
4.7.0 The $status_code parameter was added.
3.5.0 Introduced.

User Contributed Notes

  1. Skip to note 3 content

    Busy working on sending JSON requests back to Zapier, here’s an example to help people get started. You can send an associative array of any data you need to get returned/shown.

    wp_send_json_success( array( 
        'name' => 'Andrew', 
        'call' => 'From some API/trigger', 
        'variable' => $var,
    ), 200 );

  2. Skip to note 4 content

    Basic Example

    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.message ); 
    			}
    		);
    	} );
    } );

    save_field.php:

    // Bootstrap WP
    
    $return = array(
    	'message' => __( 'Saved', 'textdomain' ),
    	'ID'      => 1
    );
    wp_send_json_success( $return );