Wordpress: 오류 메시지 표시 - wp_insert_post_data 또는 publish_post에서 hook admin_notics 실패
게시물이 특정 카테고리에 있는 경우 특정 사용자 정의 필드를 설정해야 하므로 유효성 검사를 추가합니다.
이것은 쉽게 걸 수 있을 것입니다.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');
문제는 다음과 같습니다.왜 내가 오류 메시지를 던지려고 이 모든 미친 짓들을 겪어야 합니까?
내가 뭘 잘못하고 있는 거지?
WordPress do처럼 간단하게 할 수 있습니다. 이와 같은 과도현상을 사용하면 됩니다.
function set_post_pending($data, $postarr) {
// If it's not valid...
$error = "You are missing some Custom Fields.";
set_transient( get_current_user_id().'missingfield', $error );
$data['post_status'] = 'pending';
return $data;
}
add_filter('wp_insert_post_data', 'set_post_pending',1,2);
function show_admin_notice() {
if($out = get_transient( get_current_user_id().'missingfield' ) ) {
delete_transient( get_current_user_id().'missingfield' );
echo "<div class=\"error\"><p>$out</p></div>";
}
// return false; // nothing to return here
}
add_action('admin_notices', "session_admin_notice");
ps : WordPress에서 $_SESSION 회피, thx
워드프레스는 세션을 사용하지 않으며, 만약register_globals그것은 그것을 클리어 할 것입니다.$_SESSION배열해 놓은
워드프레스는 메시지를 전달합니다.messageURL에서 정수, 메시지 배열은 관련된 것으로 정의됩니다.edit-[type]-form.php줄을 늘어놓다wp-admin폴더.제 생각에는 아마도 당신이 직접 변수를 리다이렉트에 추가한 다음에 그것을 당신의 것에 넣을 수 있을 것 같습니다.admin_notices훅 기능한 번 봐봐요.edit-[type]-form.php파일들이 어떻게 작동하는지에 대한 아이디어를 얻을 수 있습니다.
if($out = $_SESSION['admin_notices']) {
$_SESSION["admin_notices"] = "";
echo $out;
}
이 조건은 항상 TRUE이므로 항상 $_SESS를 재설정합니다.ION['admin_notics'] var
언급URL : https://stackoverflow.com/questions/1242328/wordpress-displaying-an-error-message-hook-admin-notices-fails-on-wp-insert-p
'programing' 카테고리의 다른 글
| Oracle: 하위 쿼리의 여러 결과를 쉼표로 구분된 단일 값으로 결합합니다. (0) | 2023.10.29 |
|---|---|
| RecyclerView에서 보이는 항목 가져오기 (0) | 2023.10.29 |
| ${env:32bit/x86 시스템에 ProgramFiles(x86)} Powershell 변수가 포함되어 있습니까? (0) | 2023.10.29 |
| 사용자가 이미지를 보기로 스크롤할 때 이미지를 동적으로 로드하는 방법 (0) | 2023.10.29 |
| 노드 JS 약속.모두에 대하여 (0) | 2023.10.24 |