我需要在 WordPress 中创建一个选项页面,允许为同一个键输入多个值。为了让它按我想要的方式工作,我需要对选项表单发送的 $_POST 数据进行一些自定义处理。我还需要使用自定义 HTML 创建选项表单,而不是由设置 API 生成。
制作选项页面的正常过程是使用 注册选项页面add_options_page(),注册设置并让 WordPress 为您生成表单do_settings_sections()。然后该<form>标签有一个属性action="options.php"。
add_options_page()
do_settings_sections()
<form>
action="options.php"
我的问题是:我可以改变它的工作方式吗?例如,我可以让回调看起来像这样:
<?php add_options_page( 'My plugin Options', 'My plugin Options', 'administrator', 'MYPLUGIN_plugin_options', 'MYPLUGIN_options' ); function MYPLUGIN_options() { if ( 'update' == $_POST['action'] ) // Do some updating else // Generate form }
这行得通吗?这是实现我想要的可接受的方式吗?如果没有,我还能怎么做?我无法在 WordPress 文档中找到这样做的“合法”方式。也许你们中的一个人已经找到了。
我不知道这是否真的是您正在寻找的答案。该脚本创建一个插件 index.php 和 parameter-options.php 。index.php 将一个名为“Your options-pagename”的新页面添加到 wordpress 设置选项卡。在那里,您可以通过向表单添加新的输入值来管理选项。您应该将大写的 NAMESPACE 文本替换为您自己的项目名称。另外:此脚本未经测试。
plugindir/yourpluginname/index.php
/* Plugin Name: Your options page Plugin URI: http://www.yourdomain.de Description: descriptiontext Author: authorname Version: v1.0 Author URI: your uri */ if( ! function_exists('NAMESPACE_add_admin_menu_items')): function NAMESPACE_add_admin_menu_items() { // add parameter options page to wp admin add_options_page('Your options-pagename', 'Parameter', 'manage_options', 'NAMESPACE-parameter-settings', 'NAMESPACE_display_parameter_options_page'); } endif; add_action('admin_menu', 'NAMESPACE_add_admin_menu_items'); if( ! function_exists('NAMESPACE_display_parameter_options_page') ): function NAMESPACE_display_parameter_options_page() { require_once 'parameter-options.php'; } endif;
plugindir/yourpluginname/parameter-options.php
$aPosts = $_POST; if( ! empty($aPosts)) { foreach($aPosts as $cKey => $aPost) { update_option($cKey, $aPost); } } <div class="wrap nosubsub"> <div class="icon32" id="icon-edit"><br></div> <h2><?php echo __('Hello, i am the optionspage', 'NAMESPACE'); ?></h2> <form method="post"> <table class="form-table"> <tbody> <?php $aYourOptionsArray = get_option('YourOptionsArray'); ?> <tr valign="top"> <th scope="row" colspan="2"><b><?php echo __('Some Description', 'NAMESPACE'); ?></b></th> </tr> <tr valign="top"> <th scope="row"><?php echo __('Value 1', 'NAMESPACE'); ?></th> <td><input name="YourOptionsArray[value_1]" class="regular-text" type="text" value="<?php echo (isset($aYourOptionsArray['value_1']) ? $aYourOptionsArray['value_1'] : ''); ?>" /></td> </tr> <tr valign="top"> <th scope="row"><?php echo __('Value 2', 'NAMESPACE'); ?></th> <td><input name="YourOptionsArray[value_2]" class="regular-text" type="text" value="<?php echo (isset($aYourOptionsArray['value_2']) ? $aYourOptionsArray['value_2'] : ''); ?>" /></td> </tr> <!-- more options here --> </table> </form> </div>