1、用短代码 【classic_post_editor】在任意页面加载:标题输入框 + 经典编辑器 + 分类选择 + 提交按钮,登录用户提交后直接保存为文章
第一步:在主题 functions.php 里加这段代码
/*===== 前台经典编辑器投稿表单=======================================================================================================*/
/**
* 用法:在任意页面/文章插入短代码
请先 登录 后再投稿。
*/
// 1. 注册短代码
add_shortcode(‘classic_post_editor’, ‘cpe_render_editor_form’);
// 2. 渲染表单
function cpe_render_editor_form() {
// 需要登录才能投稿
if (!is_user_logged_in()) {
return ‘
请先 登录 后再投稿。
‘;
}
// 处理提交
if (isset($_POST[‘cpe_submit’])) {
$result = cpe_handle_submit();
if (is_wp_error($result)) {
echo ‘
‘;
} else {
echo ‘
‘;
}
}
// 当前编辑中的文章(可选:从 query var 读取编辑模式)
$edit_post_id = isset($_GET[‘edit_post’]) ? absint($_GET[‘edit_post’]) : 0;
$current_title = ”;
$current_content = ”;
$current_cat = 0;
if ($edit_post_id && get_post($edit_post_id)) {
$current_title = get_the_title($edit_post_id);
$current_content = get_post_field(‘post_content’, $edit_post_id);
$cats = get_the_category($edit_post_id);
if (!empty($cats)) {
$current_cat = $cats[0]->term_id;
}
}
// 输出表单
ob_start();
?>
style=”width:100%;padding:10px 12px;border:1px solid #ddd;border-radius:4px;box-sizing:border-box;”>
‘textarea_rows’ => 18,
‘media_buttons’ => true,
‘teeny’ => false,
‘quicktags’ => array(
‘buttons’ => ‘strong,em,link,block,del,ins,img,ul,ol,li,code,more,close,fullscreen’,
),
‘tinymce’ => array(
‘height’ => 400,
// 注意:不要写 plugins / toolbar1 / toolbar2,
// 让 WordPress 用内置配置,避免加载已移除的插件报错
),
)
);
?>
‘selected’ => $current_cat,
‘hide_empty’ => false,
‘show_option_none’ => ‘— 选择分类 —’,
‘option_none_value’ => 0,
));
?>
‘post_content’ => $content,
‘post_status’ => ‘pending’, // 想先审核就改成 ‘pending’,无需审核发布文章’publish’
‘post_type’ => ‘post’,
‘post_author’ => get_current_user_id(),
);
// 编辑模式 vs 新建
if ($post_id && get_post($post_id) && get_post_field(‘post_author’, $post_id) == get_current_user_id()) {
$post_data[‘ID’] = $post_id;
$saved_id = wp_update_post($post_data);
} else {
$saved_id = wp_insert_post($post_data);
}
if (is_wp_error($saved_id)) {
return $saved_id;
}
// 设置分类
if ($cat_id) {
wp_set_post_categories($saved_id, array($cat_id));
}
return $saved_id;
}
// 4. 前台加载编辑器所需资源
add_action(‘wp_enqueue_scripts’, function() {
if (is_page()) {
global $post;
if (has_shortcode($post->post_content, ‘classic_post_editor’)) {
wp_enqueue_editor();
wp_enqueue_media(); // 支持”添加媒体”按钮
}
}
});
/****************************************************/
add_action(‘wp_head’, function() {
if (is_page()) {
global $post;
if (has_shortcode($post->post_content, ‘classic_post_editor’)) {
echo ‘<style>
/* 强制显示第二行工具栏 */
#cpe-form .mce-toolbar-grp .mce-toolbar.mce-last {
display: block !important;
}
/* 隐藏wp_adv箭头(因为两行已强制显示,箭头没用了) */
#cpe-form .mce-toolbar-grp .mce-btn[aria-label=”工具栏切换”] {
display: none !important;
}
/* 发布按钮垂直居中修复 */
#cpe-form button[type=”submit”] {
display: inline-flex;
align-items: center;
justify-content: center;
line-height: 1 !important;
padding: 12px 28px !important;
vertical-align: middle !important;
min-height: 44px;
box-sizing: border-box;
}
</style>’;
}
}
});
/*======================================================================*/
// ========== 前台编辑器工具栏按钮(用过滤器,避免插件加载报错) ==========
function cpe_is_editor_page() {
if (!is_page()) return false;
global $post;
return has_shortcode($post->post_content, ‘classic_post_editor’);
}
// 第一行工具栏
add_filter(‘mce_buttons’, function($buttons) {
if (!cpe_is_editor_page()) return $buttons;
return array(
‘formatselect’, ‘bold’, ‘italic’, ‘underline’, ‘strikethrough’,
‘blockquote’, ‘bullist’, ‘numlist’,
‘alignleft’, ‘aligncenter’, ‘alignright’,
‘link’, ‘unlink’, ‘wp_adv’
);
}, 999);
// 第二行工具栏(含代码按钮)
add_filter(‘mce_buttons_2’, function($buttons) {
if (!cpe_is_editor_page()) return $buttons;
return array(
‘forecolor’, ‘backcolor’, ‘charmap’, ‘hr’,
‘code’, ‘removeformat’, ‘undo’, ‘redo’, ‘fullscreen’, ‘wp_help’
);
}, 999);








