juandahveed
9/28/2017 - 6:09 PM

meta_boxes

create custom wordpress metaboxes To use these custom fields on the front end of your site, use the get_post_meta or get_post_custom reference: https://code.tutsplus.com/tutorials/how-to-create-custom-wordpress-writemeta-boxes--wp-20336

/**
 * Register meta box(es).
 */
function register_meta_boxes() {
    // add_meta_box( 'meta-box-id', __( 'My Meta Box', 'textdomain' ), 'wpdocs_my_display_callback', 'post' );
    add_meta_box( '42', 'Quote', 'quote_callback', 'page', 'side' );
}
add_action( 'add_meta_boxes', 'register_meta_boxes' );
 
/**
 * Meta box display callback.
 *
 * @param WP_Post $post Current post object.
 */
function quote_callback( $post ) {
    // Display code/markup goes here. Don't forget to include nonces!
    global $post;
    $values = get_post_custom( $post->ID );
	$quote_text = isset( $values['quote_text'] ) ? esc_attr( $values['quote_text'][0] ) : '';
	$author_name = isset( $values['author_name'] ) ? esc_attr( $values['author_name'][0] ) : '';

	// We'll use this nonce field later on when saving.
    wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );

	echo '<p>enter the quote you want to go at the bottom of the page and the author</p>';
	?>
	<label for="my_meta_box_text">Quote Text</label><br />
    <input type="text" name="quote_text" id="quote_text" value="<?php echo $quote_text; ?>" /><br />
    <label for="my_meta_box_text">Author Name</label><br />
    <input type="text" name="author_name" id="author_name" value="<?php echo $author_name; ?>" />
	<?php
}
 
/**
 * Save meta box content.
 *
 * @param int $post_id Post ID
 */
function quote_save_meta_box( $post_id ) {
    // Save logic goes here. Don't forget to include nonce checks!

    // Bail if we're doing an auto save
    if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
     
    // if our nonce isn't there, or we can't verify it, bail
    if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;
     
    // if our current user can't edit this post, bail
    if( !current_user_can( 'edit_post' ) ) return;

     // now we can actually save the data
    $allowed = array( 
        'a' => array( // on allow a tags
        'href' => array() // and those anchors can only have href attribute
        )
    );

    if( isset( $_POST['quote_text'] ) )
        update_post_meta( $post_id, 'quote_text', wp_kses( $_POST['quote_text'] ) );

    if( isset( $_POST['author_name'] ) )
        update_post_meta( $post_id, 'author_name', wp_kses( $_POST['author_name'] ) );

}
add_action( 'save_post', 'quote_save_meta_box' );