Monday, 2 July 2018

How to use registered post type Hook

How to use registered_post_type Hook and modify post type registration

Create custom post type hook for products inside function.php file

add_action( 'init', 'wpyog_register_products_cpt' );
/**
 * Register Products Custom Post Type 
 */
function wpyog_register_products_cpt() {
    // change 'wpyog_products' to whatever your text_domain is.
    
    /** Setup labels */
    $labels = array(
        'name'               => x_( 'Products', 'wpyog_products' ),
        'singular_name'      => x_( 'Product', 'wpyog_products' ),
        'add_new'            => x_( 'Add New', 'wpyog_products' ),
        'all_items'          => x_( 'All Products', 'wpyog_products' ),
        'add_new_item'       => x_( 'Add New Product', 'wpyog_products' ),
        'edit_item'          => x_( 'Edit Product', 'wpyog_products' ),
        'new_item'           => x_( 'New Product', 'wpyog_products' ),
        'view_item'          => x_( 'View Product', 'wpyog_products' ),
        'search_items'       => x_( 'Search Products', 'wpyog_products' ),
        'not_found'          => x_( 'No Products found', 'wpyog_products' ),
        'not_found_in_trash' => x_( 'No Products found in trash', 'wpyog_products' ),
        'parent_item_colon'  => x_( 'Parent Product:', 'wpyog_products' ),
        'menu_name'          => x_( 'Products', 'wpyog_products' )
    );
    
    /** Setup args */
    $args = array(
        'labels'              => $labels,
        'description'         => x_( 'Products post type', 'wpyog_products' ),
        'public'              => true,
        'menu_position'       => 20,
        'supports'            => array( 'title', 'editor', 'excerpt', 'page-attributes', ),
        'has_archive'         => 'books',
        'rewrite'             => array( 'slug' => 'product', ),
    );
    
    /** Register Custom Post Type */
    register_post_type( 'wpyog_products', $args );
}

So if I wanted to change the label, add function inside function.php file:

add_action( 'registered_post_type', 'wpyog_products_label_rename', 10, 2 );
/**
 * Modify registered post type
 *
 * @param string $post_type Registered post type name.
 * @param array $args Array of post type parameters.
 */
function wpyog_products_label_rename( $post_type, $args ) {
    if ( 'wpyog_products' === $post_type ) {
        global $wp_post_types;
$args->labels->menu_name = __( 'Custom Products', 'wpyog_products' );
        $args->exclude_from_search = false;
        $wp_post_types[ $post_type ] = $args;
$wp_post_types['wpyog_products']->exclude_from_search = false;
    }



So if I wanted to change the taxonomy slug, add function inside function.php file:

function wpyog_modify_taxonomy() {
$product_category_args = get_taxonomy( 'kbe_taxonomy' ); // returns an object
// make changes to the args
// in this example there are three changes
// again, note that it's an object
$product_category_args->show_admin_column = true;
$product_category_args->rewrite['slug'] = 'product_resources';
$product_category_args->rewrite['with_front'] = true;

// re-register the taxonomy
register_taxonomy( 'kbe_taxonomy', 'product_resources', (array) $product_category_args );
}

add_action( 'init', 'wpyog_modify_taxonomy', 11 );

0 comments:

Post a Comment