• In my toying around with WordPress I decided I’d give plugins a try. Everything’s going pretty good, but I’ve run into one hangup.

    I was more or less following this: https://www.youtube.com/watch?v=7usuZRBsyk8

    (Note: I am not using Woo Commerce as he is)

    In an attempt to create a plugin settings page within the customizer.

    Basically, in the video he adds a button to a settings page that opens a customizer instance to customize something about his plugin.

    My hangup comes with the section where he has us add a query arg to the url…

    $url = add_query_arg(
        'wc-email-customizer',
        'true',
        $url
    );

    And then says to hide the default customizer settings on THIS page only, not the default customizer paged accessed through the appearance menu… using this code.

    
    add_filter( 'customize_control_active', 'control_filter', 10, 2 );
    
    function control_filter( $active, $control ) {
        if ( in_array(
            $control->section,
            array(
                'wc_email_header',
                'wc_email_body',
                'wc_email_footer',
                'wc_email_send'
                )
            )
        ){
        return true;
    
    } 
    return false;
    
    }

    He states in the video “And what it does, is if you return true, show the control, if you return false, hide the control. So if that flag is present… show it, otherwise hide it.”

    The flag he’s talking about is the wc-email-customizer query arg we just added to the url.

    However, while experimenting with this, all this function does is hide any sections not in that array all the time, regardless of whether or not wc-email-customizer is present in the url.

    Does anyone know what I am missing? Or how to make this work?

    My Relevant code:

    Main Plugin PHP File

    function ezt_plugin_settings_page(){
    
    add_submenu_page(
        'edit.php?post_type=ez-testimonial',
        'Ez Testimonials Settings',
        'Settings',
        'manage_options',
        'ezt_settings',
        'ezt_plugin_settings_callback'
    );
    
    }
    add_action( 'admin_menu', 'ezt_plugin_settings_page' );
    
    require_once ( plugin_dir_path(__FILE__) . 'ezt-settings.php' );

    ezt-settings.php

    function ezt_plugin_settings_callback(){
    
    //Get customizer url
    $url = admin_url( 'customize.php' );
    
    $url = add_query_arg(
        'wc-email-customizer',
        'true',
        $url
    );
    
    ?>
        <div class="container-fluid">
            <div class="row">
                <h2 id="ezt-settings-heading">EZ Testimonials Settings</h2>
            </div>
            <div class="row"> <!-- esc url -->
                <a class="btn btn-default" href="<?php echo $url; ?>">Customize</a>
            </div>
        </div>
    
    <?php
    
    }
    
    add_filter( 'customize_control_active', 'control_filter', 10, 2 );
    
    function control_filter( $active, $control ) {
        if ( in_array(
            $control->section,
            array(
                'wc_email_header',
                'wc_email_body',
                'wc_email_footer',
                'wc_email_send'
                )
            )
        ){
        return true;
    
    } 
    return false;
    
    }
