要在文章发布成功后显示提示信息,你可以在文章成功发布后,将成功提示信息存储在会话(session)中,然后在页面加载时检查会话中是否有成功提示信息,如果有则显示出来。
以下是修改后的代码:
- 启用会话(session):在代码的最开始处启用会话。
- 存储成功提示信息:在文章成功发布后,将成功提示信息存储在会话中。
- 显示成功提示信息:在页面加载时检查会话中是否有成功提示信息,如果有则显示出来,并清除会话中的提示信息。
修改后的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
| <?php
session_start();
require_once('../wp-load.php');
require_once 'Parsedown.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['title'])) { if (!isset($_POST['markdown_publish_nonce']) || !wp_verify_nonce($_POST['markdown_publish_nonce'], 'markdown_publish_action')) { echo '<div class="error">安全验证失败,请重试。</div>'; } elseif (!current_user_can('publish_posts')) { echo '<div class="error">你没有权限发布文章。</div>'; } else { $title = sanitize_text_field($_POST['title']); $category = isset($_POST['category']) ? intval($_POST['category']) : 0; $tags = isset($_POST['tags']) ? sanitize_text_field($_POST['tags']) : ''; $mode = isset($_POST['mode']) ? sanitize_text_field($_POST['mode']) : 'wysiwyg';
if ($mode === 'wysiwyg') { $content = wp_kses_post($_POST['content']); } elseif ($mode === 'markdown2html') { $markdown = $_POST['markdown']; $parsedown = new Parsedown(); $content = $parsedown->text($markdown); } elseif ($mode === 'markdown') { $content = $_POST['markdown']; } else { echo '<div class="error">无效的发布模式。</div>'; return; }
$post_id = wp_insert_post([ 'post_title' => $title, 'post_content' => $content, 'post_status' => 'publish', // 直接发布 'post_author' => get_current_user_id(), // 当前用户为作者 'post_category' => [$category], // 分配分类 ]);
if ($post_id) { if (!empty($tags)) { wp_set_post_tags($post_id, $tags); }
$_SESSION['success_message'] = '<div class="success">文章发布成功!<a href="' . get_permalink($post_id) . '">查看文章</a></div>';
header('Location: ' . $_SERVER['REQUEST_URI']); exit; } else { echo '<div class="error">文章发布失败,请重试。</div>'; } } }
$categories = get_categories(['hide_empty' => false]); ?>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>文章发布工具</title> <!-- 引入 TinyMCE --> <script src="https://cdn.tiny.cloud/1/ai2tag8ofr7pco5jz64wsp0b4ripk1jrpom8n67h6mbg009v/tinymce/5/tinymce.min.js" referrerpolicy="origin"></script> <style> </style> <script> </script> </head> <body> <div class="publish-tool"> <h1>文章发布工具</h1> <?php // 显示成功提示信息 if (isset($_SESSION['success_message'])) { echo $_SESSION['success_message']; unset($_SESSION['success_message']); } ?> <form method="POST" action="" novalidate> <?php wp_nonce_field('markdown_publish_action', 'markdown_publish_nonce'); ?> <div class="mode-switch"> <label> <input type="radio" name="mode" value="wysiwyg" checked onchange="toggleMode()"> 所见即所得 </label> <label> <input type="radio" name="mode" value="markdown2html" onchange="toggleMode()"> Markdown 转 HTML </label> <label> <input type="radio" name="mode" value="markdown" onchange="toggleMode()"> Markdown 原文 </label> </div> <div> <label for="title">标题:</label> <input type="text" name="title" id="title" required> </div> <div id="content-wrapper"> <label for="content">内容:</label> <textarea name="content" id="content" placeholder="在此输入内容..."></textarea> </div> <div id="markdown-wrapper" class="hidden"> <label for="markdown">Markdown 内容:</label> <textarea name="markdown" id="markdown" placeholder="在此输入 Markdown 文本..."></textarea> </div> <div> <label for="category">分类:</label> <select name="category" id="category"> <?php foreach ($categories as $cat): ?> <option value="<?php echo $cat->term_id; ?>"><?php echo $cat->name; ?></option> <?php endforeach; ?> </select> </div> <div> <label for="tags">标签(用逗号分隔):</label> <input type="text" name="tags" id="tags"> </div> <div class="buttons"> <button type="submit" name="publish">发布</button> <button type="button" onclick="resetForm()">重置</button> </div> </form> </div> </body> </html>
|
关键点解释:
- 启用会话:在代码的最开始处使用
session_start() 启用会话。
- 存储成功提示信息:在文章成功发布后,将成功提示信息存储在
$_SESSION['success_message'] 中。
- 显示成功提示信息:在页面加载时检查
$_SESSION['success_message'] 是否存在,如果存在则显示出来,并清除会话中的提示信息。
这样,当文章发布成功后,页面会显示“文章发布成功!”的提示信息,并且用户可以点击链接查看文章。
时间: 2025-02-27T20:00:38
👤 用户
下面的原代码我想添加文章发布成功提示功能。如下面所示:
下面是原代码:
// 引入 Parsedown 库(用于解析 Markdown)
require_once ‘Parsedown.php’;
// 检查是否提交了表单
if ($_SERVER[‘REQUEST_METHOD’] === ‘POST’ && isset($_POST[‘title’])) {
// 检查 nonce 和用户权限
if (!isset($_POST[‘markdown_publish_nonce’]) || !wp_verify_nonce($_POST[‘markdown_publish_nonce’], ‘markdown_publish_action’)) {
echo ‘
安全验证失败,请重试。
‘;
} elseif (!current_user_can(‘publish_posts’)) {
echo ‘
你没有权限发布文章。
‘;
} else {
// 获取表单数据
$title = sanitize_text_field($_POST[‘title’]);
$category = isset($_POST[‘category’]) ? intval($_POST[‘category’]) : 0; // 分类 ID
$tags = isset($_POST[‘tags’]) ? sanitize_text_field($_POST[‘tags’]) : ‘’; // 标签
$mode = isset($_POST[‘mode’]) ? sanitize_text_field($_POST[‘mode’]) : ‘wysiwyg’; // 发布模式
// 根据发布模式处理内容
if ($mode === 'wysiwyg') {
$content = wp_kses_post($_POST['content']); // 所见即所得模式
} elseif ($mode === 'markdown2html') {
$markdown = $_POST['markdown'];
$parsedown = new Parsedown();
$content = $parsedown->text($markdown); // Markdown 转 HTML
} elseif ($mode === 'markdown') {
$content = $_POST['markdown']; // 直接发布 Markdown
} else {
echo '<div class="error">无效的发布模式。</div>';
return;
}
// 发布文章
$post_id = wp_insert_post([
'post_title' => $title,
'post_content' => $content,
'post_status' => 'publish', // 直接发布
'post_author' => get_current_user_id(), // 当前用户为作者
'post_category' => [$category], // 分配分类
]);
if ($post_id) {
// 分配标签
if (!empty($tags)) {
wp_set_post_tags($post_id, $tags);
}
// 重定向到当前页面,避免表单重复提交
header('Location: ' . $_SERVER['REQUEST_URI']);
exit;
} else {
echo '<div class="error">文章发布失败,请重试。</div>';
}
}
}
// 获取所有分类
$categories = get_categories([‘hide_empty’ => false]);
?>
文章发布工具