Register taxonomy from plugin. WordPress example open

Register taxonomy from plugin. WordPress example

Approved. Code works!
This is exactly the working code that is verified by the moderator or site administrators
Tested: PHP 5.6 +

After creating your plugin, if it creates custom post types then you can also create custom taxonomy for them.


Use this example of implementing PHP classes for your needs:

<?php
/*
Plugin Name: My Plugin
Plugin URI: https://mypluginsite.com/
Description: One of best plugins in the planet
Version: 1.0
Author: John
Author URI: https://facebook.com/john
Licence: GPLv2 or later
Text Domain: myplugin
Domain Path: /lang
 */

if (!defined('ABSPATH')) {
    die;
}



class myPlugin
{
    public function custom_post_type(){
        // register_post_type('cars',
        // array(
        //     'public' => true,  
        //     'has_archive' => true,
        //     'rewrite' => array('slug' => 'cars'),
        //     'label' => 'Cars',
        //     'supports' => array('title', 'editor', 'thumbnail'),
        // ));
        
        
        $args = array (
            'hierarchical' => true,  // An extensive taxonomy system is enabled
            'show_ui' => true, // Visibility in the admin panel
            'show_admin_column' => true,
            'query_var' => true,
            'rewrite' => array('slug' => 'cars/brand'),
            'labels' => $labels,

        );
		
		register_taxonomy('brand', 'cars', $args );

        $labels = array(
            'name'              => esc_html_x( 'Brands', 'taxonomy general name', 'myplugin' ),
            'singular_name'     => esc_html_x( 'Brand', 'taxonomy singular name', 'myplugin' ),
            'search_items'      => esc_html__( 'Search Brands', 'myplugin' ),
            'all_items'         => esc_html__( 'All Brands', 'myplugin' ),
            'parent_item'       => esc_html__( 'Parent Brand', 'myplugin' ),
            'parent_item_colon' => esc_html__( 'Parent Brand:', 'myplugin' ),
            'edit_item'         => esc_html__( 'Edit Brand', 'myplugin' ),
            'update_item'       => esc_html__( 'Update Brand', 'myplugin' ),
            'add_new_item'      => esc_html__( 'Add New Brand', 'myplugin' ),
            'new_item_name'     => esc_html__( 'New Brand Name', 'myplugin' ),
            'menu_name'         => esc_html__( 'Brand', 'myplugin' ),
        );


    }



    public function register(){
        add_action('init', [$this, 'custom_post_type']);
    }



    public static function activation(){

        flush_rewrite_rules(); //rewrite site links
    }
    public static function deactivation(){

        flush_rewrite_rules(); //rewrite site links
    }
}


if (class_exists('myPlugin')) {
    $myPlugin = new myPlugin();
    $myPluign->register();
}

register_activation_hook(__FILE__, array($myPlugin, 'activation'));
register_deactivation_hook(__FILE__, array($myPlugin, 'deactivation'));


It defines an array named $args that contains configuration options for the custom taxonomy. These options are used when registering the taxonomy.

‘hierarchical’ => true: This option indicates that the taxonomy is hierarchical, meaning it can have parent and child terms, like categories.

‘show_ui’ => true: This option specifies that the taxonomy should be visible in the WordPress admin panel.

‘show_admin_column’ => true: This option ensures that an admin column is displayed for the “brand” taxonomy in the list of “cars” posts.

‘query_var’ => true: This option allows the taxonomy to be used as part of the query variables for filtering posts.

‘rewrite’ => array(‘slug’ => ‘cars/brand’): This option sets the URL slug for the “brand” taxonomy. For example, a brand term “Ford” would have a URL like “example.com/cars/brand/ford.

‘labels’ => $labels: The labels for the taxonomy, such as its name, singular name, search items, and more, are defined in the $labels array. However, there’s an issue here because you’re defining the labels before the $labels array itself.

After defining the $args array, the register_taxonomy function is called to register the “brand” taxonomy. The first argument is the name of the taxonomy (‘brand’), the second argument is the associated post type (‘cars’), and the third argument is the array of configuration options stored in $args.

Following the register_taxonomy call, the labels for the taxonomy are defined in the $labels array. These labels are used to provide user-friendly names for the “brand” taxonomy in the WordPress admin panel.

0

More

Leave a Reply

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

How many?: 22 + 22

lil-code© | 2022 - 2024
Go Top
Authorization
*
*
Registration
*
*
*
*
Password generation