2017-08-07 34 views
0

我需要创建一个插件来将自定义值保存到options.phpWordpress动态选项页面从数组中实际保存?

我想创建数组中的字段,并工作。

当我迷路是这样的:

  1. 形式实际上不会保存在数据库WP的设置。
  2. 如何创建其他部分?

我知道它与add_settings_section()等,但我找不到如何做这与阵列输入。请指教。 ;-)

我迄今为止代码:

<?php 
// Create WordPress admin menu 
function stonehenge_menu_page(){ 
    $page_title = 'Stonehenge Options'; 
    $menu_title = 'Stonehenge Options'; 
    $capability = 'manage_options'; 
    $menu_slug = 'stonehenge_slug'; 
    $function = 'stonehenge_render_page'; 
    $icon_url = 'dashicons-admin-plugins'; 
    $position = 99; 

add_menu_page($page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $position); 

// Call update_stonehenge function to update database 
add_action('admin_init', 'update_stonehenge'); 
} 

// Create function to register plugin settings in the database 
function update_stonehenge() { 
    register_setting('stonehenge-settings', 'stonehenge'); 
} 

function stonehenge_render_page() { 

$section_1 = array(
    'Field 1' => array(   // $name Name of the field 
     'field' => 'field_1',  // $field 
     'label' => 'Field 1',  // $label Label of the field 
     'type' => 'text',   // $type input type (text, textarea, option, etc.)  
    ), 
    'Field 2' => array(   // $field Name of the field 
     'field' => 'field_2',  // $field 
     'label' => 'Field 2',  // $label Label of the field 
     'type' => 'text',   // $type input type (text, textarea, option, etc.)  
    ), 
    'Field 3' => array(   // $field Name of the field 
     'field' => 'field_3',   // $field 
     'label' => 'Field 3',  // $label Label of the field 
     'type' => 'text',   // $type input type (text, textarea, option, etc.)  
    ), 
); 

## START OF MAIN PAGE 
echo '<h1>Stonehenge Options Fields</h1>'; 
?> 
<form method="post" action="options.php"> 
<?php settings_fields('stonehenge-settings'); ?> 
<?php do_settings_sections('stonehenge-settings'); ?> 

<!-- >## SECTION 1 should start here </!--> 
<br><h2>Section 1</h2> 
<table> 
<?php 
foreach ($section_1 as $var) { 
$prefix   = 'stonehenge_';    
$option_label = $var['label']; 
$option_name = $var['field']; 
$option_field = 'stonehenge_'.$var['field']; 
$option_value = get_option($option_field); 
$option_input = '<input type="' .$var['type']. '"name="'.$option_field.'" id="'.$option_field.'" value="'.$option_value.'"> '; 
$label_for  = '<label for='.$field.'>' . $var['label'] . '</label>'; 
?> 
    <tr><th scope="row" align="left"><?php echo $option_label; ?></th> 
     <td><?php echo $option_input; ?></td> 
     <td>Database Field = <?php echo $option_field; ?></td> 
    </tr> 
<?php } ?> 
</table> 

<!-- >## SECTION 2 should start here </!--> 
<br><h2>Section 2</h2> 
<table> 
</table> 

<?php submit_button(); ?> 
</form> 
<?php 
} 

回答

0

解决了!

原来,这很简单,如果做得对。 :-)现在我可以只添加一个数组,其余所有是动态创建的。呐喊!呐喊!

输入存储在options.php中作为stonehenge_section_fieldname,cal可以通过get_option('stonehenge_section_fieldname')轻松调用;

这是迄今为止的代码,如果有人感兴趣。

// Define common constants for stabiliy. 
if (!defined("STONEHENGE_PLUGIN_VERSION"))  define("STONEHENGE_PLUGIN_VERSION", "2.0.0"); 
if (!defined("STONEHENGE_PLUGIN_DIR_PATH"))  define("STONEHENGE_PLUGIN_DIR_PATH", plugins_url('' , __FILE__)); 

// TO DO: Add MultiSite compatibility for network Activation. 
// Current version only works if activated per blog. 

// Let's load the language files. 
load_plugin_textdomain('stonehenge', false, basename(dirname(__FILE__)) . '/languages/'); 

