: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
* @param string|null $blog_charset Optional. Slug representing a text character encoding, or "charset".
* E.g. "UTF-8", "Windows-1252", "ISO-8859-1", "SJIS".
* Default value is to infer from "blog_charset" option.
* @return bool Whether the slug represents the UTF-8 encoding.
function is_utf8_charset( $blog_charset = null ) {
return _is_utf8_charset( $blog_charset ?? get_option( 'blog_charset' ) );
* Retrieves a canonical form of the provided charset appropriate for passing to PHP
* functions such as htmlspecialchars() and charset HTML attributes.
* @see https://core.trac.wordpress.org/ticket/23688
* @param string $charset A charset name, e.g. "UTF-8", "Windows-1252", "SJIS".
* @return string The canonical form of the charset.
function _canonical_charset( $charset ) {
if ( is_utf8_charset( $charset ) ) {
* Normalize the ISO-8859-1 family of languages.
* This is not required for htmlspecialchars(), as it properly recognizes all of
* the input character sets that here are transformed into "ISO-8859-1".
* @todo Should this entire check be removed since it's not required for the stated purpose?
* @todo Should WordPress transform other potential charset equivalents, such as "latin1"?
( 0 === strcasecmp( 'iso-8859-1', $charset ) ) ||
( 0 === strcasecmp( 'iso8859-1', $charset ) )
* Sets the mbstring internal encoding to a binary safe encoding when func_overload
* When mbstring.func_overload is in use for multi-byte encodings, the results from
* strlen() and similar functions respect the utf8 characters, causing binary data
* to return incorrect lengths.
* This function overrides the mbstring encoding to a binary-safe encoding, and
* resets it to the users expected encoding afterwards through the
* `reset_mbstring_encoding` function.
* It is safe to recursively call this function, however each
* `mbstring_binary_safe_encoding()` call must be followed up with an equal number
* of `reset_mbstring_encoding()` calls.
* @see reset_mbstring_encoding()
* @param bool $reset Optional. Whether to reset the encoding back to a previously-set encoding.
function mbstring_binary_safe_encoding( $reset = false ) {
static $encodings = array();
static $overloaded = null;
if ( is_null( $overloaded ) ) {
if ( function_exists( 'mb_internal_encoding' )
&& ( (int) ini_get( 'mbstring.func_overload' ) & 2 ) // phpcs:ignore PHPCompatibility.IniDirectives.RemovedIniDirectives.mbstring_func_overloadDeprecated
if ( false === $overloaded ) {
$encoding = mb_internal_encoding();
array_push( $encodings, $encoding );
mb_internal_encoding( 'ISO-8859-1' );
if ( $reset && $encodings ) {
$encoding = array_pop( $encodings );
mb_internal_encoding( $encoding );
* Resets the mbstring internal encoding to a users previously set encoding.
* @see mbstring_binary_safe_encoding()
function reset_mbstring_encoding() {
mbstring_binary_safe_encoding( true );
* Filters/validates a variable as a boolean.
* Alternative to `filter_var( $value, FILTER_VALIDATE_BOOLEAN )`.
* @param mixed $value Boolean value to validate.
* @return bool Whether the value is validated.
function wp_validate_boolean( $value ) {
if ( is_bool( $value ) ) {
if ( is_string( $value ) && 'false' === strtolower( $value ) ) {
* @param string $file The path to the file to delete.
function wp_delete_file( $file ) {
* Filters the path of the file to delete.
* @param string $file Path to the file to delete.
$delete = apply_filters( 'wp_delete_file', $file );
if ( ! empty( $delete ) ) {
* Deletes a file if its path is within the given directory.
* @param string $file Absolute path to the file to delete.
* @param string $directory Absolute path to a directory.
* @return bool True on success, false on failure.
function wp_delete_file_from_directory( $file, $directory ) {
if ( wp_is_stream( $file ) ) {
$real_directory = $directory;
$real_file = realpath( wp_normalize_path( $file ) );
$real_directory = realpath( wp_normalize_path( $directory ) );
if ( false !== $real_file ) {
$real_file = wp_normalize_path( $real_file );
if ( false !== $real_directory ) {
$real_directory = wp_normalize_path( $real_directory );
if ( false === $real_file || false === $real_directory || ! str_starts_with( $real_file, trailingslashit( $real_directory ) ) ) {
* Outputs a small JS snippet on preview tabs/windows to remove `window.name` when a user is navigating to another page.
* This prevents reusing the same tab for a preview when the user has navigated away.
* @global WP_Post $post Global post object.
function wp_post_preview_js() {
if ( ! is_preview() || empty( $post ) ) {
// Has to match the window name used in post_submit_meta_box().
$name = 'wp-preview-' . (int) $post->ID;
var query = document.location.search;
if ( query && query.indexOf( 'preview=true' ) !== -1 ) {
window.name = '<?php echo $name; ?>';
if ( window.addEventListener ) {
window.addEventListener( 'pagehide', function() { window.name = ''; } );
wp_print_inline_script_tag( wp_remove_surrounding_empty_script_tags( ob_get_clean() ) );
* Parses and formats a MySQL datetime (Y-m-d H:i:s) for ISO8601 (Y-m-d\TH:i:s).
* Explicitly strips timezones, as datetimes are not saved with any timezone
* information. Including any information on the offset could be misleading.
* Despite historical function name, the output does not conform to RFC3339 format,
* which must contain timezone.
* @param string $date_string Date string to parse and format.
* @return string Date formatted for ISO8601 without time zone.
function mysql_to_rfc3339( $date_string ) {
return mysql2date( 'Y-m-d\TH:i:s', $date_string, false );
* Attempts to raise the PHP memory limit for memory intensive processes.
* Only allows raising the existing limit and prevents lowering it.
* @param string $context Optional. Context in which the function is called. Accepts either 'admin',
* 'image', 'cron', or an arbitrary other context. If an arbitrary context is passed,
* the similarly arbitrary {@see '$context_memory_limit'} filter will be
* invoked. Default 'admin'.
* @return int|string|false The limit that was set or false on failure.
function wp_raise_memory_limit( $context = 'admin' ) {
// Exit early if the limit cannot be changed.
if ( false === wp_is_ini_value_changeable( 'memory_limit' ) ) {
$current_limit = ini_get( 'memory_limit' );
$current_limit_int = wp_convert_hr_to_bytes( $current_limit );
if ( -1 === $current_limit_int ) {
$wp_max_limit = WP_MAX_MEMORY_LIMIT;
$wp_max_limit_int = wp_convert_hr_to_bytes( $wp_max_limit );
$filtered_limit = $wp_max_limit;
* Filters the maximum memory limit available for administration screens.
* This only applies to administrators, who may require more memory for tasks
* like updates. Memory limits when processing images (uploaded or edited by
* users of any role) are handled separately.
* The `WP_MAX_MEMORY_LIMIT` constant specifically defines the maximum memory
* limit available when in the administration back end. The default is 256M
* (256 megabytes of memory) or the original `memory_limit` php.ini value if
* @since 4.6.0 The default now takes the original `memory_limit` into account.
* @param int|string $filtered_limit The maximum WordPress memory limit. Accepts an integer
* (bytes), or a shorthand string notation, such as '256M'.
$filtered_limit = apply_filters( 'admin_memory_limit', $filtered_limit );
* Filters the memory limit allocated for image manipulation.
* @since 4.6.0 The default now takes the original `memory_limit` into account.
* @param int|string $filtered_limit Maximum memory limit to allocate for image processing.
* Default `WP_MAX_MEMORY_LIMIT` or the original
* php.ini `memory_limit`, whichever is higher.
* Accepts an integer (bytes), or a shorthand string
* notation, such as '256M'.
$filtered_limit = apply_filters( 'image_memory_limit', $filtered_limit );
* Filters the memory limit allocated for WP-Cron event processing.
* @param int|string $filtered_limit Maximum memory limit to allocate for WP-Cron.
* Default `WP_MAX_MEMORY_LIMIT` or the original
* php.ini `memory_limit`, whichever is higher.
* Accepts an integer (bytes), or a shorthand string
* notation, such as '256M'.
$filtered_limit = apply_filters( 'cron_memory_limit', $filtered_limit );
* Filters the memory limit allocated for an arbitrary context.
* The dynamic portion of the hook name, `$context`, refers to an arbitrary
* context passed on calling the function. This allows for plugins to define
* their own contexts for raising the memory limit.
* @param int|string $filtered_limit Maximum memory limit to allocate for this context.
* Default WP_MAX_MEMORY_LIMIT` or the original php.ini `memory_limit`,
* whichever is higher. Accepts an integer (bytes), or a
* shorthand string notation, such as '256M'.
$filtered_limit = apply_filters( "{$context}_memory_limit", $filtered_limit );
$filtered_limit_int = wp_convert_hr_to_bytes( $filtered_limit );
if ( -1 === $filtered_limit_int || ( $filtered_limit_int > $wp_max_limit_int && $filtered_limit_int > $current_limit_int ) ) {
if ( false !== ini_set( 'memory_limit', $filtered_limit ) ) {
} elseif ( -1 === $wp_max_limit_int || $wp_max_limit_int > $current_limit_int ) {
if ( false !== ini_set( 'memory_limit', $wp_max_limit ) ) {
* Generates a random UUID (version 4).
function wp_generate_uuid4() {
'%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
mt_rand( 0, 0x0fff ) | 0x4000,
mt_rand( 0, 0x3fff ) | 0x8000,
* Validates that a UUID is valid.
* @param mixed $uuid UUID to check.
* @param int $version Specify which version of UUID to check against. Default is none,
* to accept any UUID version. Otherwise, only version allowed is `4`.
* @return bool The string is a valid UUID or false on failure.
function wp_is_uuid( $uuid, $version = null ) {
if ( ! is_string( $uuid ) ) {
if ( is_numeric( $version ) ) {
if ( 4 !== (int) $version ) {
_doing_it_wrong( __FUNCTION__, __( 'Only UUID V4 is supported at this time.' ), '4.9.0' );
$regex = '/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/';
$regex = '/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/';
return (bool) preg_match( $regex, $uuid );
* This is a PHP implementation of Underscore's uniqueId method. A static variable
* contains an integer that is incremented with each call. This number is returned
* with the optional prefix. As such the returned value is not universally unique,
* but it is unique across the life of the PHP process.
* @param string $prefix Prefix for the returned ID.
* @return string Unique ID.
function wp_unique_id( $prefix = '' ) {
return $prefix . (string) ++$id_counter;
* Generates an incremental ID that is independent per each different prefix.
* It is similar to `wp_unique_id`, but each prefix has its own internal ID
* counter to make each prefix independent from each other. The ID starts at 1
* and increments on each call. The returned value is not universally unique,
* but it is unique across the life of the PHP process and it's stable per
* @param string $prefix Optional. Prefix for the returned ID. Default empty string.
* @return string Incremental ID per prefix.
function wp_unique_prefixed_id( $prefix = '' ) {
static $id_counters = array();
if ( ! is_string( $prefix ) ) {
sprintf( 'The prefix must be a string. "%s" data type given.', gettype( $prefix ) )
if ( ! isset( $id_counters[ $prefix ] ) ) {
$id_counters[ $prefix ] = 0;
$id = ++$id_counters[ $prefix ];
return $prefix . (string) $id;
* Gets last changed date for the specified cache group.
* @param string $group Where the cache contents are grouped.
* @return string UNIX timestamp with microseconds representing when the group was last changed.
function wp_cache_get_last_changed( $group ) {
$last_changed = wp_cache_get( 'last_changed', $group );
return wp_cache_set_last_changed( $group );
* Sets last changed date for the specified cache group to now.
* @param string $group Where the cache contents are grouped.
* @return string UNIX timestamp when the group was last changed.
function wp_cache_set_last_changed( $group ) {
$previous_time = wp_cache_get( 'last_changed', $group );