2013-03-21 64 views
2

是否可以有兩種形式來保存插件選項(一個插件頁面),並分別提交每個表單?Wordpress插件 - 單獨的表單 - 一個插件頁面

使用一種形式,真的很容易。 例如,插件頁面(儀表盤)可以包含這個(最小):

<form action="options.php" method="post" > 
    <?php settings_fields('my-settings'); ?> 
    <?php do_settings_sections('my-settings'); ?> 

    //more input fields 

    <?php submit_button(); ?> 
</form> 

以及保存選項,這將工作得很好。但是,是否有可能有這樣的兩種形式,使用相同的插件選項頁面(但提交時,只保存選項從表單,我提交按鈕,我點擊)?目前,如果我創建另一個表單,並點擊提交,它仍然會提交這兩個表單,而不僅僅是一個(我點擊提交按鈕)。

任何想法?

回答

1

而不是採取這種方法,我會這樣做,而不是使用ajax。它更容易實現,並會給你的插件一個非常web 2.0的感覺。就你而言,它實際上更容易實現ajax方法,而不是嘗試去做你正在做的事情。 Here is讓你開始。

<?php 
add_action('admin_print_scripts', 'my_action_javascript'); 

function my_action_javascript() { 
?> 
<script type="text/javascript" > 
jQuery(document).ready(function($) { 

    var data = { 
     action: 'my_action', 
     whatever: 1234 
    }; 

    // since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php 
    jQuery.post(ajaxurl, data, function(response) { 
     alert('Got this from the server: ' + response); 
    }); 
}); 
</script> 
<?php 
} 

然後你就可以處理這樣的要求:

<?php 

add_action('wp_ajax_my_action', 'my_action_callback'); 

function my_action_callback() { 
    global $wpdb; // this is how you get access to the database 

    $whatever = intval($_POST['whatever']); 

    $whatever += 10; 

     echo $whatever; 

    die(); // this is required to return a proper result 
} 
+0

肯定。最後,我必須用ajax來做。它需要更多的工作(安全性需要手動執行),但工作得很好! – cool 2013-03-22 05:36:59