web-dev-qa-db-fra.com

Paramètres de plug-in par défaut non écrits dans la base de données

J'ai la configuration de mon plugin en tant que telle:

Fichier du plugin principal 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

?>

Dans register_settings.php j'ai configuré les 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');

puis dans 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' );

Donc, ce que j'essaie de faire, c'est de configurer l'entrée de la table d'options, moulding_settings, d'enregistrer les paramètres, puis lors de l'activation, de définir la valeur de test, idea_item_colummns sur 4. Lors de l'activation actuelle, l'entrée de la table d'options est créée, mais sans aucun paramètre et le idea_item_columns par défaut n'est pas défini/créé non plus. Une fois que je visite ma page de paramètres et que j'enregistre les valeurs, elles apparaissent dans cette table selon les besoins - mais quelque chose ne semble tout simplement pas cliquer sur l'activation. Voici ma page de paramètres si cela peut aider:

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

?>

J'y travaille depuis quelques heures et j'espère que c'est quelque chose de petit (enfin, si c'est trop petit, je risque de perdre ma santé mentale, mais toute aide serait vraiment appréciée). Merci.

1
Zach

Je pense que le problème est que votre fonction qui configure les paramètres initialement ne s'exécute pas à l'activation. Ajoutez un appel à votre mouldings_register_settings_initial () dans le hook d'activation, comme ceci:

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' );
3
Pippin