search
HomeCMS TutorialWordPressHow to add article category management function to WordPress plug-in

How to add article category management function to WordPress plug-in

How to add article category management function to WordPress plug-in

WordPress is one of the most widely used content management systems at present, and it provides a wealth of plug-ins to extend its functions. . If you are a plug-in developer, you may encounter the need to add article category management functions to your plug-in. This article will introduce you to how to add article category management functions to WordPress plugins, and provide code examples for reference.

  1. Create a category
    First, we need to create a new article category for the plug-in. This can be accomplished using the register_taxonomy() function. The following is a sample code:
// 在插件的主活动文件中添加以下代码
function custom_plugin_taxonomy() {
    $labels = array(
        'name' => _x( '插件分类', 'taxonomy general name', 'textdomain' ),
        'singular_name' => _x( '插件分类', 'taxonomy singular name', 'textdomain' ),
        'search_items' =>  __( '搜索分类', 'textdomain' ),
        'all_items' => __( '所有分类', 'textdomain' ),
        'parent_item' => __( '父级分类', 'textdomain' ),
        'parent_item_colon' => __( '父级分类:', 'textdomain' ),
        'edit_item' => __( '编辑分类', 'textdomain' ),
        'update_item' => __( '更新分类', 'textdomain' ),
        'add_new_item' => __( '添加新分类', 'textdomain' ),
        'new_item_name' => __( '新分类名称', 'textdomain' ),
        'menu_name' => __( '分类', 'textdomain' ),
    );

    $args = array(
        'hierarchical' => true,
        'labels' => $labels,
        'show_ui' => true,
        'show_admin_column' => true,
        'query_var' => true,
        'rewrite' => array( 'slug' => 'plugin_category' ),
    );

    register_taxonomy( 'plugin_category', array( 'post' ), $args );
}
add_action( 'init', 'custom_plugin_taxonomy', 0 );

In the above code, we use the register_taxonomy() function to create a new article category named plugin_category . This category has some basic properties such as name, search text, and editing operations.

  1. Enable category management for the plug-in
    Now, we need to add an interface to the plug-in so that users can select and manage categories in the article editing page. We can use the hook function add_meta_box() to achieve this. The following is a sample code:
// 在插件的主活动文件中添加以下代码
function custom_plugin_taxonomy_meta_box() {
    add_meta_box(
        'plugin_category',
        __( '插件分类', 'textdomain' ),
        'custom_plugin_taxonomy_meta_box_callback',
        'post',
        'side',
        'default'
    );
}
add_action( 'add_meta_boxes', 'custom_plugin_taxonomy_meta_box' );

function custom_plugin_taxonomy_meta_box_callback( $post ) {
    wp_nonce_field( 'custom_plugin_taxonomy_meta_box', 'custom_plugin_taxonomy_meta_box_nonce' );

    $terms = get_terms( array(
        'taxonomy' => 'plugin_category',
        'hide_empty' => false,
    ) );

    if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
        echo '<select name="plugin_category">';
        foreach ( $terms as $term ) {
            echo '<option value="' . $term->term_id . '">' . $term->name . '</option>';
        }
        echo '</select>';
    }
}

In the above code, we use the add_meta_box() function to add a new meta box for displaying the category selection box. In the custom_plugin_taxonomy_meta_box_callback() function, we use the get_terms() function to get all available categories and output a drop-down menu for the user to choose.

  1. Saving and updating categories
    Finally, we need to add code to save and update the selected category. We can use the hook function save_post to handle this task. The following is a sample code:
// 在插件的主活动文件中添加以下代码
function custom_plugin_taxonomy_save_meta_box_data( $post_id ) {
    if ( ! isset( $_POST['plugin_category'] ) ) {
        return;
    }

    if ( ! wp_verify_nonce( $_POST['custom_plugin_taxonomy_meta_box_nonce'], 'custom_plugin_taxonomy_meta_box' ) ) {
        return;
    }

    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return;
    }

    if ( ! current_user_can( 'edit_post', $post_id ) ) {
        return;
    }

    $term_id = intval( $_POST['plugin_category'] );
    wp_set_post_terms( $post_id, array( $term_id ), 'plugin_category' );
}
add_action( 'save_post', 'custom_plugin_taxonomy_save_meta_box_data' );

