php 元框+自定义字段

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 元框+自定义字段相关的知识,希望对你有一定的参考价值。

#coaching_meta{
    overflow: hidden;
    padding-bottom: 1em;
}
 
#coaching_meta p{
    clear: both;
}
 
.coaching-row-title{
    display: block;
    float: left;
    width: 200px;
}
 
.coaching-row-content{
    float: left;
    padding-bottom: 1em;
}
 
.coaching-row-content label{
    display: block;
    line-height: 1.75em;
}
/**
 * Metabox Coaching
 */
function coaching_custom_meta() {
    add_meta_box( 'coaching_meta', __( 'Informazioni Coaching', 'rlt' ), 'crea_custom_field_coaching', 'coaching_os' );
}
add_action( 'add_meta_boxes', 'coaching_custom_meta' );

/**
 * Custom fileds per Metabox Coaching
 */
function crea_custom_field_coaching( $post ) {
    wp_nonce_field( basename( __FILE__ ), 'coaching_nonce' );
    $coaching_stored_meta = get_post_meta( $post->ID );
    ?>
 
    <p>
        <label for="data" class="coaching-row-title"><?php _e( 'Data', 'coaching-textdomain' )?></label>
        <input type="text" name="data" id="data" value="<?php if ( isset ( $coaching_stored_meta['data'] ) ) echo $coaching_stored_meta['data'][0]; ?>" />
    </p>
    
    <p>
        <label for="data_limite_mensile" class="coaching-row-title"><?php _e( 'Data limite mensile', 'coaching-textdomain' )?></label>
        <input type="text" name="data_limite_mensile" id="data_limite_mensile" value="<?php if ( isset ( $coaching_stored_meta['data_limite_mensile'] ) ) echo $coaching_stored_meta['data_limite_mensile'][0]; ?>" />
    </p>
    
    <p>
        <label for="durata" class="coaching-row-title"><?php _e( 'Durata', 'coaching-textdomain' )?></label>
        <input type="text" name="durata" id="durata" value="<?php if ( isset ( $coaching_stored_meta['durata'] ) ) echo $coaching_stored_meta['durata'][0]; ?>" />
    </p>
    
    <p>
        <label for="inizio" class="coaching-row-title"><?php _e( 'Inizio', 'coaching-textdomain' )?></label>
        <input type="text" name="inizio" id="inizio" value="<?php if ( isset ( $coaching_stored_meta['inizio'] ) ) echo $coaching_stored_meta['inizio'][0]; ?>" />
    </p> 
    
    <p>
        <label for="posti_disponibili" class="coaching-row-title"><?php _e( 'Posti disponibili', 'coaching-textdomain' )?></label>
        <input type="text" name="posti_disponibili" id="posti_disponibili" value="<?php if ( isset ( $coaching_stored_meta['posti_disponibili'] ) ) echo $coaching_stored_meta['posti_disponibili'][0]; ?>" />
    </p>
    
    <p>
        <label for="posti_prenotati" class="coaching-row-title"><?php _e( 'Posti prenotati', 'coaching-textdomain' )?></label>
        <input type="text" name="posti_prenotati" id="posti_prenotati" value="<?php if ( isset ( $coaching_stored_meta['posti_prenotati'] ) ) echo $coaching_stored_meta['posti_prenotati'][0]; ?>" />
    </p>
                                                                                                                            
    <p>
        <label for="user_prenotati" class="coaching-row-title"><?php _e( 'User prenotati', 'coaching-textdomain' )?></label>
        <input type="text" name="user_prenotati" id="user_prenotati" value="<?php if ( isset ( $coaching_stored_meta['user_prenotati'] ) ) echo $coaching_stored_meta['user_prenotati'][0]; ?>" />
    </p>        
    <?php
}



/**
 * Salvo i custom fileds
 */
function coaching_meta_save( $post_id ) {
 
    $is_autosave = wp_is_post_autosave( $post_id );
    $is_revision = wp_is_post_revision( $post_id );
    $is_valid_nonce = ( isset( $_POST[ 'coaching_nonce' ] ) && wp_verify_nonce( $_POST[ 'coaching_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false';
 
       if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
        return;
    }
 
    if( isset( $_POST[ 'data' ] ) ) {
        update_post_meta( $post_id, 'data', sanitize_text_field( $_POST[ 'data' ] ) );
    }
	if( isset( $_POST[ 'data_limite_mensile' ] ) ) {
        update_post_meta( $post_id, 'data_limite_mensile', sanitize_text_field( $_POST[ 'data_limite_mensile' ] ) );
    }
	if( isset( $_POST[ 'durata' ] ) ) {
        update_post_meta( $post_id, 'durata', sanitize_text_field( $_POST[ 'durata' ] ) );
    }
	if( isset( $_POST[ 'inizio' ] ) ) {
        update_post_meta( $post_id, 'inizio', sanitize_text_field( $_POST[ 'inizio' ] ) );
    }
	if( isset( $_POST[ 'posti_disponibili' ] ) ) {
        update_post_meta( $post_id, 'posti_disponibili', sanitize_text_field( $_POST[ 'posti_disponibili' ] ) );
    }
	if( isset( $_POST[ 'posti_prenotati' ] ) ) {
        update_post_meta( $post_id, 'posti_prenotati', sanitize_text_field( $_POST[ 'posti_prenotati' ] ) );
    }
	if( isset( $_POST[ 'user_prenotati' ] ) ) {
        update_post_meta( $post_id, 'user_prenotati', sanitize_text_field( $_POST[ 'user_prenotati' ] ) );
    }
 
}
add_action( 'save_post', 'coaching_meta_save' );


// Leggo i valori salvati nel DB
$data = get_post_meta( get_the_ID(), 'data', true );
$data_limite_mensile = get_post_meta( get_the_ID(), 'data_limite_mensile', true );
$durata = get_post_meta( get_the_ID(), 'durata', true );
$inizio = get_post_meta( get_the_ID(), 'inizio', true );
$posti_disponibili = get_post_meta( get_the_ID(), 'posti_disponibili', true );
$posti_prenotati = get_post_meta( get_the_ID(), 'posti_prenotati', true );
$user_prenotati = get_post_meta( get_the_ID(), 'user_prenotati', true );
 
// Controllo e mostro i valori salvati
if( !empty( $data ) ) {
	echo $data;
}
if( !empty( $data_limite_mensile ) ) {
        echo $data_limite_mensile;
    }
if( !empty( $durata ) ) {
        echo $durata;
    }
if( !empty( $inizio ) ) {
        echo $inizio;
    }
if( !empty( $posti_disponibili ) ) {
        echo $posti_disponibili;
    }
if( !empty( $posti_prenotati ) ) {
        echo $posti_prenotati;
    }
if( !empty( $user_prenotati ) ) {
        echo $user_prenotati;
    }

以上是关于php 元框+自定义字段的主要内容,如果未能解决你的问题,请参考以下文章

PHP wordpress自定义帖子类型元框和字段

自定义元框 Save_Post 回调未在 functions.php 中触发

带有元框字段的自定义文章类型

wordpress自定义post类型元框和字段

php 在WordPress中添加自定义元框

php 在WordPress中添加自定义元框