2011-05-27 94 views
0

我会显示一个自定义的错误消息。WordPress的:如何显示自定义错误消息在wp_insert_post_data()

function ccl($data, $postarr = '') { 
if($data['post_status'] == "publish"){ 
    $data['post_status'] = "draft"; 
    echo '<div id="my-custom-error" class="error fade"><p>Publish not allowed</p></div>'; 
} 
    return $data; 
} 

add_filter('wp_insert_post_data' , 'ccl' , '99'); 

我试了很多想,但每次成功的消息都来自WordPress的文章发表。我可以杀死成功消息并显示我自己的错误消息吗?

坦克的帮助......

回答

5

,因为用户在此之后立即重定向你不能在wp_insert_post_data过滤器打印错误。最好的办法是挂钩到重定向过滤器并向查询字符串添加消息变量(这将覆盖任何现有的Wordpress消息)。

因此,在您的wp_insert_post_data过滤器功能中添加重定向过滤器。

add_filter('wp_insert_post_data', 'ccl', 99); 
function ccl($data) { 
    if ($data['post_type'] !== 'revision' && $data['post_status'] == 'publish') { 
    $data['post_status'] = 'draft'; 
    add_filter('redirect_post_location', 'my_redirect_post_location_filter', 99); 
    } 
    return $data; 
} 

然后在重定向过滤器函数中添加一个消息变量。

function my_redirect_post_location_filter($location) { 
    remove_filter('redirect_post_location', __FUNCTION__, 99); 
    $location = add_query_arg('message', 99, $location); 
    return $location; 
} 

最后挂钩到post_updated_messages过滤器并添加您的消息,所以Wordpress知道要打印什么。

add_filter('post_updated_messages', 'my_post_updated_messages_filter'); 
function my_post_updated_messages_filter($messages) { 
    $messages['post'][99] = 'Publish not allowed'; 
    return $messages; 
} 
相关问题