2013-06-06 97 views
2

reset.php文件:在Ajax調用WordPress的PHP函數?

<?php 
    add_action('admin_init','popup_template_reset_options'); 

    function popup_template_reset_options() 
     { 
     delete_option('popup_template_on'); 
     delete_option('popup_template_close'); 
     delete_option('popup_template_escape'); 
     delete_option('popup_template_external'); 
     delete_option('popup_template_home_page'); 
     delete_option('popup_template_all_pages'); 
     delete_option('popup_template_template'); 
     delete_option('popup_cookies_display_after_like'); 
     add_option('popup_cookies_display_after_like','365'); 
     //add_option('popup_template_on','1'); 
     add_option('popup_template_close','1'); 
     add_option('popup_template_escape','1'); 
     add_option('popup_template_external','1'); 
     add_option('popup_template_force_timer','2'); 
     add_option('popup_template_home_page','1'); 
     add_option('popup_template_all_pages','1'); 
     add_option('popup_template_template','2'); 

    } 
    ?> 

腳本的Ajax:

<script type="text/javascript"> 
    $(document).ready(function() { 
     $('#reset_general').click(function() 
     { 
      $('#result1').css("display", "block"); 
      jQuery('#result1').animate({'opacity': '1'}); 

     }); 
    }); 
    function resetgeneral() { 
     $.ajax({type: 'POST', url: '<?php echo WP_PLUGIN_URL; ?>/fantasticpopuptemplate/inc/reset.php', success: function(response) { 

       //$('#fff').find('.form_result').html(response); 
       $('#result1').css("display", "none"); 
       $('#resets1').css("display", "block"); 
       $('#resets1').html("Settings Resets"); 
       $('#resets1').fadeOut(2500, "linear"); 
      }}); 

     return false; 
    } 

</script> 
<form onsubmit="return resetgeneral();" id="form_general_reset" class="form-1"> 
    <input type="submit" value="Reset" id="reset_general" name="reset" class="button-secondary"/> 
    <input name="action" type="hidden" value="reset" /> 

</form> 

嗨,我試圖調用Ajax中的PHP復位功能,但是當我使用Google我知道,不是直接可能性調用PHP函數,所以我把這個特殊的功能在單獨的PHP文件,並調用該PHP文件我不知道我怎麼能做到這一點在阿賈克斯我試過這個代碼,但沒有發生。出現設置重置消息。我如何做到這一點任何幫助都會很棒。在使用ajax概念之前,我使用isset函數在php中嘗試了但是它每次都會獲取頁面,因爲只有我跳入ajax。

回答

6

依靠admin_ajax.php存在一種在WordPress插件中實現AJAX的標準方式。該文件的名稱是誤導性的,因爲它也可以用於前端,通過將函數分配給AJAX操作。

有在WordPress的法典一個很好的說明:

http://codex.wordpress.org/AJAX_in_Plugins

有一點要注意的:你的AJAX處理功能將始終有一個die()命令終止,避免了額外的「0 'WordPress的輸出。

+0

謝謝beerwin我只用了約1周的ajax。我讀出了相應的鏈接。我調用php函數,當我點擊重置按鈕,它獲得特定的功能(在使用ajax我使用isset函數之前)。我不知道如何在ajax中做到這一點,任何幫助。對不起,我有點與相應的鏈接混淆 –

+0

你嘗試過這種解決方案嗎? – beerwin

+2

你實際上不能直接從Ajax調用調用PHP函數。你可以做的是用Ajax發送一個變量,並在PHP文件中監聽該變量,如果變量值爲true,那麼調用函數 – Martin