我有一个在更新后调用的函数,在这个函数中我正在做一些事情,如果出现问题,我想显示admin_notices
.
Inside 我的function 我尝试了以下方法:
if($error) {
add_action( \'admin_notices\', \'my_admin_notice\' );
}
my_admin_notice
看起来像:
function my_admin_notice() {
?>
<div class="error">
<p>Error happend</p>
</div>
<?php
}
如果我打电话
add_action( \'admin_notices\', \'my_admin_notice\' );
在我的功能之外,它正在工作,但不幸的是,通知总是显示…:/
Update - Context details:
使用调用我的函数
add_filter ( \'publish_post\', \'my_function\' );
在这个函数中,我检查帖子是否是第一次发布的,然后我正在做一些事情,如果有什么问题,我想调用add_action( \'admin_notices\', \'my_admin_notice\' );
要显示错误消息。。。
我的函数运行得很好,只有在我需要时才被调用,所以这不是问题所在。。。
我认为add_action( \'admin_notices\', \'my_admin_notice\' );
我在函数内部调用的函数被忽略了!
Update Code for Context:
function my_admin_notice() {
?>
<div class="error">
<p>Error happend</p>
</div>
<?php
}
function checkEdit() {
if( ($_POST[\'post_type\'] == \'post\') && ( $_POST[\'post_status\'] == \'publish\' ) &&
( $_POST[\'original_post_status\'] != \'publish\' ) ) {
$title = get_the_title();
$postId = (string) get_the_ID();
$postUrl = get_site_url() . "?p=" . $postId;
$wp_category_array = get_the_category();
$categoryArray = array();
foreach ($wp_category_array as $wp_category => $value) {
$categoryArray[] = $value->cat_ID;
}
$postDate = get_the_date(\'Y-m-j H:i:s\');
$subTitle = get_the_date();
$parse = new parseRestClient(array(
\'appid\' => \'XXXXXXXXXXXXXXXXXXXXXXX\',
\'restkey\' => \'XXXXXXXXXXXXXXXXXXXX\'
));
$params = array(
\'className\' => \'News\',
\'object\' => array(
\'Title\' => $title,
\'SubTitle\' => $subTitle,
\'array\' => $categoryArray,
\'code\' => $postId,
\'Date\' => $postDate,
\'Url\' => $postUrl
)
);
$request = $parse->create($params);
$responseArray = json_decode($request, true);
$objectId = $responseArray[\'objectId\'];
if ($responseArray [\'error\'] == true) {
// Called if error (working)
add_action( \'admin_notices\', \'my_admin_notice\' );
}else {
$sql = "UPDATE wp_posts SET parseId = \'$objectId\' WHERE ID = \'$postId\'";
$result = mysql_query($sql);
}
}
}
add_action ( \'post_updated\', \'checkEdit\');