wp_json_encode( mixed $value, int $flags = 0, int $depth = 512 ): string|false
- Since
- 4.1.0, 5.3.0, 6.5.0
- Source
wp-includes/functions.php:4397
Encodes a variable into JSON, with some confidence checks.
Parameters
$valuemixed- Variable (usually an array or object) to encode as JSON.
$flagsintoptional- Options to be passed to json_encode(). Default 0.Default:
0 $depthintoptional- Maximum depth to walk through $value. Must be greater than 0. Default 512.Default:
512
Return
string|false- The JSON encoded string, or false if it cannot be encoded.
Uses · 1
- _wp_json_sanity_check()Performs confidence checks on data that shall be encoded to JSON.
Used by · 50
- WP_Application_Passwords_List_Table::print_js_template_row()Prints the JavaScript template for the new row item.
- WP_Community_Events::maybe_log_events_response()Logs responses to Events API requests.
- WP_Customize_Date_Time_Control::content_template()Renders a JS template for the content of date time control.
- WP_Customize_Manager::customize_pane_settings()Prints JavaScript settings for parent window.
- WP_Customize_Manager::customize_preview_settings()Prints JavaScript settings for preview frame.
- WP_Customize_Manager::save_changeset_post()Saves the post for the loaded changeset.
- WP_Customize_Manager::wp_die()Custom wp_die wrapper. Returns either the standard message for UI or the Ajax message.
- WP_Customize_Nav_Menus::enqueue_scripts()Enqueues scripts and styles for Customizer pane.
- WP_Customize_Nav_Menus::export_preview_data()Exports data from PHP to JS.
- WP_Customize_Nav_Menus::filter_wp_nav_menu()Prepares wp_nav_menu() calls for partial refresh.
- WP_Customize_Selective_Refresh::export_preview_data()Exports data in preview after it has finished rendering so that partials can be added at runtime.
- WP_Customize_Site_Icon_Control::content_template()Renders a JS template for the content of the site icon control.
Show all 50
- WP_Customize_Widgets::enqueue_scripts()Enqueues scripts and styles for Customizer panel and export data to JavaScript.
- WP_Customize_Widgets::export_preview_data()Communicates the sidebars that appeared on the page at the very end of the page, and at the very end of the wp_footer,
- WP_Customize_Widgets::filter_dynamic_sidebar_params()Inject selective refresh data attributes into widget container elements.
- WP_Interactivity_API::add_load_on_client_navigation_attribute_to_script_modules()Adds the `data-wp-router-options` attribute to script modules that support client-side navigation.
- WP_Internal_Pointers::print_js()Prints the pointer JavaScript data.
- WP_List_Table::_js_vars()Sends required variables to JavaScript land.
- WP_List_Table::ajax_response()Handles an incoming ajax request (called from admin-ajax.php)
- WP_Privacy_Policy_Content::notice()Adds a notice with a link to the guide when editing the privacy policy page.
- WP_REST_Font_Faces_Controller::prepare_item_for_database()Prepares a single font face post for creation.
- WP_REST_Font_Families_Controller::prepare_item_for_database()Prepares a single font family post for create or update.
- WP_REST_Global_Styles_Controller::prepare_item_for_database()Prepares a single global styles config for update.
- WP_REST_Server::json_error()Retrieves an appropriate error representation in JSON.
- WP_REST_Server::serve_request()Handles serving a REST API request.
- WP_Script_Modules::print_import_map()Prints the import map using a script tag with a type="importmap" attribute.
- WP_Script_Modules::print_script_module_data()Print data associated with Script Modules.
- WP_Script_Modules::print_script_module_translations()Prints translations for all enqueued script modules.
- WP_Scripts::localize()Localizes a script, only if the script has already been added.
- WP_Site_Health::wp_cron_scheduled_check()Runs the scheduled event to check and update the latest site health status for the website.
- WP_Style_Engine_Processor::combine_rules_selectors()Combines selectors from the rules store when they have the same styles.
- WP_Themes_List_Table::_js_vars()Send required variables to JavaScript land
- WP_Widget_Archives::widget()Outputs the content for the current Archives widget instance.
- WP_Widget_Categories::widget()Outputs the content for the current Categories widget instance.
- WP_Widget_Custom_HTML::enqueue_admin_scripts()Loads the required scripts and styles for the widget control.
- WP_Widget_Media_Audio::enqueue_admin_scripts()Loads the required media files for the media manager and scripts for media widgets.
- WP_Widget_Media_Gallery::enqueue_admin_scripts()Loads the required media files for the media manager and scripts for media widgets.
- WP_Widget_Media_Image::enqueue_admin_scripts()Loads the required media files for the media manager and scripts for media widgets.
- WP_Widget_Media_Video::enqueue_admin_scripts()Loads the required scripts and styles for the widget control.
- WP_Widget_Text::enqueue_admin_scripts()Loads the required scripts and styles for the widget control.
- _WP_Editors::default_settings()Returns the default TinyMCE settings.
- _WP_Editors::editor_settings()
- _WP_Editors::wp_mce_translation()Translates the default TinyMCE strings and returns them as JSON encoded object ready to be loaded with tinymce.addI18n(), or as JS snippet that should run after tinymce.js is loaded.
- _json_wp_die_handler()Kills WordPress execution and displays JSON response with an error message.
- _jsonp_wp_die_handler()Kills WordPress execution and displays JSONP response with an error message.
- _print_emoji_detection_script()Prints inline Emoji detection script.
- _wp_ajax_menu_quick_search()Prints the appropriate response to a menu quick search.
- _wp_customize_loader_settings()Adds settings for the customize-loader script.
- _wp_enqueue_auto_register_blocks()Exposes blocks with autoRegister flag for ServerSideRender in the editor.
- _wp_filter_post_meta_footnotes()Strips all HTML from the content of footnotes, and sanitizes the ID.
Source
function wp_json_encode( $value, $flags = 0, $depth = 512 ) { $json = json_encode( $value, $flags, $depth ); // If json_encode() was successful, no need to do more confidence checking. if ( false !== $json ) { return $json; } try { $value = _wp_json_sanity_check( $value, $depth ); } catch ( Exception $e ) { return false; } return json_encode( $value, $flags, $depth );}History
Introduced in 4.1.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
6.5.0
The
$data parameter has been renamed to $value and the $options parameter to $flags for parity with PHP.from the docblock5.3.0
No longer handles support for PHP < 5.6.from the docblock
4.1.0
Introduced.from the docblock
About this page
- Parsed data
- Generated from the wordpress-develop 7.0.4 tag, from
src/wp-includes/functions.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.