惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
Stack Overflow Blog
Stack Overflow Blog
Hugging Face - Blog
Hugging Face - Blog
博客园_首页
T
The Blog of Author Tim Ferriss
博客园 - 叶小钗
N
Netflix TechBlog - Medium
腾讯CDC
C
Check Point Blog
P
Proofpoint News Feed
Engineering at Meta
Engineering at Meta
GbyAI
GbyAI
S
SegmentFault 最新的问题
F
Fortinet All Blogs
美团技术团队
U
Unit 42
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 司徒正美
F
Full Disclosure
Recorded Future
Recorded Future
D
DataBreaches.Net
博客园 - 【当耐特】
Martin Fowler
Martin Fowler
J
Java Code Geeks
I
InfoQ
Y
Y Combinator Blog
A
About on SuperTechFans
AI
AI
爱范儿
爱范儿
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Forbes - Security
Forbes - Security
W
WeLiveSecurity
M
MIT News - Artificial intelligence
雷峰网
雷峰网
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Simon Willison's Weblog
Simon Willison's Weblog
Schneier on Security
Schneier on Security
The GitHub Blog
The GitHub Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
aimingoo的专栏
aimingoo的专栏
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
G
GRAHAM CLULEY
Know Your Adversary
Know Your Adversary
Latest news
Latest news
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
D
Docker
Recent Commits to openclaw:main
Recent Commits to openclaw:main
量子位
V2EX - 技术
V2EX - 技术
Project Zero
Project Zero

编程设计 – 云破天开

学习写wordpress插件(三)-云破天开 学习写wordpress插件(一)-云破天开 整理电子书-云破天开 两列数据对比,找出不同数据-云破天开 一个简单实用的网站访客统计PHP源码-云破天开 在线图片处理-云破天开 DOS命令-云破天开 EMLOG头部添加名人名言-云破天开 Emlog实现标题+全文内容搜索方法-云破天开
学习写wordpress插件(二)-云破天开
作者:yptk · 2022-10-23 · via 编程设计 – 云破天开

我们的第一个插件nidaye,如果修改显示内容就要修改源代码,这显然不是我们想要的。继续改进。

今天目标:
1、在后台添加独立页面和菜单
2、用户自定义显示内容,保存在数据库中。

<?php

/*
Plugin Name:ni2daye
Plugin URI: http://www.yptk.cn
Version:V0.1
Author: YPTK
Author URI:http://www.yptk.cn
Description:My second plugin named ni2daye
*/
?>
<?php

/* 注册激活插件时要调用的函数 */ 
register_activation_hook( __FILE__, 'ni2daye_install');

/* 注册停用插件时要调用的函数 */ 
register_deactivation_hook( __FILE__, 'ni2daye_remove' );

function ni2daye_install() {  
    /* 在数据库的 wp_options 表中添加一条记录,第二个参数为默认值 */ 
    add_option("ni_2daye", "我是善变的你二大爷。", '', 'yes');  
}

function ni2daye_remove() {  
    /* 删除 wp_options 表中的对应记录 */ 
    delete_option('ni_2daye');  
}

if( is_admin() ) {
    /*  利用 admin_menu 钩子,添加菜单 */
    add_action('admin_menu', 'display_ni2daye_menu');
}

function display_ni2daye_menu() {
    /* add_options_page( $page_title, $menu_title, $capability, $menu_slug, $function);  */
    /* 页名称,菜单名称,访问级别,菜单别名,点击该菜单时的回调函数(用以显示设置页面) */
    add_options_page('Set ni2daye', 'ni2daye Menu', 'administrator','display_ni2daye', 'display_ni2daye_html_page');
}

function display_ni2daye_html_page() {
    ?>
    <div>  
        <h2>Set ni2daye</h2>  
        <form method="post" action="options.php">  
            <?php /* 下面这行代码用来保存表单中内容到数据库 */ ?>  
            <?php wp_nonce_field('update-options'); ?>  
 
            <p>  
                <textarea  
                    name="ni_2daye" 
                    id="ni_2daye"
                    cols="40" 
                    rows="6"><?php echo get_option('ni_2daye'); ?></textarea>  
            </p>  
 
            <p>  
                <input type="hidden" name="action" value="update" />  
                <input type="hidden" name="page_options" value="ni_2daye" />  
 
                <input type="submit" value="Save" class="button-primary" />  
            </p>  
        </form>  
    </div>  
<?php  
}  
add_filter( 'the_content',  'display_ni2daye' );  
/*the_content 是钩子的名字,display_copyright 是回调函数名称。*/
function display_ni2daye( $content ) {  
        $content = get_option('ni_2daye').$content; //把内容添加到每篇文章的开头。
    return $content;  
}  
?>

成功!

修改,Save成功。

显示成功!

版权共享,随意转载:云破天开 » 学习写wordpress插件(二)