customizer-library icon indicating copy to clipboard operation
customizer-library copied to clipboard

Hook Additional Options onto Action

Open gasatrya opened this issue 10 years ago • 7 comments

I'm sorry about title, I have no idea what is the best title for my question.

I'm trying to split the customizer option into seperated files, so I tried to added 'hook' in the middle of code, somethin like this

function standard_customizer_register() {

    // Stores all the controls that will be added
    $options = array();

    // Stores all the sections to be added
    $sections = array();

    // Stores all the panels to be added
    $panels = array();

    // Adds the sections to the $options array
    $options['sections'] = $sections;

    // Hook to add the custom customizer.
    do_action( 'standard_register_customizer', $options, $sections, $panels );

    // Adds the sections to the $options array
    $options['sections'] = $sections;

    // Adds the panels to the $options array
    $options['panels'] = $panels;

    $customizer_library = Customizer_Library::Instance();
    $customizer_library->add_options( $options );

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

then I called the file and hook in to add the panels/sections.

$path = trailingslashit( get_template_directory() ) . 'admin/panels/';
require_once( $path . 'general.php' );

general.php

function standard_customizer_general_panel( $sections, $panels, $options) {

    // Panel ID
    $panel = 'panel';

    $panels[] = array(
        'id' => $panel,
        'title' => __( 'General', 'Standard' ),
        'priority' => 10
    );

    // Logo
    $section = 'logo';

    $sections[] = array(
        'id' => $section,
        'title' => __( 'Image Example', 'demo' ),
        'priority' => '30',
        'description' => __( 'Example section description.', 'demo' ),
        'panel' => $panel
    );
    $options['logo'] = array(
        'id' => 'logo',
        'label'   => __( 'Logo', 'demo' ),
        'section' => $section,
        'type'    => 'image',
        'default' => ''
    );

}
add_action( 'standard_register_customizer', 'standard_customizer_general_panel', 10, 3 );

But it didn't worked. My question:

  1. Is it possible to split the options files?
  2. Am I missing something in the code?

Thank you.

gasatrya avatar May 20 '15 06:05 gasatrya

Options can just be added altogether in one call. I'm sure it's possible to rework this code a bit so multiple can be added, but I'd need to spend some time with it.

devinsays avatar May 20 '15 18:05 devinsays

I see, thank you @devinsays

gasatrya avatar May 21 '15 03:05 gasatrya

Let's leave this issue open. I'll take a look next time I need to make some updates to this codebase.

devinsays avatar May 21 '15 15:05 devinsays

@satrya Thanks for posting this and for your pull request. Were you able to make any progress with this request? I'd like to add additional theme options via a plugin.

Misplon avatar Dec 19 '16 14:12 Misplon

I'm not sure this project is maintained anymore, but here is what I have done in the past: Add an filter and then hook into it in your child theme or plugin. $options['panels'] = $panels; // Hook to add the custom customizer in child theme. $options= apply_filters( 'yourplugin_add_customizer_child_options' , $options ); $customizer_library = Customizer_Library::Instance();

Then use the filter:

` function child_customizer_library_gateway_plus_options( $options ) { // Add some code to set up your sections and panels, then add them below $options['sections'] = array_merge( $custom_sections, $options['sections'] ); $options['panels'] = array_merge( $custom_panels, $options['panels'] );

return $options; } add_action('yourplugin_add_customizer_child_options','child_customizer_library_gateway_plus_options';

`

RescueThemes avatar Dec 19 '16 17:12 RescueThemes

@Misplon I am sorry I am not using this library anymore, but I have tried by calling the file directly and it was worked, something like below

function standard_customizer_register() {

    // Stores all the controls that will be added
    $options = array();

    // Stores all the sections to be added
    $sections = array();

    // Stores all the panels to be added
    $panels = array();

    // Adds the sections to the $options array
    $options['sections'] = $sections;

    // Call the customizer settings/controls
    require_once( $path . 'general.php' );

    // Adds the sections to the $options array
    $options['sections'] = $sections;

    // Adds the panels to the $options array
    $options['panels'] = $panels;

    $customizer_library = Customizer_Library::Instance();
    $customizer_library->add_options( $options );

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

gasatrya avatar Dec 20 '16 03:12 gasatrya

Thanks guys, I really appreciate your replies.

@RescueThemes @satrya

Misplon avatar Dec 20 '16 09:12 Misplon