In the above code, we check whether the category selection box is selected and verify the submitted form data using the wp_verify_nonce() function. Then, we use the wp_set_post_terms() function to save the selected category to the current article.

Through the above steps, you have successfully added the article category management function to your WordPress plug-in. Users can now select and manage categories in the article editing page for a better content management experience.

Summary
This article introduces how to add article category management functions to the WordPress plug-in and provides relevant code examples. You can add this by using the register_taxonomy() function to create a taxonomy, the add_meta_box() function to add interfaces, and the save_post hook function to save and update the taxonomy. Features are quickly integrated into their own plug-ins. I hope this article is helpful to you, and I wish you can write more powerful WordPress plug-ins!

The above is the detailed content of How to add article category management function to WordPress plug-in. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Can I learn WordPress in 3 days?Can I learn WordPress in 3 days?Apr 09, 2025 am 12:16 AM

Can learn WordPress within three days. 1. Master basic knowledge, such as themes, plug-ins, etc. 2. Understand the core functions, including installation and working principles. 3. Learn basic and advanced usage through examples. 4. Understand debugging techniques and performance optimization suggestions.

Is WordPress a CMS?Is WordPress a CMS?Apr 08, 2025 am 12:02 AM

WordPress is a Content Management System (CMS). It provides content management, user management, themes and plug-in capabilities to support the creation and management of website content. Its working principle includes database management, template systems and plug-in architecture, suitable for a variety of needs from blogs to corporate websites.

What is the WordPress good for?What is the WordPress good for?Apr 07, 2025 am 12:06 AM

WordPressisgoodforvirtuallyanywebprojectduetoitsversatilityasaCMS.Itexcelsin:1)user-friendliness,allowingeasywebsitesetup;2)flexibilityandcustomizationwithnumerousthemesandplugins;3)SEOoptimization;and4)strongcommunitysupport,thoughusersmustmanageper

Should I use Wix or WordPress?Should I use Wix or WordPress?Apr 06, 2025 am 12:11 AM

Wix is ​​suitable for users who have no programming experience, and WordPress is suitable for users who want more control and expansion capabilities. 1) Wix provides drag-and-drop editors and rich templates, making it easy to quickly build a website. 2) As an open source CMS, WordPress has a huge community and plug-in ecosystem, supporting in-depth customization and expansion.

How much does WordPress cost?How much does WordPress cost?Apr 05, 2025 am 12:13 AM

WordPress itself is free, but it costs extra to use: 1. WordPress.com offers a package ranging from free to paid, with prices ranging from a few dollars per month to dozens of dollars; 2. WordPress.org requires purchasing a domain name (10-20 US dollars per year) and hosting services (5-50 US dollars per month); 3. Most plug-ins and themes are free, and the paid price ranges from tens to hundreds of dollars; by choosing the right hosting service, using plug-ins and themes reasonably, and regularly maintaining and optimizing, the cost of WordPress can be effectively controlled and optimized.

Is WordPress still free?Is WordPress still free?Apr 04, 2025 am 12:06 AM

The core version of WordPress is free, but other fees may be incurred during use. 1. Domain names and hosting services require payment. 2. Advanced themes and plug-ins may be charged. 3. Professional services and advanced features may be charged.

Is WordPress easy for beginners?Is WordPress easy for beginners?Apr 03, 2025 am 12:02 AM

WordPress is easy for beginners to get started. 1. After logging into the background, the user interface is intuitive and the simple dashboard provides all the necessary function links. 2. Basic operations include creating and editing content. The WYSIWYG editor simplifies content creation. 3. Beginners can expand website functions through plug-ins and themes, and the learning curve exists but can be mastered through practice.

Why would anyone use WordPress?Why would anyone use WordPress?Apr 02, 2025 pm 02:57 PM

People choose to use WordPress because of its power and flexibility. 1) WordPress is an open source CMS with strong ease of use and scalability, suitable for various website needs. 2) It has rich themes and plugins, a huge ecosystem and strong community support. 3) The working principle of WordPress is based on themes, plug-ins and core functions, and uses PHP and MySQL to process data, and supports performance optimization.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.