How to add a custom post type in wordpress
For add a custom post type in your wordpress site. just copy below code and paste it into your theme's functions.php file and change name and slug as you want.
add_action( 'init', 'rj_post_type_fireworks_video' );
function rj_post_type_fireworks_video() {
$labels = array(
'name' => _x( 'Fireworks Video', 'post type general name', 'flatsome' ),
'singular_name' => _x( 'Fireworks Video', 'post type singular name', 'flatsome' ),
'menu_name' => _x( 'Fireworks Videos', 'admin menu', 'flatsome' ),
'name_admin_bar' => _x( 'Fireworks Video', 'add new on admin bar', 'flatsome' ),
'add_new' => _x( 'Add New', 'firework-video', 'flatsome' ),
'add_new_item' => __( 'Add New Video', 'flatsome' ),
'new_item' => __( 'New Video', 'flatsome' ),
'edit_item' => __( 'Edit Video', 'flatsome' ),
'view_item' => __( 'View Video', 'flatsome' ),
'all_items' => __( 'All Video', 'flatsome' ),
'search_items' => __( 'Search Videos', 'flatsome' ),
'parent_item_colon' => __( 'Parent Videos:', 'flatsome' ),
'not_found' => __( 'No videos found.', 'flatsome' ),
'not_found_in_trash' => __( 'No videos found in Trash.', 'flatsome' )
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'firework-video' ),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
);
register_post_type( 'firework-video', $args );
}
add_filter( 'post_updated_messages', 'rj_firework_video_updated_messages' );
function rj_firework_video_updated_messages( $messages ) {
$post = get_post();
$post_type = get_post_type( $post );
$post_type_object = get_post_type_object( $post_type );
$messages['firework-video'] = array(
0 => '', // Unused. Messages start at index 1.
1 => __( 'Fireworks video updated.', 'flatsome' ),
2 => __( 'Custom field updated.', 'flatsome' ),
3 => __( 'Custom field deleted.', 'flatsome' ),
4 => __( 'Fireworks video updated.', 'flatsome' ),
/* translators: %s: date and time of the revision */
5 => isset( $_GET['revision'] ) ? sprintf( __( 'Fireworks video restored to revision from %s', 'flatsome' ), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
6 => __( 'Fireworks video published.', 'flatsome' ),
7 => __( 'Fireworks video saved.', 'flatsome' ),
8 => __( 'Fireworks video submitted.', 'flatsome' ),
9 => sprintf(
__( 'Fireworks video scheduled for: <strong>%1$s</strong>.', 'flatsome' ),
// translators: Publish box date format, see http://php.net/date
date_i18n( __( 'M j, Y @ G:i', 'flatsome' ), strtotime( $post->post_date ) )
),
10 => __( 'Fireworks video draft updated.', 'flatsome' )
);
if ( $post_type_object->publicly_queryable ) {
$permalink = get_permalink( $post->ID );
$view_link = sprintf( ' <a href="%s">%s</a>', esc_url( $permalink ), __( 'View Fireworks video', 'flatsome' ) );
$messages[ $post_type ][1] .= $view_link;
$messages[ $post_type ][6] .= $view_link;
$messages[ $post_type ][9] .= $view_link;
$preview_permalink = add_query_arg( 'preview', 'true', $permalink );
$preview_link = sprintf( ' <a target="_blank" href="%s">%s</a>', esc_url( $preview_permalink ), __( 'Preview Fireworks video', 'flatsome' ) );
$messages[ $post_type ][8] .= $preview_link;
$messages[ $post_type ][10] .= $preview_link;
}
return $messages;
}
add_action( 'init', 'rj_post_type_fireworks_video' );
function rj_post_type_fireworks_video() {
$labels = array(
'name' => _x( 'Fireworks Video', 'post type general name', 'flatsome' ),
'singular_name' => _x( 'Fireworks Video', 'post type singular name', 'flatsome' ),
'menu_name' => _x( 'Fireworks Videos', 'admin menu', 'flatsome' ),
'name_admin_bar' => _x( 'Fireworks Video', 'add new on admin bar', 'flatsome' ),
'add_new' => _x( 'Add New', 'firework-video', 'flatsome' ),
'add_new_item' => __( 'Add New Video', 'flatsome' ),
'new_item' => __( 'New Video', 'flatsome' ),
'edit_item' => __( 'Edit Video', 'flatsome' ),
'view_item' => __( 'View Video', 'flatsome' ),
'all_items' => __( 'All Video', 'flatsome' ),
'search_items' => __( 'Search Videos', 'flatsome' ),
'parent_item_colon' => __( 'Parent Videos:', 'flatsome' ),
'not_found' => __( 'No videos found.', 'flatsome' ),
'not_found_in_trash' => __( 'No videos found in Trash.', 'flatsome' )
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'firework-video' ),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
);
register_post_type( 'firework-video', $args );
}
add_filter( 'post_updated_messages', 'rj_firework_video_updated_messages' );
function rj_firework_video_updated_messages( $messages ) {
$post = get_post();
$post_type = get_post_type( $post );
$post_type_object = get_post_type_object( $post_type );
$messages['firework-video'] = array(
0 => '', // Unused. Messages start at index 1.
1 => __( 'Fireworks video updated.', 'flatsome' ),
2 => __( 'Custom field updated.', 'flatsome' ),
3 => __( 'Custom field deleted.', 'flatsome' ),
4 => __( 'Fireworks video updated.', 'flatsome' ),
/* translators: %s: date and time of the revision */
5 => isset( $_GET['revision'] ) ? sprintf( __( 'Fireworks video restored to revision from %s', 'flatsome' ), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
6 => __( 'Fireworks video published.', 'flatsome' ),
7 => __( 'Fireworks video saved.', 'flatsome' ),
8 => __( 'Fireworks video submitted.', 'flatsome' ),
9 => sprintf(
__( 'Fireworks video scheduled for: <strong>%1$s</strong>.', 'flatsome' ),
// translators: Publish box date format, see http://php.net/date
date_i18n( __( 'M j, Y @ G:i', 'flatsome' ), strtotime( $post->post_date ) )
),
10 => __( 'Fireworks video draft updated.', 'flatsome' )
);
if ( $post_type_object->publicly_queryable ) {
$permalink = get_permalink( $post->ID );
$view_link = sprintf( ' <a href="%s">%s</a>', esc_url( $permalink ), __( 'View Fireworks video', 'flatsome' ) );
$messages[ $post_type ][1] .= $view_link;
$messages[ $post_type ][6] .= $view_link;
$messages[ $post_type ][9] .= $view_link;
$preview_permalink = add_query_arg( 'preview', 'true', $permalink );
$preview_link = sprintf( ' <a target="_blank" href="%s">%s</a>', esc_url( $preview_permalink ), __( 'Preview Fireworks video', 'flatsome' ) );
$messages[ $post_type ][8] .= $preview_link;
$messages[ $post_type ][10] .= $preview_link;
}
return $messages;
}
Comments
Post a Comment