


Basic WordPress plug-in production tutorial, wordpress plug-in tutorial
Plug-in production preparation work
First we add a folder called "My-Mood" in the wp-contentplugins directory, and add a main file called index.php in the folder. This is the main file of the plug-in. The beginning of the file needs some naming. Format: as below code
<!--?php <br ?--> /* Plugin Name: My Mood Plugin URI: http://www.aips.me Description: 一个心情发布插件 Version: 1.0 Author: 周良博客 Author URI: http://www.aips.me License: GPL */ ?>
- Plugin Name represents the name of the plug-in.
- Plugin URI represents the release address of the plug-in.
- Description represents the description of this plug-in.
- Version represents the version. The first version uses 1.0. If your plug-in is updated, change this version parameter in sequence.
- Author represents the name of the plugin author.
- Author URI represents the author's homepage. .
- License represents the license of the plug-in. If you are open source, use GPL. You can query the parameters of the license on Baidu or Google. I will not describe it in too much length here.
Initial installation of plug-in
Plug-ins are not just style changes. Usually we add new tables. Then I complete the newly added tables through the installation function of the plug-in. We continue to add the following code to index.php:
<!--?php <br ?--> //激活动作 register_activation_hook( __FILE__, 'my_mood_install'); function my_mood_install() { // 启用时要做的事情 global $wpdb; $table_name = $wpdb->prefix . "mood"; $charset_collate = $wpdb->get_charset_collate(); $sql = "CREATE TABLE $table_name ( id mediumint(9) NOT NULL AUTO_INCREMENT, createdon datetime DEFAULT '0000-00-00 00:00:00' NOT NULL, publishedon datetime DEFAULT '0000-00-00 00:00:00' NOT NULL, status int NOT NULL, mood int NOT NULL, text text NOT NULL, address varchar(55) DEFAULT '' NOT NULL, UNIQUE KEY id (id) ) $charset_collate;"; require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); dbDelta( $sql ); } ?>
As commented in the above code, we complete the installation of the plug-in through the register_activation_hook activation action. The activation action is executed through the parameter my_mood_install and the function named my_mood_install is found. This action will be executed when the plug-in is activated.
We created a table named "mood" through the my_mood_install function. The creation of the database table is completed through WordPress's dbDelta function to execute the sql statement. To use this function, you need to introduce wp-admin/includes/ upgrade.php file.
Through the above code, we use the built-in method of WordPress to create a table to store data for the mood plug-in.
Plugin uninstall
Since WordPress is installed, it must be uninstalled. The uninstallation method of the WordPress plug-in is performed through a fixedly named file called uninstall.php. Create a file named uninstall.php in the root directory of the plug-in. The code content is as follows:
<!--?php <br ?--> //卸载动作 my_mood_uninstall(); function my_mood_uninstall() { // 执行内容 global $wpdb; $table_name = $wpdb->prefix . "mood"; $wpdb->query("DROP TABLE IF EXISTS " . $table_name); } ?>
Execute sql through $wpdb->query of Wordpress and delete the table created during our installation. This will delete all content related to the plug-in.
Add background management menu to the plug-in
Such as the following code:
<!--?php <br ?--> //添加菜单 add_action( 'admin_menu', 'my_mood_create_menu' ); function my_mood_create_menu() { global $my_settings; $my_mood_settings=add_menu_page( "My Mood", "My Mood", "manage_options", "my-mood", "test" ); } ?>
With the above code we can add a menu to the plug-in. The method adds a menu through add_action('admin_menu', 'my_mood_create_menu') and the specific page of the menu is bound through parameters. For example, the above method passes in a parameter called "test", so when clicking this "My Mood" menu, you will look for a method called "test" to output the style. We give the test method
<!--?php <br ?--> function test(){ global $wpdb; $table_name = $wpdb->prefix . "mood"; $fivesdrafts = $wpdb->get_results( " SELECT id, createdon, publishedon,status,mood,text,address FROM $table_name ORDER BY createdon DESC " ); ?> <div id="my-mood">foreach ( $fivesdrafts as $fivesdraft ) { ?> } ?> <table class="widefat"> <thead> <tr> <th>发布内容</th> <th>现在所在的</th> <th>心情</th> <th>创建日期</th> <th>操作</th> </tr> </thead> <tfoot> <tr> <th>发布内容</th> <th>现在所在的</th> <th>心情</th> <th>创建日期</th> <th>操作</th> </tr> </tfoot> <tbody> <tr> <td><input name="text" type="text" value="" placeholder="输入你的心情" /></td> <td><input name="address" type="text" value="" placeholder="输入现在所在地" /></td> <td><label>高兴:<input class="mood" checked="checked" name="mood" type="radio" value="0" /></label> <label>一般:<input class="mood" name="mood" type="radio" value="1" /></label> <label>悲伤:<input class="mood" name="mood" type="radio" value="2" /></label> <label>忧虑:<input class="mood" name="mood" type="radio" value="3" /></label> <label>其他:<input class="mood" name="mood" type="radio" value="4" /></label></td> <td></td> <td><a class="add">添加</a></td> </tr> <!--?php <br ?--> <tr> <td><input name="text" type="text" value="'<?php" />text; ?>'/></td> <td><input name="address" type="text" value="'<?php" />address; ?>'/></td> <td><label>高兴:<input class="mood" name="mood<?php echo $fivesdraft->id; ?>" type="radio" />mood==0?'checked=checked':''; ?> value="0"></label> <label>一般:<input class="mood" name="mood<?php echo $fivesdraft->id; ?>" type="radio" />mood=='1'?'checked=checked':''; ?> value="1"></label> <label>悲伤:<input class="mood" name="mood<?php echo $fivesdraft->id; ?>" type="radio" />mood==2?'checked=checked':''; ?> value="2"></label> <label>忧虑:<input class="mood" name="mood<?php echo $fivesdraft->id; ?>" type="radio" />mood==3?'checked=checked':''; ?> value="3"></label> <label>其他:<input class="mood" name="mood<?php echo $fivesdraft->id; ?>" type="radio" />mood==4?'checked=checked':''; ?> value="4"></label></td> <td></td> <td><a class="edit">保存</a><a class="delete">删除</a></td> </tr> <!--?php <br ?--></tbody> </table> </div> <!--?php <br ?--> } ?>
The test method is a mixed style of PHP and HTML codes. The HTMl part is mainly responsible for the output of the style, while the PHP code is responsible for executing the logic of fetching data. The main part is to read data from the database. The data in the table we created in the first step can be retrieved from the database through the $wpdb->get_results method of WordPress. What is returned is a data set containing multiple pieces of data. Finally, the data is output through the foreach loop.
We have displayed the data interface, so how can we save the data? Also based on the example of the mood plug-in in the previous article, first look at the following code
<!--?php <br ?--> function aad_load_scripts($hook) { global $my_settings; if( $hook != $my_settings ) return; /*载入ajax的js文件,也可以载入其他的javascript和/或css等*/ wp_enqueue_script('my-ajax', plugins_url( 'my-mood/js/index.js', __FILE ), array('jquery')); wp_register_style( 'my-style', plugins_url( 'my-mood/css/style.css', __FILE ), array(), '', 'all' ); wp_enqueue_style( 'my-style' ); /* 创建验证nonce 它会输出类似于: <![CDATA[ var aad_vars = {"aad_nonce":"5c18514d34"}; ]]> 之类的被注释掉的js到HTML。 */ wp_localize_script('my-js', 'my_vars', array( 'my_nonce' => wp_create_nonce('aad-nonce') ) ); } add_action('admin_enqueue_scripts', 'aad_load_scripts'); ?>
The code of index.js is as follows
jQuery(document).ready(function(){ jQuery("input").blur(function(){ var value=jQuery(this).val(); jQuery.ajax({ type:"POST", url:"/wp-admin/admin-ajax.php", dataType: 'json', data:{action:"say",value:value}, success:function(data){ } }); }) jQuery(".add").click(function(){ var parent=jQuery(this).closest("tr"); var text=jQuery(parent).find("input[name='text']").val(); var address=jQuery(parent).find("input[name='address']").val(); var mood=jQuery(parent).find("input[type='radio']:checked").val(); jQuery.ajax({ type:"POST", url:"/wp-admin/admin-ajax.php", dataType: 'json', data:{action:"add_mood",text:text,address:address,mood:mood}, success:function(data){ window.location.href=window.location; } }); }) jQuery(".delete").click(function(){ var parent=jQuery(this).closest("tr"); var id=jQuery(parent).attr('data'); jQuery.ajax({ type:"POST", url:"/wp-admin/admin-ajax.php", dataType: 'json', data:{action:"delete_mood",id:id}, success:function(data){ window.location.href=window.location; } }); }) jQuery(".edit").click(function(){ var parent=jQuery(this).closest("tr"); var id=jQuery(parent).attr('data'); var text=jQuery(parent).find("input[name='text']").val(); var address=jQuery(parent).find("input[name='address']").val(); var mood=jQuery(parent).find("input[type='radio']:checked").val(); jQuery.ajax({ type:"POST", url:"/wp-admin/admin-ajax.php", dataType: 'json', data:{action:"edit_mood",id:id,text:text,address:address,mood:mood}, success:function(data){ window.location.href=window.location; } }); }) });
In the above code, we insert the js code and css code we need through Hook, so that the js and css of our plug-in will be inserted into the page code because the plug-in is enabled.
We implement asynchronous loading of data according to the following code:
<!--?php <br ?--> function say(){ $return=array(); $return['success'] = '1'; $return['msg']=$_POST['value']."test-ajax"; echo json_encode($return); die(); } add_action('wp_ajax_say', 'say'); ?>
The meaning of this code is to use ajax to submit data. The format of add_action(‘wp_ajax_function name’, function name) is to register a say route, and its corresponding js code is
jQuery("input").blur(function(){ var value=jQuery(this).val(); jQuery.ajax({ type:"POST", url:"/wp-admin/admin-ajax.php", dataType: 'json', data:{action:"say",value:value}, success:function(data){ } }); })
So you can see that the action of the js code is say
In the same way, data needs to be added, register an add_mood route
<!--?php <br ?--> function add_mood(){ $text=$_POST['text']; $address=$_POST['address']; $mood=$_POST['mood']; add($text,$address,$mood); $return=array(); $return['success'] = '1'; echo json_encode($return); die(); } add_action('wp_ajax_add_mood', 'add_mood'); ?>
To delete data, register a delete_mood route
<!--?php <br ?--> function delete_mood(){ $id=$_POST['id']; delete($id); $return=array(); $return['success'] = '1'; echo json_encode($return); die(); } add_action('wp_ajax_delete_mood', 'delete_mood'); ?>
To edit the data, register an edit_mood route
<!--?php <br ?--> function edit_mood(){ $id=$_POST['id']; $text=$_POST['text']; $address=$_POST['address']; $mood=$_POST['mood']; edit($id,$text,$address,$mood); $return=array(); $return['success'] = '1'; echo json_encode($return); die(); } add_action('wp_ajax_edit_mood', 'edit_mood'); ?>
The php functions corresponding to the above additions, deletions and modifications are as follows
<!--?php <br ?--> function add($text,$address,$mood){ global $wpdb; $table_name = $wpdb->prefix . "mood"; $wpdb->insert( $table_name, array( 'createdon' => current_time( 'mysql' ), 'publishedon' => current_time( 'mysql' ), 'status' => 1, 'mood' => $mood, 'text'=>$text, 'address'=>$address, ) ); } ?> <!--?php <br ?--> function delete($id){ global $wpdb; $table_name = $wpdb->prefix . "mood"; $wpdb->delete( $table_name, array( 'id'=>$id ) ); } ?> <!--?php <br ?--> function edit($id,$text,$address,$mood){ global $wpdb; $table_name = $wpdb->prefix . "mood"; $wpdb->update( $table_name, array( 'mood' => $mood, 'text'=>$text, 'address'=>$address, ), array( 'id' => $id ) ); } ?>
Now that the background data and interface of the plug-in have been processed, how do we reference our mood plug-in in the foreground? We need to add the following code
<!--?php <br ?--> function mood_dispaly(){ global $wpdb; $table_name = $wpdb->prefix . "mood"; $fivesdrafts = $wpdb->get_results( " SELECT text FROM $table_name ORDER BY createdon DESC LIMIT 10 " ); ?> <!--?php <br ?--> } ?>
This code displays the mood data stored in the database in the foreground through HTML. So where is the appearance controlled? Remember the js and css we added in the first step? Yes, the style is controlled by the style inserted in the first step.
This completes a complete mood plug-in. Following the example, you can create your own mood plug-in.

wordpress后台乱码的解决办法:1、在wordpress的“wp-admin”文件夹下找到“admin.header.php”文件;2、将“charset”属性值设置为“UTF-8”格式即可恢复正常。

wordpress标签错误的解决办法:1、找到并打开wordpress的“wp-includes”目录下的“class-wp.php”文件;2、修改内容为“$pathinfo = isset( $_SERVER['PATH_INFO'] )?mb_convert_encoding($_SERVER['PATH_INFO'],'utf-8','GBK') : '';”即可。

你下载的WordPress主题提供的keywords和description这两个meta标签一般都做得很差,或者根本就不提供,这样不利于SEO。本文将指导你如何给主页、分类、页面以及文章页添加单独的Description 和 Keywords。

wordpress乱码的解决办法:1、修改“wp-config.php”文件里的“define(’DB_CHARSET’, ‘utf8′);”为“define(’DB_CHARSET’, ”);”;2、把新数据库的编码设置成“latin1_swedish_ci”;3、以uft8的格式导入备份的数据库文件即可。

wordpress进不去的解决办法:1、把地址栏“wp-login.php”后面的参数删掉,然后重新输入密码登录;2、登录FTP,下载“pluggable.php”文件,然后找到“ADMIN_COOKIE_PATH”并将它替换为“SITECOOKIEPATH”即可。

wordpress不是saas。SaaS是一种软件销售模式,它主要针对云端应用软件,而WordPress是一款CMS系统,它主要针对网站构建和管理。虽然WordPress可以作为SaaS提供服务,但它本质上不是一种SaaS应用。

本次PHP中文网整合了相关的视频教程,中文手册,以及相关的精选文章安利给大家,统统免费!!!通过我们分享的视频,可随时随地免费观看教程视频,也不需要迅雷或者百度网盘下载了。

wordpress是2003年发布的;Matt于2003年5月27日宣布推出第一版WordPress,受到了社区的欢迎,它基于b2 Cafelog并有显著改进;WordPress的第一个版本包括全新的管理界面、模板、XHTML 1.1兼容模板、内容编辑器。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download
The most popular open source editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6
Visual web development tools
