最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

configuration - Default plugin config to override wp_options?

programmeradmin0浏览0评论

I have a few plugins that I always set the configuration for exactly the same way. Every time I create a new site, the same plugins go in, and the same amount of time is needed to set them up.

Would it be possible to create a config file that overrides whatever wp_options are set per plugin?

I have a few plugins that I always set the configuration for exactly the same way. Every time I create a new site, the same plugins go in, and the same amount of time is needed to set them up.

Would it be possible to create a config file that overrides whatever wp_options are set per plugin?

Share Improve this question asked Mar 22, 2011 at 17:06 Dan GayleDan Gayle 6,1665 gold badges33 silver badges45 bronze badges 2
  • 1 Would you mind explaining a bit more, are you referring to options created by plugins and stored in the options table? – t31os Commented Mar 22, 2011 at 17:11
  • Yes. Preferably without need of the DB, but I don't think that can be accomplished since all the plugins are calling get_option. – Dan Gayle Commented Mar 29, 2011 at 16:02
Add a comment  | 

4 Answers 4

Reset to default 1

You could check which options they add (look at the source code) and then simply write a function like this:

/*
Plugin Name:    Mother of all plugins
Plugin URI:  http://wordpress.org/extend/plugins/
Description:    Offers the <code>$all_plugin_options;</code> var to access all predefined plugin options
Author:      Franz Josef Kaiser
Author URI:     http://say-hello-code.com
Version:        0.1
License:        GPL v2 - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*/

// Template Tag
function get_all_plugin_options() 
{
    // First we call the class
    $class = new MotherOfAllPlugins;
    $data = $class->predefined_plugin_options()
    return $data;
}

if ( ! class_exists('MotherOfAllPlugins') )
{

class MotherOfAllPlugins
{
    protected $plugin_options;

    public function __construct( $plugin_options )
    {
      // defaults
      $default_options = array(
         'plugin_a' => array(
             'deprecated' => ''
            ,'name'       => 'value'
            ,'key'        => 'value'
          )
        ,'plugin_b' => array(
             'deprecated' => ''
            ,'name'       => 'value'
            ,'key'        => 'value'
          )
      );
      // Now overwrite the 
      $this->plugin_options = array_merge( $default_options, $plugin_options );

      add_action( 'init', 'predefined_plugin_options' );
    }

    function predefined_plugin_options() 
    {

      // Set the flag if we have already done this
      // _EDIT #1:_ This sets an option in the wp_options table containing TRUE if your plugin predef options are already present in the DB
      if ( !get_option( 'predef_plugins_setup' ) === TRUE )
         add_option( 'predef_plugins_setup', TRUE );

      if ( !get_option( 'predef_plugins_setup' ) === TRUE )
      {
         // Add the options for the plugins
         foreach ( $plugin_options as $plugin => $options ) 
         {
            add_option( $plugin, $options, $options['deprecated'], 'yes' );
         }
      }

      // _EDIT #2:_ return the initial array for use in a global
      return $plugin_options
    }

} // END Class MotherOfAllPlugins

} // endif;

To get your plugin options inside your theme:

// Now we take the return value & add it into global scope for further useage.
// This way we can access all options easily without a call to the DB.
// You can now access these values from anywhere in your theme.
$all_plugin_options = get_all_plugin_options();

Be careful to really add the options exactly the way the plugins does it. Else stuff won't work.

You could put something like this in your theme's function.php. This way it only runs once (when the theme is activated).

add_option('my_initial_options', false);

if ( get_option('my_initial_options')== false ){
    addmyOptions();
    update_option( 'my_initial_options', true,'','yes' );
}

function addmyOptions(){

update_option("posts_per_page",1);
update_option( 'show_on_front', 'page' );

}

Not sure if something like that is possible but a workaround would be to use a dummy database of default WP install configured with all those plugins. Now whenever you need to setup another site, put in this database and edit the home url and site url.

Take a look at the pre_option_{option} hook. https://developer.wordpress.org/reference/hooks/pre_option_option/

<?php
// Force the blogname (Website Title) no matter what Settings > General says
function myslug_pre_filter_blogname( $pre_option ) {
    return 'My Awesome Website';
}
add_filter( 'pre_option_blogname', 'myslug_pre_filter_blogname' );

// Your site is special. It's not "Just Another" anything.
function myslug_pre_filter_blogdescription( $pre_option ) {
    return '';
}
add_filter( 'pre_option_blogdescription', 'myslug_pre_filter_blogdescription' );

发布评论

评论列表(0)

  1. 暂无评论