Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am sorry for the long post but I wanted to be thourough in hopes someone could help me figure out this issue. I have been trying to get it working for 2 days with no such luck. I would appreciate any help at all and I really mean that, thank you.

I have created a custom control for the theme-customizer. It is just a simple button and label that I planned on using to work as a theme reset button(to revert back to original settings). I created a theme mod setting for the control and added the button control to it's own section, like you would any other.

So now comes for the code so I can use it to access the remove theme mods function.

I had to add a function to call a custom javascript file to bind an ajax action that contains the remove theme mods function. Although after I had it all set up it does absolutely nothing. The javascript is being loaded but when calling the function it doesn't work. Can someone look through my code and tell me what is going on?

Here is the function I use to queue the javascript file.

add_action('customize_preview_init', 'newtheme_reset_theme_function');

  function newtheme_reset_theme_function() {

        wp_enqueue_script(

                'newtheme-reset', 

                get_template_directory_uri() . '/js/newtheme-reset.js', file

                array('jquery', 'customize-preview'),

                rand(1, 1000), 

                true 

        );

}

Next is the newtheme-reset.js file that I point to the ajax function. Here is the contents of newtheme-reset.js:

( function( $ ) {
wp.customize(
        'newtheme_clear_settings',
        function( value ) {
            value.bind(
                function( to ) {
                    jQuery.post( ajax_url, 
                    { 
                        action: 'newtheme_theme_reset_action',
                        reset_value: to
                    },
                    function( response ) {
                        jQuery( '.reset-info' ).html( response );
                    }
                    );
                }
                );
        }
        );
} )( jQuery );

Now here is the ajax I use to to call the remove theme mods function. This is placed in customize.php along with the first function I used to queue the JS file.

add_action('wp_ajax_newtheme_theme_reset_action', 'newtheme_theme_reset_action');

function newtheme_theme_reset_action(){
$reset_value = esc_attr($_POST['reset_value']);

if($reset_value){
remove_theme_mods() ;
}
}

As I said earlier, I have been trying to get this working for the last couple of days with no such luck. I really want to get this working. So I have provided some extra details:

Here is the code I used to add the section and button with, you can see that the setting name I added for the control here is the same that I added in the JS. I was thinking it might not be identifying the theme_mod_settings and I might have to use an id in the control itself?

function newtheme_customize_register_clear_settings( $wp_customize ) {

    $theme = wp_get_theme();

    $wp_customize->add_section( 'newtheme_clear', array(
        'title'      => sprintf( __( '%s Reset settings', 'newtheme' ), $theme->Name ),
        'priority'   => 215,
    ) );

    $wp_customize->add_setting( 'newtheme_clear_settings', array(
        'default'    => newtheme_theme_mod( 'clear_settings' )
    ) );

    $wp_customize->add_control( new newtheme_Customize_reset_Control( $wp_customize, 'newtheme_clear_settings', array(
        'label'      => __( 'Reset all settings back to their defaults. Once this is done it cannot be reversed.', 'newtheme' ),
        'section'    => 'newtheme_clear',
        'settings'   => 'newtheme_clear_settings',
        'type'       => 'button',
        'priority'   => 10
    ) ) );      

        do_action( 'newtheme_customize_clear_settings', $wp_customize );

    return $wp_customize;

}
add_action( 'customize_register', 'newtheme_customize_register_clear_settings' );

Now the last thing I will add just to be thorough is the controls code. The control shows up on the page fine though and seems to work as it should.

function newtheme_customize_reset_control($wp_customize) {
    /**
     * Reset Control
     *
     */
    class newtheme_Customize_reset_Control extends WP_Customize_Control {
        public $type = 'button';



        public function render_content() {
    ?>
        <label>
                        <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
                                <div>
                                    <a href="#" class="button-secondary upload"><?php _e( 'Reset Settings' ); ?></a>

                            </div>
                    </label>
    <?php
        }
    } 
}

    add_action( 'customize_register', 'newtheme_customize_reset_control', 1, 1 );
share|improve this question
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.