• Resolved MEnterprises

    (@menterprises)


    I’m building my first plugin, and attempting to register three custom post types in my setup_post_types() function. Within that function, I’m using the register_post_type function, exactly as instructed in the codex (see code below). However, when I activate my plugin, the post types that I registered are not appearing anywhere. My entire plugin consists of one file right now, so what you see below is everything (minus the commented meta data for all plugins).

    add_action( 'init', 'setup_post_types' );
    function setup_post_types() {
    
        // Register our "appointment" custom post type
        $appt_labels = array(
            'name'               => __('Appointments'),
            'singular_name'      => __('Appointment'),
            'menu_name'          => __('My Appointments'),
            'name_admin_bar'     => __('New Appointment'),
            'all_items'          => __('View All Appointments'),
            'add_new'            => __('Book New Appointment'),
            'add_new_item'       => __('Book New Appointment'),
            'edit_item'          => __('Update Appointment'),
            'new_item'           => __('Book New Appointment'),
            'view_item'          => __('Preview this Appointment'),
            'search_items'       => __('Search My Appointments'),
            'not_found'          => __('No Appointments Found'),
            'not_found_in_trash' => __('No Appointments found in Trash')
        );
        $appt_args = array(
            'labels' => $appt_labels,
            'public' => 'true',
            'show_ui' => 'true',
            'show_in_menu' => 'true',
            'show_in_admin_bar' => 'true',
            'menu_position' => 23,
            'menu_icon' => 'dashicons-backup',
            'rewrite' => array('slug' => 'appts'),
            'has_archive' => true
        );
    
        register_post_type('whattimeagain_appt', $appt_args);
    
        // Register our "vendor" custom post type
        $vendor_labels = array(
            'name'               => __('Vendors'),
            'singular_name'      => __('Vendor'),
            'menu_name'          => __('My Vendors'),
            'name_admin_bar'     => __('Add Vendor'),
            'all_items'          => __('All Vendors'),
            'add_new'            => __('Add New Vendor'),
            'add_new_item'       => __('Add New Vendor'),
            'edit_item'          => __('Update Vendor Information'),
            'new_item'           => __('Add New Vendor'),
            'view_item'          => __('Preview Vendor Information'),
            'search_items'       => __('Search My Vendors'),
            'not_found'          => __('Vendor not Found'),
            'not_found_in_trash' => __('Vendor not found in Trash')
        );
        $vendor_args = array(
            'labels' => $vendor_labels,
            'public' => 'true' ,
            'show_ui' => 'true',
            'show_in_menu' => 'true',
            'show_in_admin_bar' => 'true',
            'menu_position' => 21,
            'menu_icon' => 'dashicons-businessman',
            'rewrite' => array('slug' => 'vendors'),
            'has_archive' => true
        );
    
        register_post_type('whattimeagain_vend', $vendor_args);
    
        // Register our "customer" custom post type
        $customer_labels = array(
            'name'               => __('Customers'),
            'singular_name'      => __('Customer'),
            'menu_name'          => __('My Customers'),
            'name_admin_bar'     => __('Add Customer'),
            'all_items'          => __('All Customers'),
            'add_new'            => __('Add New Customer'),
            'add_new_item'       => __('Add New Customer'),
            'edit_item'          => __('Update Customer Information'),
            'new_item'           => __('Add New Customer'),
            'view_item'          => __('Preview Customer Information'),
            'search_items'       => __('Search My Customers'),
            'not_found'          => __('Customer not Found'),
            'not_found_in_trash' => __('Customer not found in Trash')
        );
        $customer_args = array(
            'labels' => $customer_labels,
            'public' => 'true',
            'show_ui' => 'true',
            'show_in_menu' => 'true',
            'show_in_admin_bar' => 'true',
            'menu_position' => 22,
            'menu_icon' => 'dashicons-groups',
            'rewrite' => array('slug' => 'customers'),
            'has_archive' => true
        );
    
        register_post_type('whattimeagain_cust', $customer_args);
    
    }
    function activate() {
    
        // Trigger our function that registers the custom post type
        setup_post_types();
    
        // Clear the permalinks after the post type has been registered
        flush_rewrite_rules();
    
    }
    function deactivate() {
    
        // Our post type will be automatically removed, so no need to unregister it
    
        // Clear the permalinks to remove our post type's rules
        flush_rewrite_rules();
    
    }
    
    register_activation_hook(__FILE__,  'activate');
    register_deactivation_hook(__FILE__,  'deactivate');
Viewing 2 replies - 1 through 2 (of 2 total)
  • This is probably not the place to paste large snippets of code, please consider using pastebin, gist, or similar.

    I can tell you that wherever you have ‘true’ or ‘false’ within quotes, you need to remove the quotes. When you wrap ‘true’ or ‘false’ with quotes (single or double quotes) you are telling PHP to interpret the value as a string.

    If you refer to the documentation of the register_post_type function, you can see that those parameters (show_in_menu for example) are intended to be boolean true or false (bool means boolean).

    PHP boolean true or false is not a string. When you wrap true or false with quotes, PHP interprets that as a string, which is not the intended parameter.

    Removing the quotes from ‘true’ will make PHP interpret them boolean, and your plugin should begin to function as expected.

    A working example can be seen at https://gist.github.com/richaber/f7044d1e7ce5d75435d5#gistcomment-1578024

    Other than some minor code style cleanup performed by my IDE, the only real difference between the code sample you provided and my working example is that I have set ‘true’ (with quotes) to true (without quotes), instructing PHP to interpret the value as boolean, and I have added a plugin header so that I could test in one of my WP sandboxes.

    Thread Starter MEnterprises

    (@menterprises)

    Thanks very much, that worked.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘No custom post types appearing in admin menu’ is closed to new replies.