Wordpress

Stop Using “CPT UI” Plugin: How to Register Custom Post Types with Code (The Clean Way)

register-custom-post-type-wordpress-without-plugin

WordPress is powerful because it’s not just for blogging. You can turn it into a Portfolio, an E-commerce store, or a Directory.

To do this, we use something called Custom Post Types (CPT).

If you are a beginner, you probably installed a plugin called “Custom Post Type UI” (CPT UI) to do this. It’s a popular plugin with 1+ million installs.

But here is the problem: Why install a plugin that adds database rows and admin overhead just to execute a function that WordPress natively supports?

If you want to call yourself a WordPress Engineer, you need to stop relying on UI wrappers for simple code.

Today, I will show you how to register a professional “Portfolio” post type directly in your functions.php file. It’s cleaner, faster, and makes you look like a pro.


The Code Snippet (Copy & Paste)

Open your theme’s functions.php file (or better, use a Child Theme). Paste this code at the bottom:

function musa_register_portfolio_cpt() {
    $labels = array(
        'name'               => _x( 'Portfolio', 'post type general name' ),
        'singular_name'      => _x( 'Project', 'post type singular name' ),
        'add_new'            => _x( 'Add New', 'project' ),
        'add_new_item'       => __( 'Add New Project' ),
        'edit_item'          => __( 'Edit Project' ),
        'new_item'           => __( 'New Project' ),
        'all_items'          => __( 'All Projects' ),
        'view_item'          => __( 'View Project' ),
        'search_items'       => __( 'Search Projects' ),
        'not_found'          => __( 'No projects found' ),
        'not_found_in_trash' => __( 'No projects found in the Trash' ), 
        'menu_name'          => 'Portfolio'
    );

    $args = array(
        'labels'        => $labels,
        'description'   => 'Displays my portfolio and case studies',
        'public'        => true,
        'menu_position' => 5,
        'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt' ),
        'has_archive'   => true,
        'menu_icon'     => 'dashicons-grid-view', // You can change icons here
        'rewrite'       => array( 'slug' => 'portfolio' ), // The URL slug
        'show_in_rest'  => true, // Enables Gutenberg Editor
    );

    register_post_type( 'portfolio', $args );
}
add_action( 'init', 'musa_register_portfolio_cpt' );

 

Understanding the Code (Don’t just copy, learn)

  1. Labels: This array handles what you see in the dashboard (e.g., “Add New Project” instead of “Add New Post”). It makes the UI user-friendly for your clients.
  2. ‘public’ => true: This makes the post type visible on your website.
  3. ‘menu_icon’: I used dashicons-grid-view. You can choose any icon from the WordPress Dashicons library.
  4. ‘show_in_rest’ => true: This is crucial! Setting this to true enables the Gutenberg Block Editor. If you set it to false, you get the old Classic Editor.

Why Is This Better Than a Plugin?

  • Portability: If you put this code in a custom plugin or child theme, you can move it to any client site instantly.
  • Performance: One less plugin to update, load, or worry about security vulnerabilities.
  • Control: You control the exact slug, capabilities, and archive settings.

What Next?

Now that you have registered the Post Type, you will see a “Portfolio” menu in your dashboard. You can start adding projects immediately.

To display these projects on your frontend, you will need a custom loop or a template.

(Need a custom template design for your portfolio? Hire me to build a high-performance showcase for your work.)

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button