gpt4 book ai didi

Wordpress - 为自定义帖子类型创建自己的管理消息

转载 作者:行者123 更新时间:2023-12-04 06:19:22 26 4
gpt4 key购买 nike

我创建了一个名为 routes 的自定义帖子类型,并且我希望能够在保存/更新期间出现问题时将错误消息返回到屏幕,例如该类型允许上传 gpx/kml 文件并检查是否已发布正确的类型。目前它只是在出错时返回 - 如何设置错误消息?

//Return if file type wrong.
if($file_type != 'application/octet-stream' && $file_type != 'application/gpx+xml' ) {
return;
}

最佳答案

尝试这个

例子:

add_admin_message('Please enter valid URL for the project link', true);
add_admin_message('Your custom post type was updated');

来源:
<?php

/**
* Messages with the default wordpress classes
*/
function showMessage($message, $errormsg = false)
{
if ($errormsg) {
echo '<div id="message" class="error">';
}
else {
echo '<div id="message" class="updated fade">';
}

echo "<p>$message</p></div>";
}

/**
* Display custom messages
*/
function show_admin_messages()
{
if(isset($_COOKIE['wp-admin-messages-normal'])) {
$messages = strtok($_COOKIE['wp-admin-messages-normal'], "@@");

while ($messages !== false) {
showMessage($messages, true);
$messages = strtok("@@");
}

setcookie('wp-admin-messages-normal', null);
}

if(isset($_COOKIE['wp-admin-messages-error'])) {
$messages = strtok($_COOKIE['wp-admin-messages-error'], "@@");

while ($messages !== false) {
showMessage($messages, true);
$messages = strtok("@@");
}

setcookie('wp-admin-messages-error', null);
}
}

/**
* Hook into admin notices
*/
add_action('admin_notices', 'show_admin_messages');

/**
* User Wrapper
*/
function add_admin_message($message, $error = false)
{
if(empty($message)) return false;

if($error) {
setcookie('wp-admin-messages-error', $_COOKIE['wp-admin-messages-error'] . '@@' . $message, time()+60);
} else {
setcookie('wp-admin-messages-normal', $_COOKIE['wp-admin-messages-normal'] . '@@' . $message, time()+60);
}
}

关于Wordpress - 为自定义帖子类型创建自己的管理消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6799662/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com