search
HomeCMS TutorialWordPressA complete list of WordPress JSON processing related functions

JSON processing is a task that often needs to be processed in WordPress development. For this reason, WordPress defines a bunch of JSON processing functions. The following is done by the WordPress Tutorial column. Let’s give a unified introduction.

A complete list of WordPress JSON processing related functions

wp_json_encode

Encode to JSON and perform some integrity checks.

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

Let’s briefly talk about the reason why WordPress introduced this function:

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

Before json_encode, wp_json_encode uses the function _wp_json_prepare_data to clean the data. If these types are boolean, integer, double, string, NULL, it will be returned directly. If it is an array, continue to use the _wp_json_prepare_data function to clean each element in the array. The element is cleaned. If it is an object, if the class of the object implements the JsonSerializable interface, then $data = $data->jsonSerialize() is returned. Otherwise, continue to use _wp_json_prepare_data to clean each attribute in the object.

Then use json_encode for encoding. If unsuccessful, use _wp_json_sanity_check to perform integrity processing on the data, and finally use json_encode for encoding. _wp_json_sanity_check mainly uses the function _wp_json_convert_string to perform deep UTF-8 detection and conversion of data.

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

wpjam_json_encode

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

PHP5.4 JSON has a new option: JSON_UNESCAPED_UNICODE, hence the name: Do not encode into Unicode to make Chinese more readable.

So we wrote a wpjam_json_encode function. Compared with wp_json_encode, the default value of the $options parameter is set to JSON_UNESCAPED_UNICODE. In this way, if wpjam_json_encode($data) is used directly, Chinese will not be encoded into unicode and is more readable. .

As long as you install the WPJAM Basic plugin, 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. If $status_code is not empty, then 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 )

Also in order to make Chinese more readable after JSON encoding, we also wrote the wpjam_send_json function, which is almost the same as wp_send_json, just calling The function to encode the data is wpjam_json_encode.

In addition, if the incoming data is an instance of WP_Error, then wpjam_send_json directly outputs errcode and errmsg JSON. If errcode is not set, wpjam_send_json will automatically add 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 output success as true, and then set The data $data is placed in data and output. wp_send_json_error will determine whether $data is a WP_Error instance. If so, it will output an array of code and message. Our implementation of wpjam_send_json can already 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, use it directly:

wp_is_json_request()

It determines that $_SERVER['HTTP_ACCEPT'] contains application/json, or $_SERVER['CONTENT_TYPE'] is equal to 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, use it 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 the JSONP callback is a legal JavaScript callback function:

wp_check_jsonp_callback( $callback )

The legal JavaScript callback function can only include numbers plus characters, and English periods.

The above is the detailed content of A complete list of WordPress JSON processing related functions. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:wpjam. If there is any infringement, please contact admin@php.cn delete
Can I learn WordPress in 3 days?Can I learn WordPress in 3 days?Apr 09, 2025 am 12:16 AM

Can learn WordPress within three days. 1. Master basic knowledge, such as themes, plug-ins, etc. 2. Understand the core functions, including installation and working principles. 3. Learn basic and advanced usage through examples. 4. Understand debugging techniques and performance optimization suggestions.

Is WordPress a CMS?Is WordPress a CMS?Apr 08, 2025 am 12:02 AM

WordPress is a Content Management System (CMS). It provides content management, user management, themes and plug-in capabilities to support the creation and management of website content. Its working principle includes database management, template systems and plug-in architecture, suitable for a variety of needs from blogs to corporate websites.

What is the WordPress good for?What is the WordPress good for?Apr 07, 2025 am 12:06 AM

WordPressisgoodforvirtuallyanywebprojectduetoitsversatilityasaCMS.Itexcelsin:1)user-friendliness,allowingeasywebsitesetup;2)flexibilityandcustomizationwithnumerousthemesandplugins;3)SEOoptimization;and4)strongcommunitysupport,thoughusersmustmanageper

Should I use Wix or WordPress?Should I use Wix or WordPress?Apr 06, 2025 am 12:11 AM

Wix is ​​suitable for users who have no programming experience, and WordPress is suitable for users who want more control and expansion capabilities. 1) Wix provides drag-and-drop editors and rich templates, making it easy to quickly build a website. 2) As an open source CMS, WordPress has a huge community and plug-in ecosystem, supporting in-depth customization and expansion.

How much does WordPress cost?How much does WordPress cost?Apr 05, 2025 am 12:13 AM

WordPress itself is free, but it costs extra to use: 1. WordPress.com offers a package ranging from free to paid, with prices ranging from a few dollars per month to dozens of dollars; 2. WordPress.org requires purchasing a domain name (10-20 US dollars per year) and hosting services (5-50 US dollars per month); 3. Most plug-ins and themes are free, and the paid price ranges from tens to hundreds of dollars; by choosing the right hosting service, using plug-ins and themes reasonably, and regularly maintaining and optimizing, the cost of WordPress can be effectively controlled and optimized.

Is WordPress still free?Is WordPress still free?Apr 04, 2025 am 12:06 AM

The core version of WordPress is free, but other fees may be incurred during use. 1. Domain names and hosting services require payment. 2. Advanced themes and plug-ins may be charged. 3. Professional services and advanced features may be charged.

Is WordPress easy for beginners?Is WordPress easy for beginners?Apr 03, 2025 am 12:02 AM

WordPress is easy for beginners to get started. 1. After logging into the background, the user interface is intuitive and the simple dashboard provides all the necessary function links. 2. Basic operations include creating and editing content. The WYSIWYG editor simplifies content creation. 3. Beginners can expand website functions through plug-ins and themes, and the learning curve exists but can be mastered through practice.

Why would anyone use WordPress?Why would anyone use WordPress?Apr 02, 2025 pm 02:57 PM

People choose to use WordPress because of its power and flexibility. 1) WordPress is an open source CMS with strong ease of use and scalability, suitable for various website needs. 2) It has rich themes and plugins, a huge ecosystem and strong community support. 3) The working principle of WordPress is based on themes, plug-ins and core functions, and uses PHP and MySQL to process data, and supports performance optimization.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.