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, namedimage-library.php
. In the main file, we need to define the basic information and initialization function of the plug-in.
Copy after login
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 theadd_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 ''; } // 添加删除图片逻辑 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 '图片上传成功!
'; } // 展示图片列表 global $wpdb; $table_name = $wpdb->prefix . 'image_library'; $images = $wpdb->get_results("SELECT * FROM $table_name"); echo '图片删除成功!
'; echo ''; }图片库管理
'; echo ''; echo '
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 ''; foreach ($images as $image) { echo ''; return ob_get_clean(); } add_shortcode('image-library', 'image_library_shortcode');'; echo ''; } echo '' . $image->title . '
'; echo 'file_url . '">'; echo '
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 fileimage-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!