How to Create CPT in WordPress ?
Register Custom Post In WordPress
<?php
/**
* Registers the "testimonial" post type.
*/
add_action( 'init', 'ch_testimonials_post_type_register' );
function ch_testimonials_post_type_register() {
$labels = array(
'name' => __( 'Testimonials', 'ch-cpt' ),
'singular_name' => __( 'Testimonial', 'ch-cpt' ),
'all_items' => __( 'All Testimonials', 'ch-cpt' ),
'add_new' => _x( 'Add New', 'testimonial', 'ch-cpt' ),
'add_new_item' => __( 'Add New Testimonial', 'ch-cpt' ),
'edit_item' => __( 'Edit Testimonial', 'ch-cpt' ),
'new_item' => __( 'New Testimonial', 'ch-cpt' ),
'view_item' => __( 'View Testimonial', 'ch-cpt' ),
'search_items' => __( 'Search Testimonials', 'ch-cpt' ),
'not_found' => __( 'No testimonials found.', 'ch-cpt' ),
'not_found_in_trash' => __( 'No testimonials found in Trash.', 'ch-cpt' ),
);
$args = array(
'labels' => $labels,
'menu_icon' => 'dashicons-format-quote',
'menu_position' => 5,
'public' => true,
'show_ui' => true,
'supports' => array(
'author',
'custom-fields',
'editor',
'page-attributes',
'revisions',
'thumbnail',
'title',
),
'hierarchical' => false,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'post',
);
//$args = apply_filters( 'ch_testimonial_post_type_args', $args );
register_post_type( 'testimonial', $args );
}
Add and Save meta field in CPT WordPress
<?php
/**
* Created action hook function for add meta in "testimonial" post type.
*/
add_action( 'add_meta_boxes', 'ch_add_meta_boxes' );
add_action( 'save_post', 'ch_testimonial_member_option_meta_box_save', 10, 2 );
/**
* Add meta box in the "testimonial" post type.
*/
function ch_add_meta_boxes( $post_type ) {
if ( 'testimonial' === $post_type ) {
add_meta_box(
'ch-item-info',
__( 'Testimonial options', 'ch-cpt' ),
'ch_testimonial_member_option_meta_box_display',
$post_type,
'normal',
'core'
);
}
}
/**
* HTML input field for "testimonial" post type.
*/
function ch_testimonial_member_option_meta_box_display( $post, $metabox ) {
wp_nonce_field( basename( __FILE__ ), 'ch-testimonial-member-option-nonce' ); ?>
<h3 for="ch-member-post"><?php _e( 'Member Post', 'ch-cpt' ); ?></h3>
<div class="ch-input-meta">
<span class="icon"><i class="fa fa-user"></i></span>
<input type="text" name="ch-member-post" id="ch-member-post"
value="<?php echo esc_attr( get_post_meta( $post->ID, 'member-post', true ) ); ?>"
size="30" />
</div>
<?php
/* Allow devs to hook in their own stuff here. */
do_action( 'ch_item_info_meta_box', $post, $metabox );
}
/**
* Save meta value for the "testimonial" post type.
*/
function ch_testimonial_member_option_meta_box_save( $post_id, $post ) {
if ( !isset( $_POST['ch-testimonial-member-option-nonce'] )
|| !wp_verify_nonce( $_POST['ch-testimonial-member-option-nonce'], basename( __FILE__ ) ) )
return;
$meta = array(
'member-post' => esc_attr( $_POST['ch-member-post'] )
);
foreach ( $meta as $meta_key => $new_meta_value ) {
/* Get the meta value of the custom field key. */
$meta_value = get_post_meta( $post_id, $meta_key, true );
/* If there is no new meta value but an old value exists, delete it. */
if ( current_user_can( 'delete_post_meta', $post_id, $meta_key ) &&
'' == $new_meta_value && $meta_value )
delete_post_meta( $post_id, $meta_key, $meta_value );
/* If a new meta value was added and there was no previous value, add it. */
elseif ( current_user_can( 'add_post_meta', $post_id, $meta_key ) &&
$new_meta_value && '' == $meta_value )
add_post_meta( $post_id, $meta_key, $new_meta_value, true );
/* If the new meta value does not match the old value, update it. */
elseif ( current_user_can( 'edit_post_meta', $post_id, $meta_key ) &&
$new_meta_value && $new_meta_value != $meta_value )
update_post_meta( $post_id, $meta_key, $new_meta_value );
}
}
Incredible tips here! I’m excited to put them into practice right away.
ReplyDelete