0

I'm developing a plugin... well trying to... for wordpress that adds a parallax header in a theme. I want to have a customizer section for my plugin, but where ever I try to add the customize_register to call my class method nothing gets added to the panel. Anyone have experience with this, and can share some tips?

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', ....
   );
}

I'm just using the Wp customizer code from the official documentation as a test, but can't seem to get it to work...

UPDATE

    <?php
/*
Plugin Name: WC Storefront Stellar
Plugin URI: 
Description: 
Version: 0.1
Author: 
Author Email: me@home.com
License:

  Copyright 2011  (me@home.com)

  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 I finally got it working. For anyone else wondering. The customize has to have all three parts; section, setting and control in order to work. Calling the add_action from the construct at the moment, if anyone has other ways to do it please share. Thanks for your time and help.

  • 1
    how are you adding this action? any code please – Reigel Jan 26 '15 at 1:51
  • I've tried several ways from a boilerplate plugin construct function and from and from an init function within the plugin class even just from the template part without using the class and nothing registered... – IamLasse Jan 27 '15 at 17:39
0

You can use the customize_register action like this:

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

You can use built-in sections or define your custom ones. Define your settings as you like and link controls to them.

The page Theme Customization API has a lot of useful code ... but, because you asked for tips: Always set WP_DEBUG when you develop code for WordPress. You'll see very often immediately the reason why something is not working as expected.

  • Could you show it from within a plugin class? So the call would be add_action('customize_register', array(&$this, 'my_customize_register')); maybe from a __construct – IamLasse Jan 27 '15 at 17:36
  • if do a var_dump within my public function I do have access to $wp_customize, but nothing gets added, and WP_DEBUG does'nt display any errors or warnings... – IamLasse Jan 27 '15 at 22:17
  • This was the closest answer, since I copied all three parts that's what got it working. – IamLasse Jan 28 '15 at 17:35
  • No problem. You can use it like that inside a constructor: add_action('customize_register', array( $this, 'my_customize_register') ); if my_customize_register is a callable method. – realloc Jan 28 '15 at 18:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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