Get the App
SLTechnology News&Howtos  ›  Servers  › 

A case of WordPress JSON dealing with related functions

Shulou Source: shulou.com Published: 2022-06-02 01:42:52 09月30日 Update

Editor to share with you a case of WordPress JSON dealing with related functions, I hope you will learn a lot after reading this article, let's discuss it together!

JSON processing is a work that often needs to be dealt with in WordPress development, so WordPress defines a bunch of JSON processing functions, which will be uniformly introduced by the WordPress tutorial column below.

Wp_json_encode

Encode it into JSON and do some integrity checks.

Wp_json_encode ($data, $options = 0, $depth = 512)

Briefly explain why WordPress introduced this function:

First of all, different PHP versions of json_encode functions support different parameters. Previously, only one parameter was supported for PHP 5.3.The $options parameter was introduced for data,PHP 5.3.The PHP 5.5introduced the $depth parameter. Therefore, WordPress adapts to different versions of PHP,wp_json_encode supports three parameters, and is compatible with different versions of PHP.

Before json_encode, wp_json_encode used the function _ wp_json_prepare_data to clean up the data, if boolean,integer,double,string,NULL these types, return directly, if the array, continue to use the _ wp_json_prepare_data function to clean up each element in the array, if it is an object, if the object's class implements the JsonSerializable interface, return $data = $data- > jsonSerialize (), otherwise Continue to use _ wp_json_prepare_data to clean up each property in the object.

Then use json_encode for coding, and if not, use _ wp_json_sanity_check to handle the integrity of the data, and finally use json_encode to encode. _ wp_json_sanity_check mainly uses the function _ wp_json_convert_string for deep UTF-8 detection and conversion of data.

Therefore, it is recommended to use wp_json_encode to encode variables with JSON, which is more reliable.

Wpjam_json_encode

Wp_json_encode ($data, $options = JSON_UNESCAPED_UNICODE, $depth = 512)

PHP5.4 JSON added a new option: JSON_UNESCAPED_UNICODE, hence the name: don't encode it into Unicode to make Chinese more readable.

So we wrote a wpjam_json_encode function, compared to wp_json_encode, is to set the default value of the $options parameter to JSON_UNESCAPED_UNICODE, so that the direct use of wpjam_json_encode ($data), the Chinese will not be encoded into unicode, more readable.

As long as you install the WPJAM Basic plug-in, your WordPress will have this function.

Wp_send_json

Send JSON data directly.

Wp_send_json ($response, $status_code = null)

He first outputs the Content-Type header of application/json, and if $status_code is not empty, it outputs the status code of $status_code.

Then call wp_json_encode to encode the data.

Wpjam_send_json

Wpjam_send_json ($response, $status_code = null)

Similarly, in order to make the Chinese more readable after JSON coding, we also wrote the wpjam_send_json function, which is almost the same as wp_send_json, except that the function that calls the data encoding is wpjam_json_encode.

In addition, if the data passed in is an instance of WP_Error, then wpjam_send_json outputs errcode and errmsg JSON directly. If errcode is not set, wpjam_send_json automatically adds errcode= > 0

Install the WPJAM Basic plug-in and your WordPress will have this function.

Wp_send_json_success and wp_send_json_error

WordPress also provides two functions, wp_send_json_success and wp_send_json_error:

Wp_send_json_success ($data = null, $status_code = null) wp_send_json_error ($data = null, $status_code = null)

Wp_send_json_success first outputs success to true, and then puts the data $data into data for output. Wp_send_json_error determines whether $data is an instance of WP_Error, and if so, outputs an array of code and message. The wpjam_send_json we implemented has been able to handle these errors automatically.

Wp_is_json_request

Determine whether the current request is a JSON request or return a JSON result. This function has no parameters and can be used directly:

Wp_is_json_request ()

It determines that $_ SERVER ['HTTP_ACCEPT'] contains application/json, or $_ SERVER [' CONTENT_TYPE'] equals application/json.

Wp_is_jsonp_request

Determine whether the current request is a JSONP request or return a JSONP result. This function has no parameters and can be used directly:

Wp_is_jsonp_request ()

It first determines whether $_ GET ['_ jsonp'] exists, and then determines whether its value is legal through the function wp_check_jsonp_callback.

Wp_check_jsonp_callback

Determine whether JSONP callback is a legitimate JavaScript callback function:

Wp_check_jsonp_callback ($callback)

Legal JavaScript callback functions can only be numbered plus characters and English periods.

After reading this article, I believe you have a certain understanding of the case of WordPress JSON dealing with related functions, want to know more about it, welcome to follow the industry information channel, thank you for reading!

Tags: Function code parameter data processing output different legal object array version Chinese support case integrity instance plug-in article result consistent Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Shulou Information Shulou Technology vpn Xiaomi