2014-09-13 68 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

相关问题