2013-04-24 41 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