How to develop a WordPress plug-in that automatically generates a picture library
With the development of the mobile Internet, pictures have become a common medium for us to express and transmit information online. In the process of establishing and maintaining a personal blog, we usually need a picture library to manage and display our picture resources. In order to facilitate the use of WordPress blog users, this article will introduce how to develop a WordPress plug-in that automatically generates image libraries, and provide code examples.
First, we need to create the infrastructure of a WordPress plugin. In your WordPress plugin directory, create a new folder and create a plugin master file in it, named image-library.php
. In the main file, we need to define the basic information and initialization function of the plug-in.
<?php /* Plugin Name: 图片库 Plugin URI: https://www.example.com Description: 一个自动生成图片库的WordPress插件 Version: 1.0 Author: Your Name Author URI: https://www.example.com License: GPL2 */ // 初始化插件 function image_library_init() { // 在这里添加插件的初始化逻辑 } add_action('init', 'image_library_init');
Next, we need to add a custom database table to store image information. In order to simplify the development process, we use the wpdb class that comes with WordPress to perform database operations.
// 创建数据库表 function image_library_create_table() { global $wpdb; $table_name = $wpdb->prefix . 'image_library'; // 添加表前缀 $charset_collate = $wpdb->get_charset_collate(); $sql = "CREATE TABLE $table_name ( id mediumint(9) NOT NULL AUTO_INCREMENT, title text NOT NULL, file_url text NOT NULL, PRIMARY KEY (id) ) $charset_collate;"; require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); dbDelta( $sql ); // 创建表 } register_activation_hook( __FILE__, 'image_library_create_table' );
Next, we need to add a menu item in the WordPress backend so that users can enter the image library management interface. You can use the add_menu_page
function to achieve this function.
// 添加菜单项 function image_library_add_menu() { add_menu_page( '图片库', '图片库', 'manage_options', 'image-library', 'image_library_menu', 'dashicons-format-gallery', 25 ); } add_action('admin_menu', 'image_library_add_menu');
Then, we need to create the interface corresponding to the menu. In this interface, we can display a list of all images and provide functions for uploading and deleting images.
// 图片库管理界面 function image_library_menu() { if (!current_user_can('manage_options')) { wp_die(__('您没有权限访问该页面。')); } // 添加上传图片逻辑 if (isset($_POST['submit'])) { $title = sanitize_text_field($_POST['title']); $file_url = sanitize_text_field($_POST['file_url']); global $wpdb; $table_name = $wpdb->prefix . 'image_library'; $wpdb->insert($table_name, array('title' => $title, 'file_url' => $file_url)); echo '<div class="notice notice-success"><p>图片上传成功!</p></div>'; } // 添加删除图片逻辑 if (isset($_GET['delete']) && isset($_GET['nonce'])) { if (!wp_verify_nonce($_GET['nonce'], 'delete_image')) { wp_die(__('非法请求。')); } $id = intval($_GET['delete']); global $wpdb; $table_name = $wpdb->prefix . 'image_library'; $wpdb->delete($table_name, array('id' => $id)); echo '<div class="notice notice-success"><p>图片删除成功!</p></div>'; } // 展示图片列表 global $wpdb; $table_name = $wpdb->prefix . 'image_library'; $images = $wpdb->get_results("SELECT * FROM $table_name"); echo '<div class="wrap">'; echo '<h1>图片库管理</h1>'; echo '<form method="post">'; echo '<table class="wp-list-table widefat fixed striped">'; echo '<thead><tr><th>标题</th><th>图片链接</th><th>操作</th></tr></thead>'; echo '<tbody>'; foreach ($images as $image) { echo '<tr>'; echo '<td>' . $image->title . '</td>'; echo '<td><a href="' . $image->file_url . '">' . $image->file_url . '</a></td>'; echo '<td><a href="?page=image-library&delete=' . $image->id . '&nonce=' . wp_create_nonce('delete_image') . '">删除</a></td>'; echo '</tr>'; } echo '</tbody>'; echo '</table>'; echo '<h2>上传图片</h2>'; echo '<label>标题:<input type="text" name="title" required></label><br>'; echo '<label>图片链接:<input type="text" name="file_url" required></label><br>'; echo '<input type="submit" name="submit" value="上传">'; echo '</form>'; echo '</div>'; }
Finally, we need to add a shortcode to the image gallery to embed images in posts or pages.
// 图片库短代码 function image_library_shortcode($atts) { ob_start(); global $wpdb; $table_name = $wpdb->prefix . 'image_library'; $images = $wpdb->get_results("SELECT * FROM $table_name"); echo '<div class="image-library">'; foreach ($images as $image) { echo '<div class="image-item">'; echo '<h3>' . $image->title . '</h3>'; echo '<img src="' . $image- alt="How to develop a WordPress plugin that automatically generates image galleries" >file_url . '">'; echo '</div>'; } echo '</div>'; return ob_get_clean(); } add_shortcode('image-library', 'image_library_shortcode');
Now, we have completed the development of the WordPress plug-in that automatically generates image libraries. You can save the above code into the main plugin file image-library.php
and place the file in your WordPress plugin directory. Then, activate the plug-in on the WordPress backend plug-in management page.
When developing plug-ins, pay attention to following WordPress development specifications as much as possible, and provide users with a friendly interface and operating experience. I hope this article can help you develop WordPress plug-ins!
The above is the detailed content of How to develop a WordPress plugin that automatically generates image galleries. For more information, please follow other related articles on the PHP Chinese website!