Skip to main content

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 );

Comments

  1. The register_post_type hook in WordPress allows developers to modify the arguments used to register a custom post type after it has been registered. This can be useful for adjusting properties like labels, supports, and other settings dynamically.

    IEEE projects for cse

    Final Year Projects for CSE

    final year projects for computer science

    ReplyDelete

Post a Comment

Popular posts from this blog

A Guide to UTF-8 for PHP and MySQL

Data Encoding: A Guide to UTF-8 for PHP and MySQL As a MySQL or PHP developer, once you step beyond the comfortable confines of English-only character sets, you quickly find yourself entangled in the wonderfully wacky world of UTF-8. On a previous job, we began running into data encoding issues when displaying bios of artists from all over the world. It soon became apparent that there were problems with the stored data, as sometimes the data was correctly encoded and sometimes it was not. This led programmers to implement a hodge-podge of patches, sometimes with JavaScript, sometimes with HTML charset meta tags, sometimes with PHP, and soon. Soon, we ended up with a list of 600,000 artist bios with double- or triple encoded information, with data being stored in different ways depending on who programmed the feature or implemented the patch. A classical technical rat’s nest.Indeed, navigating through UTF-8 related data encoding issues can be a frustrating and hair-pul...

How To Create Shortcodes In WordPress

We can create own shortcode by using its predified hooks add_shortcode( 'hello-world', 'techsudhir_hello_world_shortcode' ); 1. Write the Shortcode Function Write a function with a unique name, which will execute the code you’d like the shortcode to trigger: function techsudhir_hello_world_shortcode() {    return 'Hello world!'; } Example: [hello-world] If we were to use this function normally, it would return Hello world! as a string 2. Shortcode function with parameters function techsudhir_hello_world_shortcode( $atts ) {    $a = shortcode_atts( array(       'name' => 'world'    ), $atts );    return 'Hello ' . $a['name'] . !'; } Example: [hello-world name="Sudhir"] You can also call shortcode function in PHP using do_shortcode function Example: do_shortcode('[hello-world]');

How to replace plain URLs with links

Here we will explain how to replace Urls with links from string Using PHP $string ='Rajiv Uttamchandani is an astrophysicist, human rights activist, and entrepreneur. Academy, a nonprofit organization dedicated to providing a robust technology-centered education program for refugee and displaced youth around the world.  CNN Interview - https://www.youtube.com/watch?v=EtTwGke6Jtg   CNN Interview - https://www.youtube.com/watch?v=g7pRTAppsCc&feature=youtu.be'; $string = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.%-=#]*(\?\S+)?)?)?)@', '<a href="$1">$1</a>', $string); Using Javascript <script> function linkify(inputText) {     var replacedText, replacePattern1, replacePattern2, replacePattern3;     //URLs starting with http://, https://, or ftp://     replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim;     replacedText = inputT...