0
X
Add Snippet To Project
New Project
Add To Existing Project
<?php
namespace WPTurbo;
add_action( 'init', 'wpturbo_register_trainer_post_type' );
function wpturbo_register_trainer_post_type() {
$args = array(
'labels' => array(
'name' => __( 'Trainers', 'wpturbo' ),
'singular_name' => __( 'Trainer', 'wpturbo' ),
),
'public' => true,
'has_archive' => true,
'show_in_rest' => true,
'supports' => array(
'title',
'thumbnail',
),
);
register_post_type( 'trainer', $args );
}
add_action( 'add_meta_boxes', 'wpturbo_add_trainer_meta_boxes' );
function wpturbo_add_trainer_meta_boxes() {
add_meta_box( 'wpturbo_trainer_details', __( 'Trainer Details', 'wpturbo' ), 'wpturbo_render_trainer_meta_box', 'trainer', 'normal', 'high' );
}
function wpturbo_render_trainer_meta_box( $post ) {
wp_nonce_field( 'wpturbo_save_trainer_details', 'wpturbo_trainer_nonce' );
$profession = get_post_meta( $post->ID, '_wpturbo_profession', true );
$email = get_post_meta( $post->ID, '_wpturbo_email', true );
$description_one = get_post_meta( $post->ID, '_wpturbo_description_one', true );
$description_two = get_post_meta( $post->ID, '_wpturbo_description_two', true );
echo '<label for="wpturbo_profession">' . __( 'Profession', 'wpturbo' ) . '</label>';
echo '<input type="text" id="wpturbo_profession" name="wpturbo_profession" value="' . esc_attr( $profession ) . '" />';
echo '<label for="wpturbo_email">' . __( 'Email Address', 'wpturbo' ) . '</label>';
echo '<input type="email" id="wpturbo_email" name="wpturbo_email" value="' . esc_attr( $email ) . '" />';
echo '<label for="wpturbo_description_one">' . __( 'Description One', 'wpturbo' ) . '</label>';
echo '<textarea id="wpturbo_description_one" name="wpturbo_description_one">' . esc_textarea( $description_one ) . '</textarea>';
echo '<label for="wpturbo_description_two">' . __( 'Description Two', 'wpturbo' ) . '</label>';
echo '<textarea id="wpturbo_description_two" name="wpturbo_description_two">' . esc_textarea( $description_two ) . '</textarea>';
}
add_action( 'save_post', 'wpturbo_save_trainer_meta_box_data' );
function wpturbo_save_trainer_meta_box_data( $post_id ) {
if ( ! isset( $_POST['wpturbo_trainer_nonce'] ) ) {
return;
}
if ( ! wp_verify_nonce( $_POST['wpturbo_trainer_nonce'], 'wpturbo_save_trainer_details' ) ) {
return;
}
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
if ( isset( $_POST['wpturbo_profession'] ) ) {
update_post_meta( $post_id, '_wpturbo_profession', sanitize_text_field( $_POST['wpturbo_profession'] ) );
}
if ( isset( $_POST['wpturbo_email'] ) ) {
update_post_meta( $post_id, '_wpturbo_email', sanitize_email( $_POST['wpturbo_email'] ) );
}
if ( isset( $_POST['wpturbo_description_one'] ) ) {
update_post_meta( $post_id, '_wpturbo_description_one', sanitize_textarea_field( $_POST['wpturbo_description_one'] ) );
}
if ( isset( $_POST['wpturbo_description_two'] ) ) {
update_post_meta( $post_id, '_wpturbo_description_two', sanitize_textarea_field( $_POST['wpturbo_description_two'] ) );
}
}
This code should be added to the wp-plugin.php file you are working on. Be sure to adjust the text domain and any other specific settings as needed.