Function and usage of register_taxonomy() in wordpress

Question

Answers ( 1 )

    0
    2024-01-08T11:59:20+00:00

    The register_taxonomy() function in WordPress is a crucial tool for developers when it comes to adding custom taxonomies to a WordPress site. Here's a detailed explanation and sample usage of this function:

    Function Signature:

    register_taxonomy( string $taxonomy, array|string $object_type, array|string $args = array() ): WP_Taxonomy|WP_Error
    

    Purpose:

    • Creates or modifies a taxonomy object.
    • Important Note: This function should not be used before the ‘init’ hook.

    Parameters:

    1. $taxonomy (string, required): The key for the taxonomy. It must not exceed 32 characters and should only contain lowercase alphanumeric characters, dashes, and underscores. Refer to sanitize_key() for more details.

    2. $object_type (array|string, required): The object type(s) with which the taxonomy should be associated. This can be a single string or an array of strings representing different object types.

    3. $args (array|string, optional): An array or query string of arguments for registering a taxonomy. Key arguments include:

      • labels (string[]): Custom labels for this taxonomy. Defaults depend on whether the taxonomy is hierarchical (like categories) or not (like tags).
      • description (string): A short descriptive summary of the taxonomy.
      • public (bool): Whether the taxonomy is intended for public use. Influences various default settings like $publicly_queryable, $show_ui, and $show_in_nav_menus.
      • publicly_queryable (bool): Whether the taxonomy is publicly queryable. Inherits from $public if not set.
      • hierarchical (bool): Whether the taxonomy is hierarchical. Defaults to false.
      • show_ui (bool): Whether a UI for managing terms in this taxonomy should be available in the admin. Inherits from $public if not set.
      • show_in_menu (bool): Whether to show the taxonomy in the admin menu, as a submenu of the associated object type.
      • show_in_nav_menus, show_in_rest, rest_base, rest_namespace, rest_controller_class, show_tagcloud, show_in_quick_edit, show_admin_column: Various settings to control visibility and interactions in different parts of WordPress.
      • meta_box_cb (bool|callable): Callback function for the meta box display.
      • capabilities (string[]): Capabilities needed to manage this taxonomy.
      • rewrite (bool|array): Settings for handling rewrites, including slug customization and hierarchical behavior.
      • query_var (string|bool): Sets the query var key for this taxonomy.
      • update_count_callback (callable): Callback function when the term count is updated.
      • default_term (string|array): Default term for the taxonomy.
      • sort (bool): Whether terms should be sorted as provided to wp_set_object_terms().
      • args (array): Default arguments for wp_get_object_terms() for this taxonomy.
      • _builtin (bool): Indicates if this is a built-in taxonomy. For internal use only.

    Returns:

    • WP_Taxonomy|WP_Error: The function returns the registered taxonomy object on success or a WP_Error object on failure.

    Example Usage:

    add_action( 'init', 'create_custom_taxonomy' );
    
    function create_custom_taxonomy() {
        register_taxonomy(
            'custom_taxonomy',  // Taxonomy key
            'post',             // Object type: Post
            array(
                'label' => __( 'Custom Taxonomy' ),
                'rewrite' => array( 'slug' => 'custom-taxonomy' ),
                'hierarchical' => true, // Like categories
            )
        );
    }
    

    In this example, a custom taxonomy named 'custom_taxonomy' is created for 'post' type objects. The taxonomy is hierarchical like categories, and it has a custom slug for URL rewriting. The register_taxonomy() call is hooked to WordPress's init action to ensure it's executed at the right time in the WordPress loading phase.

Leave an answer