2014-09-13 64 views
1

我想弄清楚如何中止運行的WordPress功能。當用戶刪除自定義帖子類型(在我的例子中是商店)時,我想檢查是否有與該商店相關的帖子。我正在運行查詢並檢查是否有返回的結果。如果我們返回一個或多個結果,我想中止刪除並向用戶顯示一條錯誤消息,指出他們必須刪除關聯的帖子。我正在使用'before_delete_post'這個動作。這是我要去的:在某些條件下終止運行WordPress的功能

if (count($results)==0){ 
    //delete the data 
} else { 
    //abort the delete. 
} 

在此先感謝您的幫助。

+0

你應該嘗試的東西,讓我們知道,如果它不工作,不問我們如何做你的工作 – kennypu 2014-09-13 02:03:00

回答

2

如果你正在使用before_delete_post你可以有這樣的事情:

function prevent_delete_custom_post() { 
    if (count($results)==0){ 
     //delete the data 
    } else { 
     wp_redirect(admin_url('edit.php')); //here you can try to get the variables that you have in the url to redirect the user to the same place. 
     exit(); 
    } 
} 
add_action('before_delete_post', 'prevent_delete_custom_post', 1); 

請記住,後元數據被刪除之前「before_delete_post」行動被觸發。 http://codex.wordpress.org/Plugin_API/Action_Reference/before_delete_post

並且從數據庫中刪除帖子(或頁面)之前和之後觸發'delete_post'動作。 http://codex.wordpress.org/Plugin_API/Action_Reference/delete_post

+0

謝謝你,我怎麼會顯示一條消息給用戶,當我們將其重定向到編輯頁面的說刪除不成功? – user2078519 2014-09-13 19:24:10

相關問題