简体   繁体   中英

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. 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. I also need to create that Options form using custom HTML, and not have it generated by the Settings APIs.

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() . The <form> tag then has an attribute 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. 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 adds a new page called "Your options-pagename" to wordpress the settings tab. 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. In addition : This script is non-tested.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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