私は自分のプラグインを設定しています。
メインプラグインファイル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');
?>
私はこれを数時間続けていて、それが小さいものであることを願っています(それが小さすぎるなら、私は正気を失うかもしれませんが、どんな助けでも本当に感謝されるでしょう)。ありがとう。
問題は、最初に設定を設定する機能が起動時に実行されないことです。次のように、アクティベーションフックにmouldings_register_settings_initial()への呼び出しを追加します。
function mouldings_activate() {
global $wpdb, $mouldings_options;
mouldings_register_settings_initial();
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' );