2013-04-24 44 views
0

我已经为主题定制器创建了一个自定义控件,它是一个简单的按钮和标签。我将使用它作为主题重置按钮,将主题模式设置清除为原始状态。现在我已经添加了控件并将其显示在定制器上,但我不确定应该在哪里添加代码以重置设置。在wordpress主题定制器中需要帮助链接按钮到函数

到目前为止,我只为css和文本更改创建了自定义器设置。要使用删除我将设置remove theme mods function.

<?php remove_theme_mods() ?> 

所以我的问题是你究竟怎么做我用这个按钮上面看到执行remove_mods功能?该功能的文档非常少。

如果有另一种方式来重置主题MOD设置为默认值,这是不正确的做法比请附和。

这里是我创建了我的自定义按钮的代码。

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

回答

1

在主题定制,你可以javascript来wordpress主题定制您的自定义注册

add_action('customize_preview_init', 'your_live_preview_function'); 

public static function your_live_preview_function() { 
     wp_enqueue_script(
       'your-theme_customizer', //Give the script an ID 
       get_template_directory_uri() . '/js/your-customizer-javascript-file.js', //Define it's JS file 
       array('jquery', 'customize-preview'), //Define dependencies 
       rand(1, 1000), //Define a version (optional) (but I use it for development as random so don't have to worry about cache etc. 
       true //Specify whether to put in footer (leave this true) 
     ); 

} 

和您的JavaScript文件中,你可以做这样的事情

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

和内部AJAX可以做这样的事

add_action('wp_ajax_your_ajax_action_for_calling_reset', 'your_ajax_action_for_calling_reset_callback_function'); 

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

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

Haaaaaa希望它有帮助。

+0

你不知道我有多兴奋。我尝试了几个小时,尝试不同的东西。我非常感谢你花时间基本解决了我的整个问题。谢谢。 – user1632018 2013-04-24 19:00:46

+0

我有一个问题。我已经有了主题定制器的实时预览功能和JS文件。只使用相同的函数和JS文件,并将JavaScript添加到该文件中会更有意义吗? – user1632018 2013-04-24 21:43:51

+0

我有另外几个问题,我还没有能够得到它的工作,所以我想确保我完全理解这一点。它在jQuery中说ajax_url,我用custom.php页面的URL替换它吗?还有它说'your_reset_button_control_id'是否需要添加一个id到控件,或者我只是使用我添加控件/设置时给的名称? – user1632018 2013-04-24 23:07:53