简体   繁体   English

WordPress自定义选项页面

[英]WordPress custom options page

I need to create an options page in WordPress that allows for multiple values to be entered for the same key. 我需要在WordPress中创建一个选项页面,该页面允许为同一键输入多个值。 For it to work the way I want it to, I need to do some custom processing on the $_POST data sent by the options form. 为了使其按我想要的方式工作,我需要对选项表单发送的$ _POST数据进行一些自定义处理。 I also need to create that Options form using custom HTML, and not have it generated by the Settings APIs. 我还需要使用自定义HTML创建该“选项”表单,并且不要让Settings API生成该表单。

The normal procedure for making an options page is registering the Options Page using add_options_page() , registering the settings and letting WordPress generate a form for you using do_settings_sections() . 制作选项页面的正常过程是使用add_options_page()注册选项页面,注册设置并让WordPress使用do_settings_sections()为您生成表单。 The <form> tag then has an attribute action="options.php" . 然后, <form>标记具有属性action="options.php"

My question is: can I change the way this works? 我的问题是:我可以更改其工作方式吗? For example, I could have the callback looking like this: 例如,我可以使回调看起来像这样:

<?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
}

Would this work? 这行得通吗? Is this an acceptable way of achieving what I want? 这是实现我想要的一种可接受的方式吗? If not, how else could I do it? 如果没有,我还能怎么做? I wasn't able to find a "legal" way of doing this in the WordPress documentation. 我无法在WordPress文档中找到执行此操作的“合法”方法。 Maybe one of you has found it. 也许你们其中之一找到了它。

I don't know if this is really the answer for what you are looking for. 我不知道这是否真的是您要寻找的答案。 This script creates a plugin index.php and parameter-options.php . 该脚本创建一个插件index.php和parameter-options.php。 index.php adds a new page called "Your options-pagename" to wordpress the settings tab. index.php添加了一个名为“您的选项-页面名称”的新页面以用鼠标点击设置选项卡。 There you can manage options by adding new input values to the form. 您可以在此处通过向表单添加新的输入值来管理选项。 You should replace the uppercase NAMESPACE text with your own project-name. 您应该使用自己的项目名称替换大写的NAMESPACE文本。 In addition : This script is non-tested. 另外:此脚本未经测试。

plugindir/yourpluginname/index.php 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 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM