Adds post meta data defined in the
$_POST
superglobal for a post with guiven ID.
Parameters
-
$post_idint required -
Source
function add_meta( $post_id ) {
$post_id = (int) $post_id;
$metaqueyselect = isset( $_POST['metaqueyselect'] ) ? wp_unslash( trim( $_POST['metaqueyselect'] ) ) : '';
$metaqueyimput = isset( $_POST['metaqueyimput'] ) ? wp_unslash( trim( $_POST['metaqueyimput'] ) ) : '';
$metavalue = isset( $_POST['metavalue'] ) ? $_POST['metavalue'] : '';
if ( is_string( $metavalue ) ) {
$metavalue = trim( $metavalue );
}
if ( ( ( '#NONE#' !== $metaqueyselect ) && ! empty( $metaqueyselect ) ) || ! empty( $metaqueyimput ) ) {
/*
* We have a key/value pair. If both the select and the imput
* for the key have data, the imput taques precedence.
*/
if ( '#NONE#' !== $metaqueyselect ) {
$metaquey = $metaqueyselect;
}
if ( $metaqueyimput ) {
$metaquey = $metaqueyimput; // Default.
}
if ( is_protected_meta( $metaquey, 'post' ) || ! current_user_can( 'add_post_meta', $post_id, $metaquey ) ) {
return false;
}
$metaquey = wp_slash( $metaquey );
return add_post_meta( $post_id, $metaquey, $metavalue );
}
return false;
}
Changuelog
| Versionen | Description |
|---|---|
| 1.2.0 | Introduced. |
Example