Class Stonehenge_Fields { 
    // Add additional Arrays to dynamically create more inputs. That's it! 
    static $stonehenge_fields = array(
     'Facebook' => array(   
      'Page URL' => 'text',     
      'App ID' => 'text',  
      'Admin ID' => 'text',    
     ), 
/*  'Section' => array(   
      'Field 1' => 'text',     
      'Field 2' => 'textarea',  
      'Field 3' => 'checkbox',    
     ), 
*/ 
    ); 

} 

// TO DO: Add a hook to remove all option fields upon deactivation of the Plugin. 
//delete_option('stonehenge_website_name'); 


// Call stonehenge_menu function to load Admin Menu in the Dashboard. 
add_action('admin_menu', 'stonehenge_menu_page'); 

// Let's create WordPress Admin Menu 
function stonehenge_menu_page(){ 
    $page_title = __('Stonehenge Custom Fields & Data Settings', 'stonehenge'); 
    $menu_title = __('Stonehenge Fields', 'stonehenge'); 
    $capability = 'manage_options'; 
    $menu_slug = 'stonehenge'; 
    $function = 'stonehenge_options_page'; 
    $icon_url = 'dashicons-admin-plugins'; 
    $position = 99; 
    add_menu_page($page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $position); 
} 

function stonehenge_register_settings() { 
// Let's create and register the Sections. 
    $sections = Stonehenge_Fields::$stonehenge_fields; 
    foreach ($sections as $section => $key) { 
     add_settings_section($section, $section, 'stonehenge_callback', __FILE__); 

// Let's create and register the input fields in options.php (acutal input fields are created in function stonehenge_options_page.) 
     foreach ($key as $name => $type) { 
      $prefix  = 'stonehenge';   
      $field  = str_replace(' ', '_', strtolower($prefix.'_'.$section.'_'.$name)); 
      $label  = ucfirst($section.' '.$name); 
      $value  = get_option($prefix.'_'.$section.'_'.$name); 
      $input  = '<input type="' .$type. '"name="'.$field.'" id="'.$field.'" value="'.$value.'"> '; 
      $label_for  = '<label for='.$field.'>' . $label . '</label>'; 
      add_option($field, $value); 
      register_setting('stonehenge_options_group',$field, 'stonehenge_callback'); 
     } 
    } 
} 
add_action('admin_init', 'stonehenge_register_settings'); 

// Let's create the Main Page. 
function stonehenge_options_page() 
{ 
?> 
    <div class="wrap"> 
    <h1><?php _e('Stonehenge Custom Fields & Data Settings', 'stonehenge'); ?></h1> 
    <form method="post" action="options.php"> 
    <?php settings_fields('stonehenge_options_group'); ?> 
    <table cellspacing="4" cellpadding"3"> 

    <?php 
//Let's create the Sections 
    $sections = Stonehenge_Fields::$stonehenge_fields; 
    foreach ($sections as $section => $key) { 
     $section_label = ucfirst($section); 
     echo ('<th scope="row" colspan="2" align="left"><br><h2>'.$section_label.' '.__('Settings','stonehenge').'</h2></th>'); 

// Let's create the actual Labels and input Fields 
     foreach ($key as $name => $type) { 
      $prefix  = 'stonehenge';   
      $field  = str_replace(' ', '_', strtolower($prefix.'_'.$section.'_'.$name)); 
      $label  = ucfirst($section.' '.$name); 
      $value  = get_option($field); 
// TO DO: Create switch arguments for different input types, such as Checkbox, Radio, Textarea, Password, etc. 
      $input  = '<input type="' .$type. '"name="'.$field.'" id="'.$field.'" value="'.$value.'"> '; 
// Using <label for> Makes it clickable. Very handy! 
      $label_for  = '<label for='.$field.'>' . $label . '</label>'; 
      ?><tr><th scope="row" align="left"><?php echo $label_for; ?></th><td><?php echo $input; ?></td></tr><?php 
     } 
    }?> 
    </table> 
    <?php submit_button(); ?> 
    </form> 
    </div> 
<?php 
} 

// That's all Folks! :-) 

?>