Skip to content

Instantly share code, notes, and snippets.

@danielpataki
Last active December 2, 2022 08:49
Show Gist options
  • Save danielpataki/8be03b8fc09aaa5d8db4 to your computer and use it in GitHub Desktop.
Save danielpataki/8be03b8fc09aaa5d8db4 to your computer and use it in GitHub Desktop.
Custom Post Type Example
function my_custom_post_types() {
$args = array(
'label' => 'Recipes',
'hierarchical' => true,
'taxonomies => array( 'post_tag' )
);
register_post_type( 'recipe', $args );
}
add_action( 'init', 'my_custom_post_types' );
add_action( 'init', 'my_recipes' );
add_filter( 'post_updated_messages', 'my_recipes_messages' );
add_action( 'admin_head', 'my_recipes_help' );
function my_recipes() {
$labels = array(
'name' => 'Recipes',
'singular_name' => 'Recipe',
'menu_name' => 'Recipes',
'name_admin_bar' => 'Recipe',
'add_new' => 'Add New',
'add_new_item' => 'Add New Recipe',
'new_item' => 'New Recipe',
'edit_item' => 'Edit Recipe',
'view_item' => 'View Recipe',
'all_items' => 'All Recipes',
'search_items' => 'Search Recipes',
'parent_item_colon' => 'Parent Recipes:',
'not_found' => 'No recipes found.',
'not_found_in_trash' => 'No recipes found in Trash.'
);
$args = array(
'labels' => $labels,
'public' => true,
'rewrite' => array( 'slug' => 'recipe' ),
'has_archive' => true,
'menu_position' => 20,
'menu_icon' => 'dashicons-carrot',
'taxonomies' => array( 'post_tag', 'category' ),
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'custom-fields', 'comments' )
);
register_post_type( 'my_recipe', $args );
}
function my_recipes_messages( $messages ) {
$post = get_post();
$messages['recipe'] = array(
0 => '',
1 => 'Recipe updated.',
2 => 'Custom field updated.',
3 => 'Custom field deleted.',
4 => 'Recipe updated.',
5 => isset( $_GET['revision'] ) ? sprintf( 'Recipe restored to revision from %s',wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
6 => 'Recipe published.',
7 => 'Recipe saved.',
8 => 'Recipe submitted.',
9 => sprintf(
'Recipe scheduled for: <strong>%1$s</strong>.',
date_i18n( 'M j, Y @ G:i', strtotime( $post->post_date ) )
),
10 => 'Recipe draft updated.'
);
return $messages;
}
function my_recipes_help() {
$screen = get_current_screen();
if ( 'recipe' != $screen->post_type ) {
return;
}
$basics = array(
'id' => 'recipe_basics',
'title' => 'Recipe Basics',
'content' => 'Content for help tab here'
);
$formatting = array(
'id' => 'recipe_formatting',
'title' => 'Recipe Formatting',
'content' => 'Content for help tab here'
);
$screen->add_help_tab( $basics );
$screen->add_help_tab( $formatting );
}
add_action( 'admin_head', 'my_custom_post_type_help' );
function my_custom_post_type_help() {
$screen = get_current_screen();
if ( 'recipe' != $screen->post_type ) {
return;
}
$basics = array(
'id' => 'recipe_basics',
'title' => 'Recipe Basics',
'content' => 'Content for help tab here'
);
$formatting = array(
'id' => 'recipe_formatting',
'title' => 'Recipe Formatting',
'content' => 'Content for help tab here'
);
$screen->add_help_tab( $basics );
$screen->add_help_tab( $formatting );
}
function my_custom_post_types() {
$labels = array(
'name' => 'Recipes',
'singular_name' => 'Recipe',
'menu_name' => 'Recipe',
'name_admin_bar' => 'Recipe',
'add_new' => 'Add New',
'add_new_item' => 'Add New Recipe',
'new_item' => 'New Recipe',
'edit_item' => 'Edit Recipe',
'view_item' => 'View Recipe',
'all_items' => 'All Recipes',
'search_items' => 'Search Recipes',
'parent_item_colon' => 'Parent Recipes:',
'not_found' => 'No recipes found.',
'not_found_in_trash' => 'No recipes found in Trash.'
);
$args = array(
'public' => true,
'labels' => $labels,
'description' => 'My yummy recipes will be published using this post type'
);
register_post_type( 'recipe', $args );
}
add_action( 'init', 'my_custom_post_types' );
function my_custom_post_types() {
$args = array(
'menu_position' => 20,
'menu_icon' => 'dashicons-carrot'
);
register_post_type( 'recipe', $args );
}
add_action( 'init', 'my_custom_post_types' );
add_filter( 'post_updated_messages', 'my_custom_post_type_messages' );
function my_custom_post_type_messages( $messages ) {
$post = get_post();
$messages['recipe'] = array(
0 => ''
1 => 'Recipe updated.'
2 => 'Custom field updated.'
3 => 'Custom field deleted.'
4 => 'Recipe updated.'
5 => isset( $_GET['revision'] ) ? sprintf( 'Recipe restored to revision from %s',wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
6 => 'Recipe published.'
7 => 'Recipe saved.'
8 => 'Recipe submitted.'
9 => sprintf(
'Recipe scheduled for: <strong>%1$s</strong>.',
date_i18n( 'M j, Y @ G:i', strtotime( $post->post_date ) )
),
10 => 'Recipe draft updated.'
);
return $messages;
}
function my_custom_post_types() {
$args = array(
'label' => 'Product',
'rewrite' => array( 'slug' => 'product' )
);
register_post_type( 'my_product', $args );
}
add_action( 'init', 'my_custom_post_types' );
function my_custom_post_types() {
register_post_type( 'recipe', array( 'public' => true, 'label' => 'Recipes' ) );
}
add_action( 'init', 'my_custom_post_types' );
function my_custom_post_types() {
$args = array(
'label' => 'Customer Notes',
'public' => false,
'show_ui' => true,
'show_in_admin_bar' => true
);
register_post_type( 'customer_note', $args );
}
add_action( 'init', 'my_custom_post_types' );
@jeuxdaily
Copy link

jeuxdaily commented Feb 18, 2017

thanks

i create a menu

Travel
==== All travel
==== New Travel
==== Tag
==== category

but how can Add

sub menu Calender and reservation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment