在WordPress插件中插入Customize_Register的位置

时间:2015-01-26 作者:IamLasse

我正在开发一个插件。。。嗯,试图。。。用于在主题中添加视差标题的wordpress。我想为我的插件创建一个customizer部分,但无论我在哪里尝试添加customize\\u寄存器来调用我的类方法,都不会向面板中添加任何内容。任何人都有这方面的经验,可以分享一些技巧吗?

Update:

public function __construct(){
   add_action(\'customize_register\', array(&$this, \'register_customizer\'), 30, 1 );
}


public static function register_customizer($wp_customize){
   $wp_customize->add_section(
       \'my_section\', ....
   );
}
我只是使用官方文档中的Wp customizer代码作为测试,但似乎无法使其正常工作。。。

UPDATE

    <?php
/*
Plugin Name: WC Storefront Stellar
Plugin URI: 
Description: 
Version: 0.1
Author: 
Author Email: [email protected]
License:

  Copyright 2011  ([email protected])

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License, version 2, as 
  published by the Free Software Foundation.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

*/

  class StorefrontStellar {

    /*--------------------------------------------*
     * Constants
     *--------------------------------------------*/
    const name = \'Storefront Stellar\';
    const slug = \'storefront_stellar\';

    /**
     * Constructor
     */
    function __construct() {
        //register an activation hook for the plugin
        register_activation_hook( __FILE__, array( &$this, \'install_storefront_stellar\' ) );

        //Hook up to the init action
        add_action( \'plugins_loaded\', array( &$this, \'init_storefront_stellar\' ) );
    }

    /**
     * Runs when the plugin is activated
     */  
    function install_storefront_stellar() {
        // do not generate any output here
    }

    /**
     * Runs when the plugin is initialized
     */
    function init_storefront_stellar() {
        // Setup localization
        load_plugin_textdomain( self::slug, false, dirname( plugin_basename( __FILE__ ) ) . \'/lang\' );
        // Load JavaScript and stylesheets
        $this->register_scripts_and_styles();

        // Register the shortcode [stellar_header]
        add_shortcode( \'stellar_header\', array( &$this, \'render_shortcode\' ) );

        if ( is_admin() ) {
            //this will run when in the WordPress admin
            add_action( \'customize_register\', array( &$this, \'action_register_customizer\' ), 30, 1 );
        } else {
            //this will run when on the frontend
        }

        /*
         * TODO: Define custom functionality for your plugin here
         *
         * For more information: 
         * http://codex.wordpress.org/Plugin_API#Hooks.2C_Actions_and_Filters
         */
        add_action( \'customize_register\', array( &$this, \'action_register_customizer\' ), 30, 1 );
        // add_filter( \'your_filter_here\', array( &$this, \'filter_callback_method_name\' ) );    
    }

    public function action_register_customizer($wp_customize) {
        // TODO define your action method here

        $wp_customize->add_section(
            \'my_section\',
            array(
                \'title\'       => \'My Section\',
                \'priority\'    => 30,
                )
            );


    }

    function filter_callback_method_name() {
        // TODO define your filter method here
    }

    function render_shortcode($atts) {
        // Extract the attributes
        extract(shortcode_atts(array(
            \'attr1\' => \'foo\', //foo is a default value
            \'attr2\' => \'bar\'
            ), $atts));
        // you can now access the attribute values using $attr1 and $attr2
    }

    /**
     * Registers and enqueues stylesheets for the administration panel and the
     * public facing site.
     */
    private function register_scripts_and_styles() {
        if ( is_admin() ) {
            $this->load_file( self::slug . \'-admin-script\', \'/js/admin.js\', true );
            $this->load_file( self::slug . \'-admin-style\', \'/css/admin.css\' );
        } else {
            $this->load_file( self::slug . \'-script\', \'/js/widget.js\', true );
            $this->load_file( self::slug . \'-style\', \'/css/widget.css\' );
        } // end if/else
    } // end register_scripts_and_styles

    /**
     * Helper function for registering and enqueueing scripts and styles.
     *
     * @name    The     ID to register with WordPress
     * @file_path       The path to the actual file
     * @is_script       Optional argument for if the incoming file_path is a JavaScript source file.
     */
    private function load_file( $name, $file_path, $is_script = false ) {

        $url = plugins_url($file_path, __FILE__);
        $file = plugin_dir_path(__FILE__) . $file_path;

        if( file_exists( $file ) ) {
            if( $is_script ) {
                wp_register_script( $name, $url, array(\'jquery\') ); //depends on jquery
                wp_enqueue_script( $name );
            } else {
                wp_register_style( $name, $url );
                wp_enqueue_style( $name );
            } // end if
        } // end if

    } // end load_file

} // end class
new StorefrontStellar();

?>
update我终于成功了。给任何想知道的人。定制必须包含所有三个部分;部分,设置和控制以便工作。目前正在从构造调用add\\u操作,如果有其他方法,请共享。谢谢你的时间和帮助。

1 个回复
最合适的回答,由SO网友:realloc 整理而成

您可以使用customize\\u寄存器action 像这样:

function my_customize_register( $wp_customize ) {
    /* Just use the $wp_customize object and create a section or use a built-in
       section. */
    $wp_customize->add_section(
        \'my_section\',
        array(
            \'title\'       => \'My Section\',
            \'priority\'    => 30,
        )
    );
    /* Now you can add your settings ... */
    $wp_customize->add_setting(
        \'my_options[my_first_option]\',
        array(
            \'default\'    => \'\',
            \'type\'       => \'option\',
            \'capability\' => \'edit_theme_options\',
        )
    );
    /* ... and link controls to these settings. */
    $wp_customize->add_control(
        \'my_first_option\',
        array(
            \'label\'      => \'My First Option\',
            \'section\'    => \'my_section\',
            \'settings\'   => \'my_options[my_first_option]\',
        )
    );

}
add_action( \'customize_register\' , \'my_customize_register\' );
您可以使用内置节或定义自定义节。根据需要定义设置并将控件链接到这些设置。

页面Theme Customization API 有很多有用的代码。。。但是,因为你要求提示:始终设置WP_DEBUG 当您为WordPress开发代码时。您经常会立即看到某些东西没有按预期工作的原因。

结束

相关推荐

必须使用插件自动加载器:如何正确使用get_plugins()?

我的autoloader类负责加载必须使用的插件,这些插件不位于mu-plugins 文件夹要定位它们,我需要使用get_plugins() 作用According to Codex, 该函数接受一个参数:$plugin\\u folder(string)(可选):单个插件文件夹的相对路径。我的文件层次结构如下所示:|-- /mu-plugins | |-- autoload.php // only includes wpmu/autoload.php&#