1

I am a WordPress learner. My question is, how can I create a custom simple event in WordPress. Can you explain me in briefly how can I do this? Where should I add CSS and JS files for custom event.

I have also use this link but its not working.noeltock.com

Which type of hook should I use for create a custom post type?

  • Noel Tocks article does exactly what you want, if it doesn't work you might want explain what didn't work for you or what you didn't understand, otherwise any answer you get will be exactly the same. Your question's also very broad. – Tom J Nowell May 8 '15 at 9:57
  • Also note that you asked which hook you should use to create a custom post type, Noels code uses the init hook, which is correct, you can copy paste his code in its entirety for the most part – Tom J Nowell May 8 '15 at 9:58
2

The easiest way to create a custom post type in WordPress is by using a plugin. This method is recommended for beginner because it is safe and easy.

For example : Custom Post Type UI

The problem with using a plugin is that your custom post types will disappear when the plugin is deactivated. you can manually create your custom post type by adding the required code in your theme’s functions.php file.

// Our custom post type function
function create_posttype() {

    register_post_type( 'events',
    // CPT Options
        array(
            'labels' => array(
                'name' => __( 'Event' ),
                'singular_name' => __( 'Event' )
            ),
            'public' => true,
            'has_archive' => true,
            'rewrite' => array('slug' => 'events'),
        )
    );
}
// Hooking up our function to theme setup
add_action( 'init', 'create_posttype' );

It registers a post type 'events' with an array of arguments. These arguments are the options of custom post type. This array has two parts, the first part is labels, which itself is an array. The second part contains other arguments like public visibility, has archive, and slug.

/*
* Creating a function to create Custom post type
*/

function custom_post_type() {

// Set UI labels for Custom Post Type
    $labels = array(
        'name'                => _x( 'Events', 'Post Type General Name', 'twentythirteen' ),
        'singular_name'       => _x( 'Event', 'Post Type Singular Name', 'twentythirteen' ),
        'menu_name'           => __( 'Events', 'twentythirteen' ),
        'parent_item_colon'   => __( 'Parent Event', 'twentythirteen' ),
        'all_items'           => __( 'All Events', 'twentythirteen' ),
        'view_item'           => __( 'View Event', 'twentythirteen' ),
        'add_new_item'        => __( 'Add New Event', 'twentythirteen' ),
        'add_new'             => __( 'Add New', 'twentythirteen' ),
        'edit_item'           => __( 'Edit Event', 'twentythirteen' ),
        'update_item'         => __( 'Update Event', 'twentythirteen' ),
        'search_items'        => __( 'Search Event', 'twentythirteen' ),
        'not_found'           => __( 'Not Found', 'twentythirteen' ),
        'not_found_in_trash'  => __( 'Not found in Trash', 'twentythirteen' ),
    );

// Set other options for Custom Post Type

    $args = array(
        'label'               => __( 'events', 'twentythirteen' ),
        'description'         => __( 'Event news and reviews', 'twentythirteen' ),
        'labels'              => $labels,
        // Features this CPT supports in Post Editor
        'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
        // You can associate this CPT with a taxonomy or custom taxonomy. 
        'taxonomies'          => array( 'types' ),
        /* A hierarchical CPT is like Pages and can have
        * Parent and child items. A non-hierarchical CPT
        * is like Posts.
        */  
        'hierarchical'        => false,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 5,
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'page',
    );

    // Registering your Custom Post Type
    register_post_type( 'events', $args );

}

/* Hook into the 'init' action so that the function
* Containing our post type registration is not 
* unnecessarily executed. 
*/

add_action( 'init', 'custom_post_type', 0 );
  • This code is use only for create a custom post type.I need also create a custom filed in event post like as a event start and end time , start and end date also save in one database.So how can I do this functionality ? – Priyank Prajapati May 11 '15 at 13:39
  • you can add custom fields using add_meta_box function. please refer this link. codex.wordpress.org/Function_Reference/add_meta_box – Jayson May 13 '15 at 9:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.