wordpress开发之插件开发初识(wordpress插件开发基础)


当前第2页 返回上一页

add_option有4个参数,功能分别如下:
$name:必选,变量名
$value:可选,变量值,默认为空字符
$deprecated:没用的参数,纯粹是历史遗留问题。留着它只是为了兼容以有的插件。当然如果你要调用到后面的$autoload,你需要为它传入一个空字符或null。
$autoload: “yes” or “no”,默认是”yes”,当设为”yes”时,该属性会在wp_load_alloptions调用时获取到。
get_option用来获取你添加的参数,同时系统中已经默认定义了一些参数,你可以参考Wordpress的官方列表:Option Refernce。
而update_option则是用来更新option。
三个方法都比较好理解,我也不多说了。通过这三个方法你可以把你需要长久保存的数据放在数据库中。
设置页面

有了filter和option,我们已经完成了一个插件的核心工作。不过做为一个插件,它经常还需要为用户提供一个设置页面,也就是在Wordpress后台插件列表中所看到的settings链接,如下图:
settings
这里我们用一个最简单的例子还说明如何添加一个设置页面:


代码如下:

<?php class wctest{
public function __construct(){
if(is_admin()){
add_action('admin_menu', array($this, 'add_plugin_page'));
add_action('admin_init', array($this, 'page_init'));
}
}
public function add_plugin_page(){
// This page will be under "Settings"
add_options_page('Settings Admin', 'Settings', 'manage_options', 'test-setting-admin', array($this, 'create_admin_page'));
}
public function create_admin_page(){
?>
<div>
< ?php screen_icon(); ?>
<h2>Settings</h2>
<form method=”post” action=”options.php”>
< ?php
// This prints out all hidden setting fields
settings_fields(‘test_option_group’);
do_settings_sections(‘test-setting-admin’);
?>
< ?php submit_button(); ?>
</form>
</div>
< ?php
}
public function page_init(){
register_setting(‘test_option_group’, ‘array_key’, array($this, ‘check_ID’));
add_settings_section(
‘setting_section_id’,
‘Setting’,
array($this, ‘print_section_info’),
‘test-setting-admin’
);
add_settings_field(
‘some_id’,
‘Some ID(Title)’,
array($this, ‘create_an_id_field’),
‘test-setting-admin’,
‘setting_section_id’
);
}
public function check_ID($input){
if(is_numeric($input['some_id'])){
$mid = $input['some_id'];
if(get_option(‘test_some_id’) === FALSE){
add_option(‘test_some_id’, $mid);
}else{
update_option(‘test_some_id’, $mid);
}
}else{
$mid = ”;
}
return $mid;
}
public function print_section_info(){
print ‘Enter your setting below:’;
}
public function create_an_id_field(){
?><input type=”text” id=”input_whatever_unique_id_I_want” name=”array_key[some_id]” value=”<?=get_option(‘test_some_id’);?/>” />< ?php
}
}
$wctest = new wctest();

上面创建的类会在你的Wordpress后台添加一个新页面,同时它允许用户保存一个id值。

到这里,我们一个简单的Wordpress插件就完成了。虽然这个插件本身没有什么用途。谢谢大家。


标签:WordPress

返回前面的内容

相关阅读 >>

复现wordpressxmlrpc.php漏洞和ssrf的详细步骤

wordpress在安装使用中出现404、403、500及502问题的分析与解决方法

wordpress固定链接翻译插件 自动将标题翻译成英文

wordpress 实现文章评论排行榜

wordpress中用于获取文章作者与分类信息的方法整理

将博客园(cnblogs.com)数据导入到wordpress的代码

php中__file__、dirname与basename用法实例分析

wordpress免登录发布接口,支持所有wordpress版本

wordpress 插件直接将服务器文件导入媒体库

详解wordpress开发中的get_post与get_posts函数使用

更多相关阅读请进入《wordpress》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...