![](/img/trans.png)
[英]WordPress Forgets $_POST Fields on Page Reload In Admin Panel to Create Post
[英]Wordpress ajax post to same page - with only admin panel access
我僅對wordpress網站具有管理面板訪問權限(無文件級別訪問權限)。 我正在使用此插件Shortcode Exec PHP添加php代碼,並將我的jquery添加到文本編輯器中。
一切正常。 但是現在我想進行ajax調用,其中我的ajax發布網址將是window.location.href
,並將指向我的短代碼exec php if(isset($_POST)){ echo "Yes!" }
if(isset($_POST)){ echo "Yes!" }
,我得到整個頁面作為響應。 請注意,我是第一次接觸Wordpress和使用草稿進行測試。
請讓我知道我在做什么錯。 贊賞有關實現此目標的更好做法的建議。 以下是我的代碼:
// jQuery的
$.ajax(
{
type: 'POST',
url: window.location.href,
dataType: 'json',
cache: false,
data:{
action: 'test'
},
success: function(response)
{
alert('success');
},
error: function(xhr, textStatus, errorThrown)
{
alert('error');
}
});
// PHP
if(isset($_POST)){ echo "Yes!"; return "true"; }
謝謝。
The statndard way to interact with ajax in Wordpress through WP AJAX.You can do like this.
$.ajax(
{
type: 'POST',
url: "/wp-admin/admin-ajax.php?action=your_action_name",
dataType: 'json',
cache: false,
data:{
your data should be here
},
success: function(response)
{
alert('success');
},
error: function(xhr, textStatus, errorThrown)
{
alert('error');
}
});
then in functions.php or plugin file you need to register a hook for ajax request.wp_ajax should be prefix your action hook name and then method for processing the request.
add_action('wp_ajax','method_name');
function method_name(){
here is your code
}
我認為您就在客戶端(JQuery)上。
但是在wordpress中,您無法使用isset檢查請求。
為此,您必須像這樣實現:
正如您在示例中將測試聲明為操作一樣 。
//For example wp_ajax_**Your_Action**
add_action( 'wp_ajax_test', 'test_callback' );
function test_callback(){
//implement your code
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.