Viewing 12 replies - 1 through 12 (of 12 total)
  • I may sound dumb but you didnt sanitize the modified URL esc_url();

    Thread Starter cgfx360

    (@cgfx360)

    @saurabhsaneja

    Sorry about that, let me try and clarify.

    If I understand it right, we add this query arg to the url:

    $url = add_query_arg(
        'wc-email-customizer',
        'true',
        $url
    );

    which I echo out here:

    <a class="btn btn-default" href="<?php echo $url; ?>">Customize</a>

    And This section of code:

    add_filter( 'customize_control_active', 'control_filter', 10, 2 );
    
    function control_filter( $active, $control ) {
        if ( in_array(
            $control->section,
            array(
                'wc_email_header',
                'wc_email_body',
                'wc_email_footer',
                'wc_email_send'
                )
            )
        ){
        return true;
    
    } 
    return false;
    
    }

    Is supposed to somehow be checking to see if the url contains the query arg we set here above. This one:

    $url = add_query_arg(
        'wc-email-customizer',
        'true',
        $url
    );

    Is present in the url (or rather, set to true ?). If it is, it will run the control_filter() function and remove any sections that are not in that array from the customizer. (Widgets, Colors, etc… the default customizer sections).

    So if we created a section like:

    $wp_customize->add_section( 'wc_email_header' , array(
        'title'      => __( 'Section Name', 'mytheme' ),
    ) );

    It’s my understanding this section would display because it is listed in that array inside the control_filter function. Whereas the default customizer sections/controls (Widgets, Colors, etc…) are not, so they wouldn’t be displayed.

    My problem is, I feel like I am missing something, because the function does in fact remove any sections not in that array, but it’s supposed to only do so for the customizer accessed via the button here:

    <a class="btn btn-default" href="<?php echo $url; ?>">Customize</a>

    and not the when the customizer is accessed via the appearance menu or any other method. But instead it’s running no matter how the customizer is accessed. Which makes me feel like I’m missing an if statement or something somewhere, but anything i’ve tried hasn’t worked.

    I hope that makes more sense?
    (Sorry for such a long reply, I wanted to try and clarify it as best I could)

    • This reply was modified 7 years, 7 months ago by cgfx360.
    • This reply was modified 7 years, 7 months ago by cgfx360.

    Thanks for the additional info.. I do understand the problem now.. Lemme think of a solution to it ??

    Can you please share with me the code for creating a customize button in wooommerce panel?

    Thread Starter cgfx360

    (@cgfx360)

    @saurabhsaneja

    The code to create a button in the woocommerce panel is:

    array(
       'title' => __('Customize!', 'woocommerce'),
       'desc'  => __('Some Text', 'woocommerce'),
       'type'  => 'wc_button',
       'link'  => $url
     )

    WooCommerce Settings API
    https://docs.woocommerce.com/document/settings-api/

    However, in my case, I’m not using WooCommerce. I’m creating a standalone plugin, so it’s slightly different (I use add_submenu_page() to create a settings page, and then the button is created in the callback function. which may be why it’s not working like the video. You can see what I mean with the code I posted in the original post.).

    But I basically create the button with:

    //Get customizer url
    $url = admin_url( 'customize.php' );
    
    $url = add_query_arg(
        'wc-email-customizer',
        'true',
        $url
    );
    
    ?>
        
                <a class="btn btn-default" href="<?php echo $url; ?>">Customize</a>
            
    
    <?php
    
    }

    P.S. As for the esc_url, I have a comment above some of the code to sanitize it, I just didn’t do so because I couldn’t get this to work in the first place. Not sure that’d make a difference in it working?

    • This reply was modified 7 years, 7 months ago by cgfx360.

    I am unable to add customize button; looks like woo commerce code is outdated

    Hey, I have solved it.

    I added submenu page to posts menu:

    <?php
    
    /* 
     * Plugin Name: Usefull
     */
    
    function ezt_plugin_settings_page(){
    
    add_submenu_page(
        'edit.php',
        'Ez Testimonials Settings',
        'Usefull plugin Settings',
        'manage_options',
        'ezt_settings',
        'ezt_plugin_settings_callback'
    );
    
    }
    add_action( 'admin_menu', 'ezt_plugin_settings_page' );
    
    function ezt_plugin_settings_callback(){
        
            $screen = get_current_screen();
        
            if($screen->base !== 'posts_page_ezt_settings'){
                return;
            }
        
            //var_dump($screen);
            var_dump($screen->base);
            //echo $base; 
            echo 'bullshit';    
    
            //Get customizer url
            $url = admin_url( 'customize.php' );
    
            $url = add_query_arg(
                'wc-email-customizer',
                'true',
                $url
            );
    ?>
        <div class="container-fluid">
            <div class="row">
                <h2 id="ezt-settings-heading">EZ Testimonials Settings</h2>
            </div>
            <div class="row"> <!-- esc url -->
                <a class="btn btn-default" href="<?php echo $url; ?>">Customize</a>
            </div>
        </div>
    
    <?php
    
    add_filter( 'customize_control_active', 'control_filter', 10, 2 );
    
    function control_filter( $active, $control ) {
        if ( in_array(
            $control->section,
            array(
                'wc_email_header',
                'wc_email_body',
                'wc_email_footer',
                'wc_email_send'
                )
            )
            ){
            return true;
    
        } 
        return false;
    
        }
    
    }

    You would need to edit $screen->base !== ‘posts_page_ezt_settings’`; you can find out what’s your settings page screen base is by var dumping and checking for base

    function ezt_plugin_settings_callback(){
        
            $screen = get_current_screen();
            var_dump($screen->base);
    }

    Something weird is happening when I click on customize second time from appearnace menu while the first customize is still open in new tab, second time customize link changes to https://localhost/wp-admin/customize.php?return=%2Fwp-admin%2Fedit.php%3Fpage%3Dezt_settings

    OK, looks like I have figured it out.

    When you’re on this page https://www.whatever-is-your-url/wp-admin and hover over customize under appearance the url-of-customize You'd only see www.whatever-is-your-url/wp-admin/customize.php?return=%2Fwp-admin%2F would LOOK perfect (don’t let it deceive you) but if you’re on some other page for instance www.whatever-is-your-url/wp-admin/nav-menus.php the beatiful perfect www.whatever-is-your-url/wp-admin/customize.php?return=%2Fwp-admin%2F would not look the same under appearance or when you’d open customize from CUSTOMIZE under APPEARANCE; you’d see the url is suffixed with something. Don’t worry it’s meant to be that way. It still does the same thing i.e. loads what it’s supposed to load.

    code for plugin:

    <?php
    
    /* 
     * Plugin Name: Usefull
     */
    
    function ezt_plugin_settings_page(){
    
    add_submenu_page(
        'edit-comments.php',
        'Ez Testimonials Settings',
        'Usefull plugin Settings',
        'manage_options',
        'ezt_settings',
        'ezt_plugin_settings_callback'
    );
    
    }
    add_action( 'admin_menu', 'ezt_plugin_settings_page' );
    
    function ezt_plugin_settings_callback(){
        
            $screen = get_current_screen();
        var_dump($screen->base);
            if($screen->base !== 'comments_page_ezt_settings'){
                return;
            }
        
            //var_dump($screen);
            var_dump($screen->base);
            //echo $base; 
            echo 'bullshit';    
    
            //Get customizer url
            $url = admin_url( 'customize.php' );
    
            $url = add_query_arg(
                'wc-email-customizer',
                'true',
                $url
            );
    ?>
        
                <a class="btn btn-default" href="<?php echo $url; ?>">Customize</a>
        
    <?php
    add_filter( 'customize_control_active', 'control_filter', 10, 2 );
    
    function control_filter( $active, $control ) {
        if ( in_array(
            $control->section,
            array(
                'wc_email_header',
                'wc_email_body',
                'wc_email_footer',
                'wc_email_send'
                )
            )
            ){
            return true;
    
        } 
        return false;
    
        }}
    
    
    Thread Starter cgfx360

    (@cgfx360)

    @saurabhsaneja

    I still haven’t managed to make this work.

    The code you provided, at least for me, still presents the same problem I was having. When you access the customizer via the settings page, it still doesn’t hide any sections not in the array, it just shows them.

    If I move the control_filter() function outside of the ezt_plugin_settings_callback() function, it will fire and remove all the sections not in the array, but it fire’s all the time and removes the sections even when the customizer is accessed via the appearance menu, which I dont want.

    I made a video to better show what I mean:
    https://streamable.com/cqla3

    Not sure if it’s just me, or what, but I can’t seem to get it to work.

    Thanks your all your help so far by the way, I really appreciate it.

    • This reply was modified 7 years, 7 months ago by cgfx360.
    • This reply was modified 7 years, 7 months ago by cgfx360.

    hey, i think you’d need to create a new instance of wp customize class.

    I have been thinking if you could move this topic to fixing wordpress and change subject to: if I create a new customize button and change its action, it changes the action of the existing customize button under the appearance.

    Dont write any code there and don’t mention woocommerce(i think it’s a good idea) but expaining it well enough by using pcitures or video you made for me

    Hey, I got a GREAT idea and this time it would work. I am using a premium theme where you can go to theme options and select and select which sections you want tp see in the customizr. SO what you can do is once a person clicks cusgomize button in the plugin page, all sections of the customize get deselected except woocommerce/plugin section and when a person closes the customize all sections are selected again(reverse operation).

    You would have to write the code for it.

    check 3rd image where one can select and deselect sections in customise
    https://wpexplorer-themes.com/total/docs/theme-options/

    Wish you best of LUCK ??

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Using the Customizer as Plugin Settings Page, Hide default panels’ is closed to new replies.