• WordPress插件制作笔记(一)---First Plugins Demo


    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 Demo
    Plugin URI: http://www.cnblogs.com/fxmbz/p/4030286.html
    Description: First Plugins Demo, admin head add a line information, add the Settings menu button In the Settings menu
    Version: 1.0
    Author: xiaolong
    Author URI: http://www.cnblogs.com/fxmbz/
    License: GPL
    Text 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 (参考网址)

  • 相关阅读:
    备忘asp.net core使用中间件实现IP白名单访问
    Unity asp.net 依赖注入框架Unity
    linunx命令学习_文件命令
    Css中的属性选择器
    Html页基础代码
    JSX简介
    Js对象与Json字符串的互转
    React中的函数式组件和类组件
    win文件夹上右键后以xx打开
    Java中的泛型1_泛型的概念
  • 原文地址:https://www.cnblogs.com/fxmbz/p/4030286.html
Copyright © 2020-2023  润新知