wil*_*ler 5 php wordpress session hook plugins
我正在添加验证,因此如果帖子属于特定类别,则需要设置某些自定义字段.
这应该是简单的挂钩wp_insert_post_data和admin_notices,但没有引起重定向admin_notices回调消失.
好的 - 所以我创建了一个使用Session在重定向中存储我的错误消息的hack:
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");
Run Code Online (Sandbox Code Playgroud)
这种解决方案的问题是,一些会话打电话时怎么不可用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');
Run Code Online (Sandbox Code Playgroud)
问题是:为什么我必须通过所有这些疯狂来抛出错误信息?
我究竟做错了什么?
小智 7
你可以像WordPress一样做:使用这样的瞬态:
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");
Run Code Online (Sandbox Code Playgroud)
ps:在WordPress中避免$ _SESSION,thx
Wordpress 不使用会话,如果register_globals使用会话,则会清除数组$_SESSION。
Wordpress 使用 URL 中的整数传递消息,然后在文件夹中的相关文件message中定义消息数组。我认为您可以将自己的变量附加到重定向中,然后在您的钩子函数中获取它。查看这些文件以了解其工作原理。edit-[type]-form.phpwp-adminadmin_noticesedit-[type]-form.php
| 归档时间: |
|
| 查看次数: |
5357 次 |
| 最近记录: |