it-swarm.com.de

Standardeinstellungen des Plugins, die nicht in die Datenbank schreiben

Ich habe mein Plugin als solches eingerichtet:

Haupt-Plugin-Datei 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 habe ich die Optionen eingerichtet:

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

und dann 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' );

Ich versuche also, den Optionstabelleneintrag moulding_settings einzurichten, die Einstellungen zu registrieren und bei der Aktivierung den Testwert idea_item_colummns auf 4 zu setzen. Bei der Aktivierung wird derzeit der Optionstabelleneintrag erstellt, jedoch ohne Einstellungen, und der Standardcode idea_item_columns wird ebenfalls nicht festgelegt/erstellt. Sobald ich meine Einstellungsseite besuche und die Werte speichere, erscheinen sie nach Bedarf in dieser Tabelle - aber etwas scheint nicht auf Aktivierung zu klicken. Hier ist meine Einstellungsseite, wenn das überhaupt hilft:

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

?>

Ich bin schon seit ein paar Stunden dabei und hoffe, dass es etwas Kleines ist (na ja, wenn es zu klein ist, kann ich meinen Verstand verlieren, aber jede Hilfe wäre wirklich dankbar). Vielen Dank.

1
Zach

Ich glaube, das Problem ist, dass Ihre Funktion, mit der die Einstellungen anfänglich vorgenommen werden, bei der Aktivierung nicht ausgeführt wird. Fügen Sie Ihrem mouldings_register_settings_initial () im Aktivierungs-Hook einen Aufruf wie folgt hinzu:

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