: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
* Sends a JSON response back to an Ajax request, indicating success.
* @since 4.7.0 The `$status_code` parameter was added.
* @since 5.6.0 The `$flags` parameter was added.
* @param mixed $value Optional. Data to encode as JSON, then print and die. Default null.
* @param int $status_code Optional. The HTTP status code to output. Default null.
* @param int $flags Optional. Options to be passed to json_encode(). Default 0.
function wp_send_json_success( $value = null, $status_code = null, $flags = 0 ) {
$response = array( 'success' => true );
$response['data'] = $value;
wp_send_json( $response, $status_code, $flags );
* Sends a JSON response back to an Ajax request, indicating failure.
* If the `$value` parameter is a WP_Error object, the errors
* within the object are processed and output as an array of error
* codes and corresponding messages. All other types are output
* without further processing.
* @since 4.1.0 The `$value` parameter is now processed if a WP_Error object is passed in.
* @since 4.7.0 The `$status_code` parameter was added.
* @since 5.6.0 The `$flags` parameter was added.
* @param mixed $value Optional. Data to encode as JSON, then print and die. Default null.
* @param int $status_code Optional. The HTTP status code to output. Default null.
* @param int $flags Optional. Options to be passed to json_encode(). Default 0.
function wp_send_json_error( $value = null, $status_code = null, $flags = 0 ) {
$response = array( 'success' => false );
if ( is_wp_error( $value ) ) {
foreach ( $value->errors as $code => $messages ) {
foreach ( $messages as $message ) {
$response['data'] = $result;
$response['data'] = $value;
wp_send_json( $response, $status_code, $flags );
* Checks that a JSONP callback is a valid JavaScript callback name.
* Only allows alphanumeric characters and the dot character in callback
* function names. This helps to mitigate XSS attacks caused by directly
* @param string $callback Supplied JSONP callback function name.
* @return bool Whether the callback function name is valid.
function wp_check_jsonp_callback( $callback ) {
if ( ! is_string( $callback ) ) {
preg_replace( '/[^\w\.]/', '', $callback, -1, $illegal_char_count );
return 0 === $illegal_char_count;
* Reads and decodes a JSON file.
* @param string $filename Path to the JSON file.
* @param array $options {
* Optional. Options to be used with `json_decode()`.
* @type bool $associative Optional. When `true`, JSON objects will be returned as associative arrays.
* When `false`, JSON objects will be returned as objects. Default false.
* @return mixed Returns the value encoded in JSON in appropriate PHP type.
* `null` is returned if the file is not found, or its content can't be decoded.
function wp_json_file_decode( $filename, $options = array() ) {
$filename = wp_normalize_path( realpath( $filename ) );
/* translators: %s: Path to the JSON file. */
__( "File %s doesn't exist!" ),
$options = wp_parse_args( $options, array( 'associative' => false ) );
$decoded_file = json_decode( file_get_contents( $filename ), $options['associative'] );
if ( JSON_ERROR_NONE !== json_last_error() ) {
/* translators: 1: Path to the JSON file, 2: Error message. */
__( 'Error when decoding a JSON file at path %1$s: %2$s' ),
* Retrieves the WordPress home page URL.
* If the constant named 'WP_HOME' exists, then it will be used and returned
* by the function. This can be used to counter the redirection on your local
* development environment.
* @param string $url URL for the home location.
* @return string Homepage location.
function _config_wp_home( $url = '' ) {
if ( defined( 'WP_HOME' ) ) {
return untrailingslashit( WP_HOME );
* Retrieves the WordPress site URL.
* If the constant named 'WP_SITEURL' is defined, then the value in that
* constant will always be returned. This can be used for debugging a site
* on your localhost while not having to change the database to your URL.
* @param string $url URL to set the WordPress site location.
* @return string The WordPress site URL.
function _config_wp_siteurl( $url = '' ) {
if ( defined( 'WP_SITEURL' ) ) {
return untrailingslashit( WP_SITEURL );
* Deletes the fresh site option.
function _delete_option_fresh_site() {
update_option( 'fresh_site', '0' );
* Sets the localized direction for MCE plugin.
* Will only set the direction to 'rtl', if the WordPress locale has
* the text direction set to 'rtl'.
* Fills in the 'directionality' setting, enables the 'directionality'
* plugin, and adds the 'ltr' button to 'toolbar1', formerly
* 'theme_advanced_buttons1' array keys. These keys are then returned
* in the $mce_init (TinyMCE settings) array.
* @param array $mce_init MCE settings array.
* @return array Direction set for 'rtl', if needed by locale.
function _mce_set_direction( $mce_init ) {
$mce_init['directionality'] = 'rtl';
$mce_init['rtl_ui'] = true;
if ( ! empty( $mce_init['plugins'] ) && ! str_contains( $mce_init['plugins'], 'directionality' ) ) {
$mce_init['plugins'] .= ',directionality';
if ( ! empty( $mce_init['toolbar1'] ) && ! preg_match( '/\bltr\b/', $mce_init['toolbar1'] ) ) {
$mce_init['toolbar1'] .= ',ltr';
* Determines whether WordPress is currently serving a REST API request.
* The function relies on the 'REST_REQUEST' global. As such, it only returns true when an actual REST _request_ is
* being made. It does not return true when a REST endpoint is hit as part of another request, e.g. for preloading a
* REST response. See {@see wp_is_rest_endpoint()} for that purpose.
* This function should not be called until the {@see 'parse_request'} action, as the constant is only defined then,
* even for an actual REST request.
* @return bool True if it's a WordPress REST API request, false otherwise.
function wp_is_serving_rest_request() {
return defined( 'REST_REQUEST' ) && REST_REQUEST;
* Converts smiley code to the icon graphic file equivalent.
* You can turn off smilies, by going to the write setting screen and unchecking
* the box, or by setting 'use_smilies' option to false or removing the option.
* Plugins may override the default smiley list by setting the $wpsmiliestrans
* to an array, with the key the code the blogger types in and the value the
* The $wp_smiliessearch global is for the regular expression and is set each
* time the function is called.
* The full list of smilies can be found in the function and won't be listed in
* the description. Probably should create a Codex page for it, so that it is
* @global array $wpsmiliestrans
* @global array $wp_smiliessearch
function smilies_init() {
global $wpsmiliestrans, $wp_smiliessearch;
// Don't bother setting up smilies if they are disabled.
if ( ! get_option( 'use_smilies' ) ) {
if ( ! isset( $wpsmiliestrans ) ) {
':mrgreen:' => 'mrgreen.png',
':neutral:' => "\xf0\x9f\x98\x90",
':twisted:' => "\xf0\x9f\x98\x88",
':arrow:' => "\xe2\x9e\xa1",
':shock:' => "\xf0\x9f\x98\xaf",
':smile:' => "\xf0\x9f\x99\x82",
':???:' => "\xf0\x9f\x98\x95",
':cool:' => "\xf0\x9f\x98\x8e",
':evil:' => "\xf0\x9f\x91\xbf",
':grin:' => "\xf0\x9f\x98\x80",
':idea:' => "\xf0\x9f\x92\xa1",
':oops:' => "\xf0\x9f\x98\xb3",
':razz:' => "\xf0\x9f\x98\x9b",
':roll:' => "\xf0\x9f\x99\x84",
':wink:' => "\xf0\x9f\x98\x89",
':cry:' => "\xf0\x9f\x98\xa5",
':eek:' => "\xf0\x9f\x98\xae",
':lol:' => "\xf0\x9f\x98\x86",
':mad:' => "\xf0\x9f\x98\xa1",
':sad:' => "\xf0\x9f\x99\x81",
'8-)' => "\xf0\x9f\x98\x8e",
'8-O' => "\xf0\x9f\x98\xaf",
':-(' => "\xf0\x9f\x99\x81",
':-)' => "\xf0\x9f\x99\x82",
':-?' => "\xf0\x9f\x98\x95",
':-D' => "\xf0\x9f\x98\x80",
':-P' => "\xf0\x9f\x98\x9b",
':-o' => "\xf0\x9f\x98\xae",
':-x' => "\xf0\x9f\x98\xa1",
':-|' => "\xf0\x9f\x98\x90",
';-)' => "\xf0\x9f\x98\x89",
// This one transformation breaks regular text with frequency.
// '8)' => "\xf0\x9f\x98\x8e",
'8O' => "\xf0\x9f\x98\xaf",
':(' => "\xf0\x9f\x99\x81",
':)' => "\xf0\x9f\x99\x82",
':?' => "\xf0\x9f\x98\x95",
':D' => "\xf0\x9f\x98\x80",
':P' => "\xf0\x9f\x98\x9b",
':o' => "\xf0\x9f\x98\xae",
':x' => "\xf0\x9f\x98\xa1",
':|' => "\xf0\x9f\x98\x90",
';)' => "\xf0\x9f\x98\x89",
* Filters all the smilies.
* This filter must be added before `smilies_init` is run, as
* it is normally only run once to setup the smilies regex.
* @param string[] $wpsmiliestrans List of the smilies' hexadecimal representations, keyed by their smily code.
$wpsmiliestrans = apply_filters( 'smilies', $wpsmiliestrans );
if ( count( $wpsmiliestrans ) === 0 ) {
* NOTE: we sort the smilies in reverse key order. This is to make sure
* we match the longest possible smilie (:???: vs :?) as the regular
* expression used below is first-match
krsort( $wpsmiliestrans );
$spaces = wp_spaces_regexp();
// Begin first "subpattern".
$wp_smiliessearch = '/(?<=' . $spaces . '|^)';
foreach ( (array) $wpsmiliestrans as $smiley => $img ) {
$firstchar = substr( $smiley, 0, 1 );
$rest = substr( $smiley, 1 );
if ( $firstchar !== $subchar ) {
$wp_smiliessearch .= ')(?=' . $spaces . '|$)'; // End previous "subpattern".
$wp_smiliessearch .= '|(?<=' . $spaces . '|^)'; // Begin another "subpattern".
$wp_smiliessearch .= preg_quote( $firstchar, '/' ) . '(?:';
$wp_smiliessearch .= '|';
$wp_smiliessearch .= preg_quote( $rest, '/' );
$wp_smiliessearch .= ')(?=' . $spaces . '|$)/m';
* Merges user defined arguments into defaults array.
* This function is used throughout WordPress to allow for both string or array
* to be merged into another array.
* @since 2.3.0 `$args` can now also be an object.
* @param string|array|object $args Value to merge with $defaults.
* @param array $defaults Optional. Array that serves as the defaults.
* @return array Merged user defined values with defaults.
function wp_parse_args( $args, $defaults = array() ) {
if ( is_object( $args ) ) {
$parsed_args = get_object_vars( $args );
} elseif ( is_array( $args ) ) {
wp_parse_str( $args, $parsed_args );
if ( is_array( $defaults ) && $defaults ) {
return array_merge( $defaults, $parsed_args );
* Converts a comma- or space-separated list of scalar values to an array.
* @param array|string $input_list List of values.
* @return array Array of values.
function wp_parse_list( $input_list ) {
if ( ! is_array( $input_list ) ) {
return preg_split( '/[\s,]+/', $input_list, -1, PREG_SPLIT_NO_EMPTY );
// Validate all entries of the list are scalar.
$input_list = array_filter( $input_list, 'is_scalar' );
* Cleans up an array, comma- or space-separated list of IDs.
* @since 5.1.0 Refactored to use wp_parse_list().
* @param array|string $input_list List of IDs.
* @return int[] Sanitized array of IDs.
function wp_parse_id_list( $input_list ) {
$input_list = wp_parse_list( $input_list );
return array_unique( array_map( 'absint', $input_list ) );
* Cleans up an array, comma- or space-separated list of slugs.
* @since 5.1.0 Refactored to use wp_parse_list().
* @param array|string $input_list List of slugs.
* @return string[] Sanitized array of slugs.
function wp_parse_slug_list( $input_list ) {
$input_list = wp_parse_list( $input_list );
return array_unique( array_map( 'sanitize_title', $input_list ) );
* Extracts a slice of an array, given a list of keys.
* @param array $input_array The original array.
* @param array $keys The list of keys.
* @return array The array slice.
function wp_array_slice_assoc( $input_array, $keys ) {
foreach ( $keys as $key ) {
if ( isset( $input_array[ $key ] ) ) {
$slice[ $key ] = $input_array[ $key ];
* Sorts the keys of an array alphabetically.
* The array is passed by reference so it doesn't get returned
* which mimics the behavior of `ksort()`.
* @param array $input_array The array to sort, passed by reference.
function wp_recursive_ksort( &$input_array ) {
foreach ( $input_array as &$value ) {
if ( is_array( $value ) ) {
wp_recursive_ksort( $value );