个人工具
名字空间
变换
操作

WordPress:Creating Options Pages

来自站长百科
跳转到: 导航, 搜索

目录

Introduction

介绍

Creating custom options panels in WordPress is relatively easy.

在WordPress中创建自定义选项面板,较为简单。

Firstly, to create the menu item and the new page, see WordPress:Adding Administration Menus.

首先,请看看添加管理菜单,创建菜单内容和新的页面。

So long as you stick to this structure, WordPress will handle all of the option creation, update, saving, and redirection for you. It will check permissions, and do all that other magic behind the scenes.

只要你遵循这个结构,WordPress会处理所有的选项创建,更新,保存和更改的内容。WordPress会检查权限,并且在幕后操作进行许多其它神奇的操作。

Form Tag

表格标签

Once you have your page, you need to create an HTML form. Use this code:

创建了网页后,你就需要创建HTML表格。使用这个代码:

<form method="post" action="options.php">

<form method="post" action="options.php">

nonce Magic

nonce Magic

Then after the opening form tag, insert this PHP code:

<?php wp_nonce_field('update-options'); ?>

打开表格标签后,插入这个PHP代码:

<?php wp_nonce_field('update-options'); ?>

This will insert two hidden fields which automatically help to check that the user can update options and also redirect the user back to the correct admin page (because the form action is a different page).

这样会插入两个隐藏的栏,可以自动地帮助核对,用户可以更新选项,也可以使用户返回到当前的管理页面(因为form action是个不同的页面)。

Update Options Button

更新选项按钮

At this point you may choose to insert the standard Update Options button, the HTML for which is:

这时,你可以选择插入标准的更新选项按钮,HTML是:

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


<p class="submit">

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

Note the use of the _e() function to handle translation of the text, see WordPress:Localizing WordPress for more info.

注意使用the _e() function处理文本翻译,更多的信息,请看看WordPress本地化

Field Names

Field 名称

Use fields with the same names that you want your newly created options (stored in the options table) to be called, for example:

使用栏的名称,与你希望最新创建的选项(储存在选项表格中)得到调用的名称相同,例如:

<input type="text" name="new_option_name" value="<?php echo get_option('new_option_name'); ?>" />


<input type="text" name="new_option_name" value="<?php echo get_option('new_option_name'); ?>" />

Note that by using the get_option() function as the value for the field, this will automatically be updated when the options are saved.

注意通过使用get_option()函数作为栏的参数值,选项保存的时候,这会自动更新。

action Field

action Field

Next, create a hidden field called action containing the value update.

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

其次,创建创建隐藏的栏called action,包含更新的参数值。 <input type="hidden" name="action" value="update" />

page_options Field

page_options Field

Finally, create a hidden field called page_options containing a comma separated list of all the options in the page that should be written on save.

最后,创建包含用逗号分开的网页中所有选项的列表,称为page_options,隐藏的field,编写这些网页时,应该保存。

<input type="hidden" name="page_options" value="new_option_name,some_other_option,option_etc" />

<input type="hidden" name="page_options" value="new_option_name,some_other_option,option_etc" />

Closing Tags

关闭标签

Then obviously close the form tag after your other options, and if you like, include another "Update Options" button, this is the WordPress default.

接着,很明显,在你的其它选项之后,关闭表格标签,如果你愿意,可以加上另一个"更新选项"按钮,这个按钮是WordPress默认带有的。

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


<p class="submit">

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

留言