How to add tag management functionality to WordPress plugins
Introduction:
WordPress is one of the most popular content management systems in the world, with a large number of plugins to extend it its function. And tag management function is a very common requirement, in this article, we will learn how to add tag management function to WordPress plugin.
Introduction:
Adding tag management functions to WordPress plug-ins can greatly improve the flexibility and customizability of the plug-in. Users can add tags to plug-ins and categorize, filter and search plug-ins based on these tags. In this article, we will learn how to add a tag management system to a plugin and provide some code examples to aid understanding.
Step 1: Create a database table
First, we need to create a new table in the WordPress database to store the plugin’s tag data. We can do this using the global database object $wpdb
provided by WordPress.
function my_plugin_create_table() { global $wpdb; $table_name = $wpdb->prefix . 'plugin_tags'; $charset_collate = $wpdb->get_charset_collate(); $sql = "CREATE TABLE $table_name ( id mediumint(9) NOT NULL AUTO_INCREMENT, name varchar(100) NOT NULL, PRIMARY KEY (id) ) $charset_collate;"; require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); dbDelta( $sql ); } register_activation_hook( __FILE__, 'my_plugin_create_table' );
The above code will create a new table named plugin_tags
when the plugin is activated.
Step 2: Add a tag management page
Next, we need to add a management page for the plug-in to manage tags. We can use WordPress’s add_menu_page
function to create the admin page.
function my_plugin_add_tags_page() { add_menu_page( 'Plugin Tags', 'Plugin Tags', 'manage_options', 'my_plugin_tags', 'my_plugin_tags_callback', 'dashicons-tag', 25 ); } add_action( 'admin_menu', 'my_plugin_add_tags_page' ); function my_plugin_tags_callback() { // 在这里显示标签管理页面的内容 }
The above code will add a menu item named "Plugin Tags" to the WordPress backend management menu, and specify the callback function my_plugin_tags_callback
to display the content of the tag management page.
Step 3: Add tag management function
In the tag management page, we can add tags to the plug-in and display the existing tag list. We can use WordPress's built-in wp_insert_term
function to add tags, and use the get_terms
function to get a list of existing tags.
function my_plugin_tags_callback() { if ( isset( $_POST['submit'] ) ) { $tag_name = sanitize_text_field( $_POST['tag_name'] ); $term = wp_insert_term( $tag_name, 'plugin_tags' ); if ( is_wp_error( $term ) ) { echo '<div class="error">添加标签失败,请重试。</div>'; } else { echo '<div class="updated">标签已成功添加。</div>'; } } $tags = get_terms( 'plugin_tags' ); echo '<h2>标签管理</h2>'; echo '<form method="post" action="">'; echo '<input type="text" name="tag_name" placeholder="输入标签名称" required />'; echo '<input type="submit" name="submit" value="添加标签" />'; echo '</form>'; if ( ! empty( $tags ) ) { echo '<h3>已有标签</h3>'; foreach ( $tags as $tag ) { echo '<p>' . $tag->name . '</p>'; } } }
The above code example demonstrates how to add tags in the tag management page and display the existing tag list.
Conclusion:
By adding the tag management function to the WordPress plug-in, we can better organize and manage the tag data of the plug-in. I hope the code examples provided in this article can help you easily implement tag management functions when developing plug-ins. Good luck with your plug-in development journey!
The above is the detailed content of How to add tag management functionality to a WordPress plugin. For more information, please follow other related articles on the PHP Chinese website!