我的插件设置如下:
主插件文件mouldings.php
<?php
/*
Plugin Name: Moulding Profiles
Plugin URI:
Description: Creates a \'Moulding Profiles\' and \'Idea Gallery\' custom post type, creates a \'Wood Types\' and \'Categories\' taxonomy for \'Moulding Profiles\'
Version: 0.1
Author:
Author URI:
License: GPL2
*/
// Global Variables
$mouldings_prefix = \'mouldings_\';
$mouldings_name = \'Mouldings\';
// plugin folder url
if(!defined(\'MOULDINGS_BASE_URL\')) {
define(\'MOULDINGS_BASE_URL\', plugin_dir_url(__FILE__));
}
// plugin folder path
if(!defined(\'MOULDINGS_BASE_DIR\')) {
define(\'MOULDINGS_BASE_DIR\', plugin_dir_path( __FILE__ ));
}
// plugin root file
if(!defined(\'MOULDINGS_PLUGIN_FILE\')) {
define(\'MOULDINGS_PLUGIN_FILE\', __FILE__);
}
// Includes
include(\'includes/register-settings.php\'); // Register
// Retrieve settings from the options page
$mouldings_options = get_option(\'mouldings_settings\');
include(\'includes/install.php\'); // Sets default plugin settings on activation
include(\'includes/plugin-settings.php\'); // Plugin options page HTML/Save functions
include(\'includes/scripts.php\'); // Conrols JS/CSS
include(\'includes/admin-post-types.php\'); // Sets up post types/taxonomies
include(\'includes/helper-functions.php\'); // Functions that extend functionality of Mouldings
include(\'includes/admin-metaboxes.php\'); // Metaboxes that appear in the admin (non-ACF)
include(\'includes/display-functions.php\'); // Holds filters/actions for output
?>
在中
register_settings.php
我已设置选项:
<?php
function mouldings_register_settings_initial() {
if( false == get_option( \'mouldings_settings\' ) ) {
add_option( \'mouldings_settings\' );
}
register_setting(\'mouldings_settings_group\', \'idea_gallery_thumb_height\');
register_setting(\'mouldings_settings_group\', \'idea_gallery_thumb_width\');
register_setting(\'mouldings_settings_group\', \'collection_thumb_height\');
register_setting(\'mouldings_settings_group\', \'collection_thumb_width\');
register_setting(\'mouldings_settings_group\', \'profile_item_columns\');
register_setting(\'mouldings_settings_group\', \'idea_item_columns\');
register_setting(\'mouldings_settings_group\', \'collections_item_columns\');
register_setting(\'mouldings_settings_group\', \'combinations_item_columns\');
}
add_action(\'admin_init\', \'mouldings_register_settings_initial\');
然后进来
install.php
<?php
function mouldings_activate() {
global $wpdb, $mouldings_options;
if ($mouldings_options === false){
$mouldings_options = array(
\'idea_item_columns\' => \'4\'
);
update_option( \'mouldings_settings\', $mouldings_options );
}
}
register_activation_hook( MOULDINGS_PLUGIN_FILE, \'mouldings_activate\' );
所以,我要做的是设置选项表条目,
moulding_settings
, 注册设置,然后在激活时设置测试值,
idea_item_colummns
到
4
. 当前激活时,将创建选项表条目,但没有任何设置和默认值
idea_item_columns
也未设置/创建。一旦我访问了我的设置页面并保存了这些值,它们会根据需要弹出在该表中,但似乎没有单击激活。这是我的设置页面,如果有帮助的话:
<?php
function mouldings_options_page() {
global $mouldings_options;
ob_start(); ?>
<div class="wrap">
<h2><?php _e(\'Mouldings Options\',\'roots\'); ?></h2>
<form method="post" action="options.php" id="mouldings_options">
<?php settings_fields(\'mouldings_settings_group\'); ?>
<h4><?php _e(\'Image Sizes\',\'roots\'); ?></h4>
<p class="description"><?php _e(\'The options below are used to resize the various images that appear throughout Moulding Profiles, Idea Galleries, Moulding Combinations and Moulding Collections. If any values are left blank, they are assumed to be un-restricted. If you put anything other than a number in those boxes, I\\\'ll take out the screws in your office chair.\',\'roots\'); ?></p>
<p>
<label class="description" for="mouldings_settings[idea_gallery_thumb_height]"><?php _e(\'Idea Gallery Thumbnail Size\',\'roots\'); ?></label>
<input id="mouldings_settings[idea_gallery_thumb_height]" name="mouldings_settings[idea_gallery_thumb_height]" type="text" value="<?php echo (isset($mouldings_options[\'idea_gallery_thumb_height\'])) ? $mouldings_options[\'idea_gallery_thumb_height\'] : \'\'; ?>">
<span class="description"><?php _e(\'height (pixels)\',\'roots\'); ?></span>
<input id="mouldings_settings[idea_gallery_thumb_width]" name="mouldings_settings[idea_gallery_thumb_width]" type="text" value="<?php echo (isset($mouldings_options[\'idea_gallery_thumb_width\'])) ? $mouldings_options[\'idea_gallery_thumb_width\'] : \'\'; ?>">
<span class="description"><?php _e(\'width (pixels)\',\'roots\'); ?></span>
</p>
<p>
<label class="description" for="mouldings_settings[collection_thumb_height]"><?php _e(\'Moulding Collection Thumbnail Size\',\'roots\'); ?></label>
<input id="mouldings_settings[collection_thumb_height]" name="mouldings_settings[collection_thumb_height]" type="text" value="<?php echo (isset($mouldings_options[\'collection_thumb_height\'])) ? $mouldings_options[\'collection_thumb_height\'] : \'\'; ?>">
<span class="description"><?php _e(\'height (pixels)\',\'roots\'); ?></span>
<input id="mouldings_settings[collection_thumb_width]" name="mouldings_settings[collection_thumb_width]" type="text" value="<?php echo (isset($mouldings_options[\'collection_thumb_width\'])) ? $mouldings_options[\'collection_thumb_width\'] : \'\'; ?>">
<span class="description"><?php _e(\'width (pixels)\',\'roots\'); ?></span>
</p>
<h4><?php _e(\'Column Settings\',\'roots\'); ?></h4>
<p class="description"><?php _e(\'The options below are used to set a column count for each row.\',\'roots\'); ?></p>
<?php $item_columns = array(\'1\',\'2\',\'3\',\'4\',\'5\',\'6\',\'7\',\'8\',\'9\',\'10\'); ?>
<p>
<label class="description" for="mouldings_settings[profile_item_columns]"><?php _e(\'Moulding Profile Item Columns\',\'roots\'); ?></label>
<?php $profile_item_columns = $item_columns ?>
<select name="mouldings_settings[profile_item_columns]" id="mouldings_settings[profile_item_columns]">
<?php foreach($profile_item_columns as $profile_item_column) { ?>
<?php if($mouldings_options[\'profile_item_columns\'] == $profile_item_column) { $selected = \'selected="selected"\'; } else { $selected = \'\'; } ?>
<option value="<?php echo $profile_item_column; ?>" <?php echo $selected; ?>><?php echo $profile_item_column; ?></option>
<?php } ?>
</select>
</p>
<p>
<label class="description" for="mouldings_settings[idea_item_columns]"><?php _e(\'Idea Gallery Item Columns\',\'roots\'); ?></label>
<?php $idea_item_columns = $item_columns ?>
<select name="mouldings_settings[idea_item_columns]" id="mouldings_settings[idea_item_columns]">
<?php foreach($idea_item_columns as $idea_item_column) { ?>
<?php if($mouldings_options[\'idea_item_columns\'] == $idea_item_column) { $selected = \'selected="selected"\'; } else { $selected = \'\'; } ?>
<option value="<?php echo $idea_item_column; ?>" <?php echo $selected; ?>><?php echo $idea_item_column; ?></option>
<?php } ?>
</select>
</p>
<p>
<label class="description" for="mouldings_settings[collections_item_columns]"><?php _e(\'Moulding Collections Item Columns\',\'roots\'); ?></label>
<?php $collections_item_columns = $item_columns ?>
<select name="mouldings_settings[collections_item_columns]" id="mouldings_settings[collections_item_columns]">
<?php foreach($collections_item_columns as $collections_item_column) { ?>
<?php if($mouldings_options[\'collections_item_columns\'] == $collections_item_column) { $selected = \'selected="selected"\'; } else { $selected = \'\'; } ?>
<option value="<?php echo $collections_item_column; ?>" <?php echo $selected; ?>><?php echo $collections_item_column; ?></option>
<?php } ?>
</select>
</p>
<p>
<label class="description" for="mouldings_settings[combinations_item_columns]"><?php _e(\'Moulding Combinations Item Columns\',\'roots\'); ?></label>
<?php $combinations_item_columns = $item_columns ?>
<select name="mouldings_settings[combinations_item_columns]" id="mouldings_settings[combinations_item_columns]">
<?php foreach($combinations_item_columns as $combinations_item_column) { ?>
<?php if($mouldings_options[\'combinations_item_columns\'] == $combinations_item_column) { $selected = \'selected="selected"\'; } else { $selected = \'\'; } ?>
<option value="<?php echo $combinations_item_column; ?>" <?php echo $selected; ?>><?php echo $combinations_item_column; ?></option>
<?php } ?>
</select>
</p>
<p class="submit">
<input type="submit" class="button-primary" value="<?php _e(\'Save Options\',\'roots\'); ?>">
</p>
<?php
if(function_exists(\'RegenerateThumbnails\')) {
ob_start(); ?>
<div class="updated">
<p><strong>Notice:</strong> If you are altering the thumbnail sizes, remember to <a href="<?php echo admin_url(\'tools.php?page=regenerate-thumbnails\'); ?>">Regenerate Thumbnails</a></p>
</div>
<?php echo ob_get_clean();
}
else {
ob_start(); ?>
<div class="error">
<p><strong>Notice:</strong> You do not have <a href="<?php echo admin_url(\'plugin-install.php?tab=search&type=term&s=regenerate+thumbnails&plugin-search-input=Search+Plugins\'); ?>">Regenerate Thumbnails</a> installed. It is recommended to use this plugin - as this is the tool used to re-sample your images based on your preferences.</p>
</div>
<?php echo ob_get_clean();
}
?>
</form>
</div>
<?php
echo ob_get_clean();
}
function mouldings_add_options_link() {
add_options_page(__(\'Mouldings Options\',\'roots\'),__(\'Mouldings\',\'roots\'),\'manage_options\',\'mouldings-options\',\'mouldings_options_page\');
}
add_action(\'admin_menu\',\'mouldings_add_options_link\');
function mouldings_register_settings() {
register_setting(\'mouldings_settings_group\',\'mouldings_settings\');
}
add_action(\'admin_init\',\'mouldings_register_settings\');
?>
我已经做了几个小时了,我希望这是一件小事(好吧,如果太小了,我可能会失去理智,但任何帮助都将不胜感激)。谢谢