投稿が特定のカテゴリにある場合、特定のカスタムフィールドを設定する必要があります。Wordpress:エラーメッセージを表示する - wp_insert_post_dataまたはpublish_postでフックadmin_noticesが失敗する
wp_insert_post_data
とadmin_notices
をフックするのは簡単ですが、admin_notices
コールバックが消えるリダイレクトがあります。
OK - だから私はリダイレクト全体に私のエラーメッセージを格納するためのセッションを使用してハックを作成しました:
function set_post_pending($data, $postarr) {
// If it's not valid...
$error = "You are missing some Custom Fields.";
$_SESSION['admin_notices'] = $error;
$data['post_status'] = 'pending';
return $data;
}
add_filter('wp_insert_post_data', 'set_post_pending',1,2);
function session_admin_notice() {
if($out = $_SESSION['admin_notices']) {
$_SESSION["admin_notices"] = "";
echo $out;
}
return false;
}
add_action('admin_notices', "session_admin_notice");
この解決策で問題がsession_admin_notice
を呼び出すときにセッションが使用できませんどのように一部です、これは簡単な(しかしクレイジー)ソリューションました:
public static function fix_session_bs() {
// TODO: Why do I have to do this?
if(!session_id() && $_COOKIE["PHPSESSID"]) {
session_start($_COOKIE["PHPSESSID"]);
}
}
add_action('admin_init', 'fix_session_bs');
質問は:なぜ私は、ERをスローするように、すべてのこの狂気を通過する必要がありますメッセージですか?
私は間違っていますか?
正確には、割り当ては(割り当てられた値として)評価されるので、 'fals '$ _SESSION ['admin_notices']'が 'false'か'!isset() 'の場合は' e'を返します。 http://www.sitepoint.com/assignment-inside-a-condition/ – willoller