WORDPRESS插件开发(二)HELLO WORLD改进版

在上一篇文章中WORDPRESS插件开发(一)HELLO WORLD,演示了Hello World的最简单实现,只是在每篇文章的后面加入Hello World字符,而且字符也是写死的。

如果用户需要自己输入一些文字,然后在每篇文章的后面显示,改怎么做呢?

首先要在后台有一个菜单,点击菜单显示一个页面,在页面中有一个输入框,用户输入完毕后点击保存,将内容保存到数据库,显示每篇文章时,提取保存的信息到页面中就可以了。

实现思路
激活插件时,使用add_option函数向wp_options添加一个字段,禁止插件时,使用delete_option函数删除。
在wordpress后台设置菜单添加插件菜单,添加菜单时使用add_options_page函数
点击菜单时,显示页面,页面中一个输入框一个提交按钮。

代码实现:

<?php 
/*
Plugin Name: Hello-World
Plugin URI: http://1100w.com/
Description: 最简单的插件实现,在每篇文章的后面追加hello world
Version: 1.0
Author: 1100w
Author URI: http://1100w.com
License: GPL
*/
add_filter('the_content','hello_world');
/* Runs when plugin is activated */
register_activation_hook(__FILE__,'hello_world_install'); 

/* Runs on plugin deactivation*/
register_deactivation_hook( __FILE__, 'hello_world_remove' );

function hello_world_install() {
add_option("hello_world_data", 'Default', '', 'yes');
}

function hello_world_remove() {
delete_option('hello_world_data');
}
if ( is_admin() ){

    /* Call the html code */
    add_action('admin_menu', 'hello_world_admin_menu');

    function hello_world_admin_menu() {
        add_options_page('Hello World', 'Hello World', 'administrator','hello-world', 'hello_world_html_page');
    }
}
?>
<?php
function hello_world_html_page() {
?>
<div>
<h2>Hello World Options</h2>

<form method="post" action="options.php">
<?php wp_nonce_field('update-options'); ?>

<table width="510">
<tr valign="top">
<th width="92" scope="row">输入显示内容</th>
<td width="406">
<input name="hello_world_data" type="text" id="hello_world_data"
value="<?php echo get_option('hello_world_data'); ?>" />
(ex. Hello World)</td>
</tr>
</table>

<input type="hidden" name="action" value="update" />
<input type="hidden" name="page_options" value="hello_world_data" />

<p>
<input type="submit" value="<?php _e('Save Changes') ?>" />
</p>

</form>
</div>
<?php
    }
?>
<?php
//Callback function
function hello_world($content)
{
     //Checking if on post page.
     if ( is_single() ) {
         return $content . '<h1>'.get_option('hello_world_data').'</h1>';
     }
     else {
         return $content;
     }
}
?>

显示效果

转载于:https://www.cnblogs.com/imlions/p/4447494.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值