1->add_action HOOK簡單說明:http://codex.wordpress.org/Plugin_API/Action_Reference (參考網址)
//在后臺頁腳位置加載(執行)函數 add_str 為自定義的函數名add_action('admin_footer','add_str'); //在后臺頭部位置加載(執行)函數add_action('admin_head','add_str');//在wp加載之前加載(執行)函數add_action('wp_loaded','add_str');//當每加載一篇文章的時候加載(執行)函數add_action('the_post','add_str');//當更新文章的時候加載(執行)函數add_action('save_post','add_str');function add_str() { echo '<h1>Welcome to the plugin (xaiolong production)</h1>';}2->add_filter() HOOK簡單說明 http://codex.wordpress.org/Plugin_API/Filter_Reference(參考網址)
//給每個文章標題,后面加上id編號,和一個自定義的字符串add_filter('the_title','write_title',10,2);function write_title($title,$id){ return $title.'---'.$id.'---create by xiaolong';}//給每個文章內容后面加上一個自定義的字符串add_filter('the_content','write_content',10,1);function write_content($content) { return $content.'---create by xiaolong';}3->插件演示代碼:下載地址:http://pan.baidu.com/s/1mg3JpVy
在 wordpress/wp-content/plugins/ 目錄下 新建一個文件夾取名為first_plugins_demo(或自己定義),在新建的first_plugins_demo目錄下新建first_plugins_demo.php(自定義.php但要求與父級目錄名一直)文件,將以下代碼拷貝到first_plugins_demo.php文件中保存,刷新wordpress后臺-插件菜單-已安裝的插件,啟用First plugins Demo 插件即可
目錄示例:*/wordpress/wp-content/plugins/first_plugins_demo/first_plugins_demo.php
<?php/*Plugin Name: First Plugins DemoPlugin URI: http://m.survivalescaperooms.com/fxmbz/p/4030286.htmlDescription: First Plugins Demo, admin head add a line information, add the Settings menu button In the Settings menuVersion: 1.0Author: xiaolongAuthor URI: http://m.survivalescaperooms.com/fxmbz/License: GPLText Domain: 29583035*//** * [register_activation_hook run set_color_options] */register_activation_hook( __FILE__, 'set_color_options' );/** * [register_deactivation_hook run del_color_options] */register_deactivation_hook( __FILE__, 'del_color_options' );/** * [set_color_options options表插入數據] */function set_color_options() { // options表插入數據 參數:('$option_name','$option_value') add_option( 'color','red' );}/** * [del_color_options 刪除options表數據] */function del_color_options() { // 刪除options表數據 參數:('$option_name') delete_option( 'color' );}/** * [add_action 在wp后臺頭部位置加載(執行)函數 add_acton('$hook', '$function')] * [add_str 輸出字符串, get_option('color') 獲得options表中option_name為color的option_value的值] */add_action( 'admin_head', 'add_str' );function add_str() { echo '<h2 style="color:' . get_option( 'color' ) . '">Welcome to first plugins demo! author: Zhangxl, Email: 29583035@QQ.com</h2>';}/** * [add_action 在后臺菜單位置添加一個頁面 add_acton('$hook', '$function')] * [create_admin_page 配置增加頁面的title, 菜單欄的title, 權限, slug, $function] */add_action( 'admin_menu', 'create_admin_page' );function create_admin_page() { // add_options_page() http://codex.wordpress.org/Function_Reference/add_options_page(參考網址) // add_options_page( $page_title, $menu_title, $capability(權限), $menu_slug(URL-friendly name), $function ); add_options_page( 'First Plugins Demo Setting','First Plugins Settings','manage_options','firstplugindemo','wp_options_page' );}/** * [wp_options_page 輸出頁面模板] * @return [type] [description] */function wp_options_page() { ?> <div class="wrap"> <h2>First Plugins Settings</h2> <!-- 更新options表的數據 --> <?php update_color_option(); ?> <form action="" method="post"> Color: <input type="text" name="color" value="<?php echo get_option( 'color' );?>" /> <input type="submit" name="submit" value="submit" /> </form> </div><?php}/** * [update_color_option 更新options表的數據] * @return [type] [string] */function update_color_option() { $color = $_POST[ 'color' ]; $rule = "/[a-z]|#([0-9a-zA-Z])/"; // 正則匹配客戶輸入的顏色代碼,為純字母,或者是#開頭的16進制代碼 $result = preg_match($rule, $color); if ( $_POST[ 'submit' ] ) { if ( $result ) { update_option( 'color', $color ); echo "<p style='color: green; font-size: 18px;'>Update Success Full !</p>"; } else { echo "<p style='color: red; font-size: 18px;'>Some Thing Wrong ! Please check the Color Name spelling, or a Color input format for 'red' or #aabbcc (Hexadecimal color ode).</p>"; } }}
http://codex.wordpress.org/Template_Tags (參考網址)
https://codex.wordpress.org/Function_Reference (參考網址)
http://codex.wordpress.org/Plugin_API/Filter_Reference(參考網址)
http://codex.wordpress.org/Plugin_API/Action_Reference(參考網址)
新聞熱點
疑難解答