Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have my plugin setup as such:

Main plugin file 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

?>

In register_settings.php I have setup the options:

<?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');

and then in 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' );

So, what I'm trying to do is setup the options table entry, moulding_settings, register the settings and then on activation, set the test value, idea_item_colummns to 4. On activation currently, the options table entry is created, but without any settings and the default idea_item_columns is not set/created either. Once I visit my settings page and save the values, they pop-up in that table as need - but something just doesn't seem to be clicking on activation. Here is my settings page if that helps at all:

<?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');

?>

I've been at this for a few hours and am hoping it's something small (well, if it's too small, I may loose my sanity, but any help would really be appreciated). Thanks.

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

I believe the issue is that your function that sets up the settings initially isn't getting run upon activation. Add a call to your mouldings_register_settings_initial() in the activation hook, like this:

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' );
share|improve this answer
    
Hey @pippin really appreciate the answer, but sadly, no dice. The register_setting should be set to the settings group, mouldings_settings_group right (that is first called in plugin-settings.php)? –  Zach Apr 23 '12 at 20:52
    
Just realized what it is. Your mouldings_register_settings_initial() function is incorrect. register_setting() should only be called once, like this: function mouldings_register_settings_initial() { if( false == get_option( 'mouldings_settings' ) ) { add_option( 'mouldings_settings' ); } register_setting('mouldings_settings_group', 'mouldings_settings'); } add_action('admin_init', 'mouldings_register_settings_initial'); –  Pippin Apr 23 '12 at 21:27
    
Here is a pastebin that is easier to read: pastebin.com/gth5maSQ –  Pippin Apr 23 '12 at 21:28
    
Hey @pippin appreciate you hanging with me on this - that doesn't seem to be doing the trick either -- I also tried taking out that first snippet you suggested without any avail. Is it possibly "where" I am setting up those setting fields? I followed your "writing your first plugin" series which sets up the options a little differently than what I referenced in EDD - could that be it? Thanks. –  Zach Apr 23 '12 at 22:14
1  
In mouldings_activate(), replace if ($mouldings_options === false){ with if( false == get_option( 'mouldings_settings' ) ) { and then remove mouldings_register_settings_initial() completely. –  Pippin Apr 24 '12 at 2:15
show 5 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.