修复重置按钮并去除预览功能

好的,我们来解决以下问题:

  1. 发布文章后加载的页面有误:可能是发布成功后跳转的链接有问题,或者是页面加载逻辑有误。
  2. 重置按钮失去作用:可能是重置按钮的逻辑没有正确绑定或执行。
  3. 加上之前的 CSS:确保样式表完整。

1. 修复发布后加载页面问题

发布成功后,页面会显示一个成功消息,并提供一个链接查看文章。如果链接有问题,可能是 get_permalink($post_id) 返回了错误的 URL。我们可以通过以下方式修复:

  • 确保 get_permalink($post_id) 返回正确的 URL。
  • 如果仍然有问题,可以手动构建文章链接。

修改发布成功后的代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
if ($post_id) {
// 分配标签
if (!empty($tags)) {
wp_set_post_tags($post_id, $tags);
}

// 获取文章链接
$post_url = get_permalink($post_id);
if (is_wp_error($post_url)) {
$post_url = home_url('/?p=' . $post_id); // 如果 get_permalink 失败,手动构建链接
}

echo '<div class="success">文章发布成功!<a href="' . esc_url($post_url) . '">查看文章</a></div>';
} else {
echo '<div class="error">文章发布失败,请重试。</div>';
}

2. 修复重置按钮失效问题

重置按钮的逻辑需要确保以下几点:

  • 清空所有输入字段。
  • 保留当前的发布模式。
  • 重置 TinyMCE 编辑器和 Markdown 输入框的内容。

以下是修复后的 resetForm() 函数:

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
// 重置表单(仅清空内容,保留当前模式)
function resetForm() {
// 清空标题
document.getElementById('title').value = '';

// 清空 TinyMCE 编辑器内容
if (tinymce.get('content')) {
tinymce.get('content').setContent('');
}

// 清空 Markdown 输入框
document.getElementById('markdown').value = '';

// 清空标签
document.getElementById('tags').value = '';

// 重置分类到第一个选项
document.getElementById('category').selectedIndex = 0;

// 不改变当前的发布模式
var currentMode = document.querySelector('input[name="publish_mode"]:checked').value;
if (currentMode === 'wysiwyg') {
// 确保所见即所得编辑器显示
document.getElementById('wysiwyg-section').style.display = 'block';
document.getElementById('markdown-section').style.display = 'none';
} else {
// 确保 Markdown 输入框显示
document.getElementById('wysiwyg-section').style.display = 'none';
document.getElementById('markdown-section').style.display = 'block';
}
}

3. 加上之前的 CSS

以下是完整的 CSS 样式,确保所有样式都被正确加载:

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
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #f5f5f5;
}
.publish-tool {
max-width: 800px;
margin: 40px auto;
padding: 30px;
background-color: #fff;
border-radius: 8px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}
.publish-tool h1 {
font-size: 24px;
margin-bottom: 20px;
color: #333;
text-align: center;
}
.publish-tool label {
display: block;
font-size: 14px;
font-weight: bold;
margin-bottom: 8px;
color: #555;
}
.publish-tool input[type="text"],
.publish-tool select,
.publish-tool textarea {
width: 100%;
padding: 10px;
font-size: 14px;
border: 1px solid #ddd;
border-radius: 4px;
margin-bottom: 20px;
box-sizing: border-box;
}
.publish-tool textarea[name="markdown"] {
height: 300px; /* 调整 Markdown 输入框高度 */
}
.publish-tool input[type="text"]:focus,
.publish-tool select:focus,
.publish-tool textarea:focus {
border-color: #0073aa;
outline: none;
}
.publish-tool .buttons {
display: flex;
justify-content: flex-end;
gap: 10px;
margin-top: 20px;
}
.publish-tool .buttons button {
padding: 10px 20px;
font-size: 14px;
border: none;
border-radius: 4px;
cursor: pointer;
transition: background-color 0.3s ease;
}
.publish-tool .buttons button[type="submit"] {
background-color: #0073aa;
color: #fff;
}
.publish-tool .buttons button[type="submit"]:hover {
background-color: #005177;
}
.publish-tool .buttons button[type="button"] {
background-color: #f0f0f0;
color: #333;
}
.publish-tool .buttons button[type="button"]:hover {
background-color: #ddd;
}
.success {
color: #28a745;
margin: 10px 0;
padding: 10px;
background-color: #e6ffed;
border: 1px solid #b2f2bb;
border-radius: 4px;
}
.error {
color: #dc3545;
margin: 10px 0;
padding: 10px;
background-color: #fff3f3;
border: 1px solid #ffc9c9;
border-radius: 4px;
}
.mode-selector {
margin-bottom: 20px;
}
.mode-selector label {
margin-right: 10px;
}
</style>

完整代码

以下是修复后的完整代码:

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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
<?php
// 加载 WordPress 环境
require_once('../wp-load.php');

// 引入 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 '<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; // 分类 ID
$tags = isset($_POST['tags']) ? sanitize_text_field($_POST['tags']) : ''; // 标签

// 判断发布方式
$publish_mode = isset($_POST['publish_mode']) ? sanitize_text_field($_POST['publish_mode']) : 'wysiwyg';

// 处理内容
if ($publish_mode === 'markdown2html') {
// Markdown 转换为 HTML 发布模式
$markdown = isset($_POST['markdown']) ? $_POST['markdown'] : '';
$parsedown = new Parsedown();
$content = $parsedown->text($markdown); // 将 Markdown 转换为 HTML
} elseif ($publish_mode === 'markdown') {
// 直接发布 Markdown 原文
$content = isset($_POST['markdown']) ? sanitize_textarea_field($_POST['markdown']) : '';
} else {
// 所见即所得发布模式
$content = isset($_POST['content']) ? wp_kses_post($_POST['content']) : '';
}

// 发布文章
$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);
}

// 获取文章链接
$post_url = get_permalink($post_id);
if (is_wp_error($post_url)) {
$post_url = home_url('/?p=' . $post_id); // 如果 get_permalink 失败,手动构建链接
}

echo '<div class="success">文章发布成功!<a href="' . esc_url($post_url) . '">查看文章</a></div>';
} 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>
<script>
tinymce.init({
selector: '#content', // 绑定到 textarea
height: 500, // 编辑器高度
menubar: false, // 隐藏菜单栏
plugins: [
'advlist autolink lists link image charmap print preview anchor',
'searchreplace visualblocks code fullscreen',
'insertdatetime media table paste code help wordcount'
],
toolbar: 'undo redo | formatselect | bold italic backcolor | \
alignleft aligncenter alignright alignjustify | \
bullist numlist outdent indent | removeformat | help',
init_instance_callback: function(editor) {
console.log('TinyMCE 初始化完成'); // 检查初始化是否成功
}
});

// 切换发布模式
function togglePublishMode() {
var mode = document.querySelector('input[name="publish_mode"]:checked').value;
var wysiwygSection = document.getElementById('wysiwyg-section');
var markdownSection = document.getElementById('markdown-section');

if (mode === 'wysiwyg') {
wysiwygSection.style.display = 'block';
markdownSection.style.display = 'none';
} else {
wysiwygSection.style.display = 'none';
markdownSection.style.display = 'block';
}
}

// 表单提交时同步 TinyMCE 内容
document.querySelector('form').onsubmit = function() {
var mode = document.querySelector('input[name="publish_mode"]:checked').value;
if (mode === 'wysiwyg') {
var content = tinymce.get('content').getContent(); // 获取 TinyMCE 内容
if (!content.trim()) {
alert('内容不能为空!');
return false; // 阻止表单提交
}
document.querySelector('textarea[name="content"]').value = content; // 同步到 textarea
} else if (mode === 'markdown' || mode === 'markdown2html') {
var markdown = document.getElementById('markdown').value;
if (!markdown.trim()) {
alert('Markdown 内容不能为空!');
return false; // 阻止表单提交
}
}
return true; // 允许表单提交
};

// 重置表单(仅清空内容,保留当前模式)
function resetForm() {
// 清空标题
document.getElementById('title').value = '';

// 清空 TinyMCE 编辑器内容
if (tinymce.get('content')) {
tinymce.get('content').setContent('');
}

// 清空 Markdown 输入框
document.getElementById('markdown').value = '';

// 清空标签
document.getElementById('tags').value = '';

// 重置分类到第一个选项
document.getElementById('category').selectedIndex = 0;

// 不改变当前的发布模式
var currentMode = document.querySelector('input[name="publish_mode"]:checked').value;
if (currentMode === 'wysiwyg') {
// 确保所见即所得编辑器显示
document.getElementById('wysiwyg-section').style.display = 'block';
document.getElementById('markdown-section').style.display = 'none';
} else {
// 确保 Markdown 输入框显示
document.getElementById('wysiwyg-section').style.display = 'none';
document.getElementById('markdown-section').style.display = 'block';
}
}
</script>
<style>
/* 之前的 CSS 样式 */
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #f5f5f5;
}
.publish-tool {
max-width: 800px;
margin: 40px auto;
padding: 30px;
background-color: #fff;
border-radius: 8px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}
.publish-tool h1 {
font-size: 24px;
margin-bottom: 20px;
color: #333;
text-align: center;
}
.publish-tool label {
display: block;
font-size: 14px;
font-weight: bold;
margin-bottom: 8px;
color: #555;
}
.publish-tool input[type="text"],
.publish-tool select,
.publish-tool textarea {
width: 100%;
padding: 10px;
font-size: 14px;
border: 1px solid #ddd;
border-radius: 4px;
margin-bottom: 20px;
box-sizing: border-box;
}
.publish-tool textarea[name="markdown"] {
height: 300px; /* 调整 Markdown 输入框高度 */
}
.publish-tool input[type="text"]:focus,
.publish-tool select:focus,
.publish-tool textarea:focus {
border-color: #0073aa;
outline: none;
}
.publish-tool .buttons {
display: flex;
justify-content: flex-end;
gap: 10px;
margin-top: 20px;
}
.publish-tool .buttons button {
padding: 10px 20px;
font-size: 14px;
border: none;
border-radius: 4px;
cursor: pointer;
transition: background-color 0.3s ease;
}
.publish-tool .buttons button[type="submit"] {
background-color: #0073aa;
color: #fff;
}
.publish-tool .buttons button[type="submit"]:hover {
background-color: #005177;
}
.publish-tool .buttons button[type="button"] {
background-color: #f0f0f0;
color: #333;
}
.publish-tool .buttons button[type="button"]:hover {
background-color: #ddd;
}
.success {
color: #28a745;
margin: 10px 0;
padding: 10px;
background-color: #e6ffed;
border: 1px solid #b2f2bb;
border-radius: 4px;
}
.error {
color: #dc3545;
margin: 10px 0;
padding: 10px;
background-color: #fff3f3;
border: 1px solid #ffc9c9;
border-radius: 4px;
}
.mode-selector {
margin-bottom: 20px;
}
.mode-selector label {
margin-right: 10px;
}
</style>
</head>
<body>
<div class="publish-tool">
<h1>文章发布工具</h1>
<form method="POST" action="" novalidate>
<?php wp_nonce_field('markdown_publish_action', 'markdown_publish_nonce'); ?>
<div class="mode-selector">
<label>
<input type="radio" name="publish_mode" value="wysiwyg" checked onchange="togglePublishMode()"> 所见即所得
</label>
<label>
<input type="radio" name="publish_mode" value="markdown2html" onchange="togglePublishMode()"> Markdown2html
</label>
<label>
<input type="radio" name="publish_mode" value="markdown" onchange="togglePublishMode()"> Markdown
</label>
</div>
<div>
<label for="title">标题:</label>
<input type="text" name="title" id="title" required>
</div>
<div id="wysiwyg-section">
<label for="content">内容:</label>
<textarea name="content" id="content" placeholder="在此输入内容..."></textarea>
</div>
<div id="markdown-section" style="display: none;">
<label for

**时间**: `2025-02-26T23:32:56`

---

## 👤 **用户**

这段代码的重置按钮功能似乎有问题,你能帮修复下吗?同时帮去除“Markdown 转换为 HTML 预览”,因为我需要的是所见即所得,markdown2htmlmarkdown原文发布到blog中的功能。
<?php
// 加载 WordPress 环境
require_once('../wp-load.php');

// 引入 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 '<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; // 分类 ID
$tags = isset($_POST['tags']) ? sanitize_text_field($_POST['tags']) : ''; // 标签

// 判断发布方式
$publish_mode = isset($_POST['publish_mode']) ? sanitize_text_field($_POST['publish_mode']) : 'wysiwyg';

// 处理内容
if ($publish_mode === 'markdown2html') {
// Markdown 转换为 HTML 发布模式
$markdown = $_POST['markdown'];
$parsedown = new Parsedown();
$content = $parsedown->text($markdown); // 将 Markdown 转换为 HTML
} elseif ($publish_mode === 'markdown') {
// 直接发布 Markdown 原文
$content = sanitize_textarea_field($_POST['markdown']); // 直接保存 Markdown 原文
} else {
// 所见即所得发布模式
$content = wp_kses_post($_POST['content']); // 清理 HTML 内容
}

// 发布文章
$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);
}

echo '<div class="success">文章发布成功!<a href="' . get_permalink($post_id) . '">查看文章</a></div>';
} 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>
<script>
tinymce.init({
selector: '#content', // 绑定到 textarea
height: 500, // 编辑器高度
menubar: false, // 隐藏菜单栏
plugins: [
'advlist autolink lists link image charmap print preview anchor',
'searchreplace visualblocks code fullscreen',
'insertdatetime media table paste code help wordcount'
],
toolbar: 'undo redo | formatselect | bold italic backcolor | \
alignleft aligncenter alignright alignjustify | \
bullist numlist outdent indent | removeformat | help',
init_instance_callback: function(editor) {
console.log('TinyMCE 初始化完成'); // 检查初始化是否成功
}
});

// 切换发布模式
function togglePublishMode() {
var mode = document.querySelector('input[name="publish_mode"]:checked').value;
var wysiwygSection = document.getElementById('wysiwyg-section');
var markdownSection = document.getElementById('markdown-section');

if (mode === 'wysiwyg') {
wysiwygSection.style.display = 'block';
markdownSection.style.display = 'none';
} else {
wysiwygSection.style.display = 'none';
markdownSection.style.display = 'block';
}
}

// 表单提交时同步 TinyMCE 内容
document.querySelector('form').onsubmit = function() {
var mode = document.querySelector('input[name="publish_mode"]:checked').value;
if (mode === 'wysiwyg') {
var content = tinymce.get('content').getContent(); // 获取 TinyMCE 内容
if (!content.trim()) {
alert('内容不能为空!');
return false; // 阻止表单提交
}
document.querySelector('textarea[name="content"]').value = content; // 同步到 textarea
}
};
</script>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #f5f5f5;
}
.publish-tool {
max-width: 800px;
margin: 40px auto;
padding: 30px;
background-color: #fff;
border-radius: 8px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}
.publish-tool h1 {
font-size: 24px;
margin-bottom: 20px;
color: #333;
text-align: center;
}
.publish-tool label {
display: block;
font-size: 14px;
font-weight: bold;
margin-bottom: 8px;
color: #555;
}
.publish-tool input[type="text"],
.publish-tool select,
.publish-tool textarea {
width: 100%;
padding: 10px;
font-size: 14px;
border: 1px solid #ddd;
border-radius: 4px;
margin-bottom: 20px;
box-sizing: border-box;
}
.publish-tool textarea[name="markdown"] {
height: 300px; /* 调整 Markdown 输入框高度 */
}
.publish-tool input[type="text"]:focus,
.publish-tool select:focus,
.publish-tool textarea:focus {
border-color: #0073aa;
outline: none;
}
.publish-tool .buttons {
display: flex;
justify-content: flex-end;
gap: 10px;
margin-top: 20px;
}
.publish-tool .buttons button {
padding: 10px 20px;
font-size: 14px;
border: none;
border-radius: 4px;
cursor: pointer;
transition: background-color 0.3s ease;
}
.publish-tool .buttons button[type="submit"] {
background-color: #0073aa;
color: #fff;
}
.publish-tool .buttons button[type="submit"]:hover {
background-color: #005177;
}
.publish-tool .buttons button[type="button"] {
background-color: #f0f0f0;
color: #333;
}
.publish-tool .buttons button[type="button"]:hover {
background-color: #ddd;
}
.success {
color: #28a745;
margin: 10px 0;
padding: 10px;
background-color: #e6ffed;
border: 1px solid #b2f2bb;
border-radius: 4px;
}
.error {
color: #dc3545;
margin: 10px 0;
padding: 10px;
background-color: #fff3f3;
border: 1px solid #ffc9c9;
border-radius: 4px;
}
.mode-selector {
margin-bottom: 20px;
}
.mode-selector label {
margin-right: 10px;
}
.output {
margin-top: 20px;
padding: 10px;
border: 1px solid #ccc;
background-color: #f9f9f9;
white-space: pre-wrap; /* 保留换行和空格 */
}
</style>
</head>
<body>
<div class="publish-tool">
<h1>文章发布工具</h1>
<form method="POST" action="" novalidate>
<?php wp_nonce_field('markdown_publish_action', 'markdown_publish_nonce'); ?>
<div class="mode-selector">
<label>
<input type="radio" name="publish_mode" value="wysiwyg" checked onchange="togglePublishMode()"> 所见即所得
</label>
<label>
<input type="radio" name="publish_mode" value="markdown2html" onchange="togglePublishMode()"> Markdown2html
</label>
<label>
<input type="radio" name="publish_mode" value="markdown" onchange="togglePublishMode()"> Markdown
</label>
</div>
<div>
<label for="title">标题:</label>
<input type="text" name="title" id="title" required>
</div>
<div id="wysiwyg-section">
<label for="content">内容:</label>
<textarea name="content" id="content" placeholder="在此输入内容..."></textarea>
</div>
<div id="markdown-section" style="display: none;">
<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>

<?php if (isset($markdown) && $publish_mode === 'markdown2html'): ?>
<div class="output">
<h2>Markdown 转换为 HTML 预览:</h2>
<pre><?php echo htmlspecialchars($parsedown->text($markdown)); ?></pre>
</div>
<?php endif; ?>
</div>
</body>
</html>

**时间**: `2025-02-26T23:04:54`

---


## 🤖 **DeepSeek (deepseek-chat)**

你提到的问题有两个:重置按钮功能有问题,以及需要去除“Markdown 转换为 HTML 预览”功能。下面我将分别解决这两个问题。

### 1. 修复重置按钮功能

重置按钮的功能可以通过 JavaScript 来实现。当前的代码中,`resetForm()` 函数没有定义,因此我们需要添加这个函数。这个函数的作用是清空表单中的所有输入字段,并将发布模式切换回默认的“所见即所得”模式。

### 2. 去除“Markdown 转换为 HTML 预览”功能

你提到不需要“Markdown 转换为 HTML 预览”功能,因此我们可以直接删除相关的代码。

以下是修改后的代码:

```php
<?php
// 加载 WordPress 环境
require_once('../wp-load.php');

// 引入 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 '<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; // 分类 ID
$tags = isset($_POST['tags']) ? sanitize_text_field($_POST['tags']) : ''; // 标签

// 判断发布方式
$publish_mode = isset($_POST['publish_mode']) ? sanitize_text_field($_POST['publish_mode']) : 'wysiwyg';

// 处理内容
if ($publish_mode === 'markdown2html') {
// Markdown 转换为 HTML 发布模式
$markdown = $_POST['markdown'];
$parsedown = new Parsedown();
$content = $parsedown->text($markdown); // 将 Markdown 转换为 HTML
} elseif ($publish_mode === 'markdown') {
// 直接发布 Markdown 原文
$content = sanitize_textarea_field($_POST['markdown']); // 直接保存 Markdown 原文
} else {
// 所见即所得发布模式
$content = wp_kses_post($_POST['content']); // 清理 HTML 内容
}

// 发布文章
$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);
}

echo '<div class="success">文章发布成功!<a href="' . get_permalink($post_id) . '">查看文章</a></div>';
} 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>
<script>
tinymce.init({
selector: '#content', // 绑定到 textarea
height: 500, // 编辑器高度
menubar: false, // 隐藏菜单栏
plugins: [
'advlist autolink lists link image charmap print preview anchor',
'searchreplace visualblocks code fullscreen',
'insertdatetime media table paste code help wordcount'
],
toolbar: 'undo redo | formatselect | bold italic backcolor | \
alignleft aligncenter alignright alignjustify | \
bullist numlist outdent indent | removeformat | help',
init_instance_callback: function(editor) {
console.log('TinyMCE 初始化完成'); // 检查初始化是否成功
}
});

// 切换发布模式
function togglePublishMode() {
var mode = document.querySelector('input[name="publish_mode"]:checked').value;
var wysiwygSection = document.getElementById('wysiwyg-section');
var markdownSection = document.getElementById('markdown-section');

if (mode === 'wysiwyg') {
wysiwygSection.style.display = 'block';
markdownSection.style.display = 'none';
} else {
wysiwygSection.style.display = 'none';
markdownSection.style.display = 'block';
}
}

// 表单提交时同步 TinyMCE 内容
document.querySelector('form').onsubmit = function() {
var mode = document.querySelector('input[name="publish_mode"]:checked').value;
if (mode === 'wysiwyg') {
var content = tinymce.get('content').getContent(); // 获取 TinyMCE 内容
if (!content.trim()) {
alert('内容不能为空!');
return false; // 阻止表单提交
}
document.querySelector('textarea[name="content"]').value = content; // 同步到 textarea
}
};

// 重置表单
function resetForm() {
document.querySelector('form').reset(); // 重置表单
tinymce.get('content').setContent(''); // 清空 TinyMCE 编辑器内容
document.getElementById('markdown').value = ''; // 清空 Markdown 输入框
document.querySelector('input[name="publish_mode"][value="wysiwyg"]').checked = true; // 切换回默认模式
togglePublishMode(); // 更新界面显示
}
</script>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #f5f5f5;
}
.publish-tool {
max-width: 800px;
margin: 40px auto;
padding: 30px;
background-color: #fff;
border-radius: 8px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}
.publish-tool h1 {
font-size: 24px;
margin-bottom: 20px;
color: #333;
text-align: center;
}
.publish-tool label {
display: block;
font-size: 14px;
font-weight: bold;
margin-bottom: 8px;
color: #555;
}
.publish-tool input[type="text"],
.publish-tool select,
.publish-tool textarea {
width: 100%;
padding: 10px;
font-size: 14px;
border: 1px solid #ddd;
border-radius: 4px;
margin-bottom: 20px;
box-sizing: border-box;
}
.publish-tool textarea[name="markdown"] {
height: 300px; /* 调整 Markdown 输入框高度 */
}
.publish-tool input[type="text"]:focus,
.publish-tool select:focus,
.publish-tool textarea:focus {
border-color: #0073aa;
outline: none;
}
.publish-tool .buttons {
display: flex;
justify-content: flex-end;
gap: 10px;
margin-top: 20px;
}
.publish-tool .buttons button {
padding: 10px 20px;
font-size: 14px;
border: none;
border-radius: 4px;
cursor: pointer;
transition: background-color 0.3s ease;
}
.publish-tool .buttons button[type="submit"] {
background-color: #0073aa;
color: #fff;
}
.publish-tool .buttons button[type="submit"]:hover {
background-color: #005177;
}
.publish-tool .buttons button[type="button"] {
background-color: #f0f0f0;
color: #333;
}
.publish-tool .buttons button[type="button"]:hover {
background-color: #ddd;
}
.success {
color: #28a745;
margin: 10px 0;
padding: 10px;
background-color: #e6ffed;
border: 1px solid #b2f2bb;
border-radius: 4px;
}
.error {
color: #dc3545;
margin: 10px 0;
padding: 10px;
background-color: #fff3f3;
border: 1px solid #ffc9c9;
border-radius: 4px;
}
.mode-selector {
margin-bottom: 20px;
}
.mode-selector label {
margin-right: 10px;
}
</style>
</head>
<body>
<div class="publish-tool">
<h1>文章发布工具</h1>
<form method="POST" action="" novalidate>
<?php wp_nonce_field('markdown_publish_action', 'markdown_publish_nonce'); ?>
<div class="mode-selector">
<label>
<input type="radio" name="publish_mode" value="wysiwyg" checked onchange="togglePublishMode()"> 所见即所得
</label>
<label>
<input type="radio" name="publish_mode" value="markdown2html" onchange="togglePublishMode()"> Markdown2html
</label>
<label>
<input type="radio" name="publish_mode" value="markdown" onchange="togglePublishMode()"> Markdown
</label>
</div>
<div>
<label for="title">标题:</label>
<input type="text" name="title" id="title" required>
</div>
<div id="wysiwyg-section">
<label for="content">内容:</label>
<textarea name="content" id="content" placeholder="在此输入内容..."></textarea>
</div>
<div id="markdown-section" style="display: none;">
<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>

主要修改点:

  1. 重置按钮功能:添加了 resetForm() 函数,用于清空表单内容并重置发布模式。
  2. 去除“Markdown 转换为 HTML 预览”功能:删除了相关的 HTML 和 PHP 代码。

现在,重置按钮应该可以正常工作,并且不再显示“Markdown 转换为 HTML 预览”部分。

时间: 2025-02-26T23:04:54


👤 用户

还是有问题,当我点击重置会自动默认选择所见即所得模式,能不能我选择某一模式后重置仅仅输入内容而不是连模式一起改变。

时间: 2025-02-26T23:10:58


🤖 DeepSeek (deepseek-chat)

明白了,你希望在点击重置按钮时,仅清空输入内容,而不改变当前的发布模式。我们可以通过调整 resetForm() 函数来实现这一点。

以下是修改后的代码:

修改后的 resetForm() 函数

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
// 重置表单(仅清空内容,保留当前模式)
function resetForm() {
// 清空标题
document.getElementById('title').value = '';

// 清空 TinyMCE 编辑器内容
tinymce.get('content').setContent('');

// 清空 Markdown 输入框
document.getElementById('markdown').value = '';

// 清空标签
document.getElementById('tags').value = '';

// 重置分类到第一个选项
document.getElementById('category').selectedIndex = 0;

// 不改变当前的发布模式
var currentMode = document.querySelector('input[name="publish_mode"]:checked').value;
if (currentMode === 'wysiwyg') {
tinymce.get('content').setContent(''); // 清空所见即所得编辑器
} else if (currentMode === 'markdown' || currentMode === 'markdown2html') {
document.getElementById('markdown').value = ''; // 清空 Markdown 输入框
}
}

完整代码更新

以下是更新后的完整代码,确保重置按钮仅清空内容而不改变模式:

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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<?php
// 加载 WordPress 环境
require_once('../wp-load.php');

// 引入 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 '<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; // 分类 ID
$tags = isset($_POST['tags']) ? sanitize_text_field($_POST['tags']) : ''; // 标签

// 判断发布方式
$publish_mode = isset($_POST['publish_mode']) ? sanitize_text_field($_POST['publish_mode']) : 'wysiwyg';

// 处理内容
if ($publish_mode === 'markdown2html') {
// Markdown 转换为 HTML 发布模式
$markdown = $_POST['markdown'];
$parsedown = new Parsedown();
$content = $parsedown->text($markdown); // 将 Markdown 转换为 HTML
} elseif ($publish_mode === 'markdown') {
// 直接发布 Markdown 原文
$content = sanitize_textarea_field($_POST['markdown']); // 直接保存 Markdown 原文
} else {
// 所见即所得发布模式
$content = wp_kses_post($_POST['content']); // 清理 HTML 内容
}

// 发布文章
$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);
}

echo '<div class="success">文章发布成功!<a href="' . get_permalink($post_id) . '">查看文章</a></div>';
} 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>
<script>
tinymce.init({
selector: '#content', // 绑定到 textarea
height: 500, // 编辑器高度
menubar: false, // 隐藏菜单栏
plugins: [
'advlist autolink lists link image charmap print preview anchor',
'searchreplace visualblocks code fullscreen',
'insertdatetime media table paste code help wordcount'
],
toolbar: 'undo redo | formatselect | bold italic backcolor | \
alignleft aligncenter alignright alignjustify | \
bullist numlist outdent indent | removeformat | help',
init_instance_callback: function(editor) {
console.log('TinyMCE 初始化完成'); // 检查初始化是否成功
}
});

// 切换发布模式
function togglePublishMode() {
var mode = document.querySelector('input[name="publish_mode"]:checked').value;
var wysiwygSection = document.getElementById('wysiwyg-section');
var markdownSection = document.getElementById('markdown-section');

if (mode === 'wysiwyg') {
wysiwygSection.style.display = 'block';
markdownSection.style.display = 'none';
} else {
wysiwygSection.style.display = 'none';
markdownSection.style.display = 'block';
}
}

// 表单提交时同步 TinyMCE 内容
document.querySelector('form').onsubmit = function() {
var mode = document.querySelector('input[name="publish_mode"]:checked').value;
if (mode === 'wysiwyg') {
var content = tinymce.get('content').getContent(); // 获取 TinyMCE 内容
if (!content.trim()) {
alert('内容不能为空!');
return false; // 阻止表单提交
}
document.querySelector('textarea[name="content"]').value = content; // 同步到 textarea
}
};

// 重置表单(仅清空内容,保留当前模式)
function resetForm() {
// 清空标题
document.getElementById('title').value = '';

// 清空 TinyMCE 编辑器内容
tinymce.get('content').setContent('');

// 清空 Markdown 输入框
document.getElementById('markdown').value = '';

// 清空标签
document.getElementById('tags').value = '';

// 重置分类到第一个选项
document.getElementById('category').selectedIndex = 0;

// 不改变当前的发布模式
var currentMode = document.querySelector('input[name="publish_mode"]:checked').value;
if (currentMode === 'wysiwyg') {
tinymce.get('content').setContent(''); // 清空所见即所得编辑器
} else if (currentMode === 'markdown' || currentMode === 'markdown2html') {
document.getElementById('markdown').value = ''; // 清空 Markdown 输入框
}
}
</script>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #f5f5f5;
}
.publish-tool {
max-width: 800px;
margin: 40px auto;
padding: 30px;
background-color: #fff;
border-radius: 8px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}
.publish-tool h1 {
font-size: 24px;
margin-bottom: 20px;
color: #333;
text-align: center;
}
.publish-tool label {
display: block;
font-size: 14px;
font-weight: bold;
margin-bottom: 8px;
color: #555;
}
.publish-tool input[type="text"],
.publish-tool select,
.publish-tool textarea {
width: 100%;
padding: 10px;
font-size: 14px;
border: 1px solid #ddd;
border-radius: 4px;
margin-bottom: 20px;
box-sizing: border-box;
}
.publish-tool textarea[name="markdown"] {
height: 300px; /* 调整 Markdown 输入框高度 */
}
.publish-tool input[type="text"]:focus,
.publish-tool select:focus,
.publish-tool textarea:focus {
border-color: #0073aa;
outline: none;
}
.publish-tool .buttons {
display: flex;
justify-content: flex-end;
gap: 10px;
margin-top: 20px;
}
.publish-tool .buttons button {
padding: 10px 20px;
font-size: 14px;
border: none;
border-radius: 4px;
cursor: pointer;
transition: background-color 0.3s ease;
}
.publish-tool .buttons button[type="submit"] {
background-color: #0073aa;
color: #fff;
}
.publish-tool .buttons button[type="submit"]:hover {
background-color: #005177;
}
.publish-tool .buttons button[type="button"] {
background-color: #f0f0f0;
color: #333;
}
.publish-tool .buttons button[type="button"]:hover {
background-color: #ddd;
}
.success {
color: #28a745;
margin: 10px 0;
padding: 10px;
background-color: #e6ffed;
border: 1px solid #b2f2bb;
border-radius: 4px;
}
.error {
color: #dc3545;
margin: 10px 0;
padding: 10px;
background-color: #fff3f3;
border: 1px solid #ffc9c9;
border-radius: 4px;
}
.mode-selector {
margin-bottom: 20px;
}
.mode-selector label {
margin-right: 10px;
}
</style>
</head>
<body>
<div class="publish-tool">
<h1>文章发布工具</h1>
<form method="POST" action="" novalidate>
<?php wp_nonce_field('markdown_publish_action', 'markdown_publish_nonce'); ?>
<div class="mode-selector">
<label>
<input type="radio" name="publish_mode" value="wysiwyg" checked onchange="togglePublishMode()"> 所见即所得
</label>
<label>
<input type="radio" name="publish_mode" value="markdown2html" onchange="togglePublishMode()"> Markdown2html
</label>
<label>
<input type="radio" name="publish_mode" value="markdown" onchange="togglePublishMode()"> Markdown
</label>
</div>
<div>
<label for="title">标题:</label>
<input type="text" name="title" id="title" required>
</div>
<div id="wysiwyg-section">
<label for="content">内容:</label>
<textarea name="content" id="content" placeholder="在此输入内容..."></textarea>
</div>
<div id="markdown-section" style="display: none;">
<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>

修改后的功能:

  1. 重置按钮:仅清空输入内容(标题、内容、标签、分类),不会改变当前的发布模式
  2. 保留模式:重置后,用户选择的发布模式(所见即所得、Markdown2html 或 Markdown)保持不变。

现在,重置按钮的行为应该符合你的需求了!

时间: 2025-02-26T23:10:58


👤 用户

你帮仔细看下上面的代码,貌似发布到blog不成功啊,只有标题,没有文章内容啊。

时间: 2025-02-26T23:21:31


🤖 DeepSeek (deepseek-chat)

好的,我来仔细检查一下代码。你提到的问题是:发布到博客时,只有标题,没有文章内容。这可能是由于以下原因之一:

  1. 内容未正确传递到 wp_insert_post:可能是表单提交时,内容字段没有正确同步到 PHP 处理逻辑中。
  2. TinyMCE 内容未同步到 <textarea>:在表单提交时,TinyMCE 编辑器的内容需要同步到 <textarea>,否则 PHP 无法获取到内容。
  3. 发布模式逻辑问题:可能是发布模式切换时,内容字段没有正确处理。

修复步骤

1. 确保 TinyMCE 内容同步到 <textarea>

在表单提交时,我们需要确保 TinyMCE 的内容同步到 <textarea>,否则 PHP 无法获取到内容。修改表单提交的逻辑如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
document.querySelector('form').onsubmit = function() {
var mode = document.querySelector('input[name="publish_mode"]:checked').value;
if (mode === 'wysiwyg') {
var content = tinymce.get('content').getContent(); // 获取 TinyMCE 内容
if (!content.trim()) {
alert('内容不能为空!');
return false; // 阻止表单提交
}
document.querySelector('textarea[name="content"]').value = content; // 同步到 textarea
} else if (mode === 'markdown' || mode === 'markdown2html') {
var markdown = document.getElementById('markdown').value;
if (!markdown.trim()) {
alert('Markdown 内容不能为空!');
return false; // 阻止表单提交
}
}
return true; // 允许表单提交
};

2. 确保内容字段正确传递到 PHP

在 PHP 处理逻辑中,确保内容字段被正确获取。修改如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
// 处理内容
if ($publish_mode === 'markdown2html') {
// Markdown 转换为 HTML 发布模式
$markdown = isset($_POST['markdown']) ? $_POST['markdown'] : '';
$parsedown = new Parsedown();
$content = $parsedown->text($markdown); // 将 Markdown 转换为 HTML
} elseif ($publish_mode === 'markdown') {
// 直接发布 Markdown 原文
$content = isset($_POST['markdown']) ? sanitize_textarea_field($_POST['markdown']) : '';
} else {
// 所见即所得发布模式
$content = isset($_POST['content']) ? wp_kses_post($_POST['content']) : '';
}

3. 调试输出

为了确保内容被正确获取,可以在发布文章前添加调试输出:

1
2
3
4
5
6
// 调试输出
error_log("Title: " . $title);
error_log("Content: " . $content);
error_log("Category: " . $category);
error_log("Tags: " . $tags);
error_log("Publish Mode: " . $publish_mode);

4. 完整修复后的代码

以下是修复后的完整代码:

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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
// 加载 WordPress 环境
require_once('../wp-load.php');

// 引入 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 '<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; // 分类 ID
$tags = isset($_POST['tags']) ? sanitize_text_field($_POST['tags']) : ''; // 标签

// 判断发布方式
$publish_mode = isset($_POST['publish_mode']) ? sanitize_text_field($_POST['publish_mode']) : 'wysiwyg';

// 处理内容
if ($publish_mode === 'markdown2html') {
// Markdown 转换为 HTML 发布模式
$markdown = isset($_POST['markdown']) ? $_POST['markdown'] : '';
$parsedown = new Parsedown();
$content = $parsedown->text($markdown); // 将 Markdown 转换为 HTML
} elseif ($publish_mode === 'markdown') {
// 直接发布 Markdown 原文
$content = isset($_POST['markdown']) ? sanitize_textarea_field($_POST['markdown']) : '';
} else {
// 所见即所得发布模式
$content = isset($_POST['content']) ? wp_kses_post($_POST['content']) : '';
}

// 调试输出
error_log("Title: " . $title);
error_log("Content: " . $content);
error_log("Category: " . $category);
error_log("Tags: " . $tags);
error_log("Publish Mode: " . $publish_mode);

// 发布文章
$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);
}

echo '<div class="success">文章发布成功!<a href="' . get_permalink($post_id) . '">查看文章</a></div>';
} 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>
<script>
tinymce.init({
selector: '#content', // 绑定到 textarea
height: 500, // 编辑器高度
menubar: false, // 隐藏菜单栏
plugins: [
'advlist autolink lists link image charmap print preview anchor',
'searchreplace visualblocks code fullscreen',
'insertdatetime media table paste code help wordcount'
],
toolbar: 'undo redo | formatselect | bold italic backcolor | \
alignleft aligncenter alignright alignjustify | \
bullist numlist outdent indent | removeformat | help',
init_instance_callback: function(editor) {
console.log('TinyMCE 初始化完成'); // 检查初始化是否成功
}
});

// 切换发布模式
function togglePublishMode() {
var mode = document.querySelector('input[name="publish_mode"]:checked').value;
var wysiwygSection = document.getElementById('wysiwyg-section');
var markdownSection = document.getElementById('markdown-section');

if (mode === 'wysiwyg') {
wysiwygSection.style.display = 'block';
markdownSection.style.display = 'none';
} else {
wysiwygSection.style.display = 'none';
markdownSection.style.display = 'block';
}
}

// 表单提交时同步 TinyMCE 内容
document.querySelector('form').onsubmit = function() {
var mode = document.querySelector('input[name="publish_mode"]:checked').value;
if (mode === 'wysiwyg') {
var content = tinymce.get('content').getContent(); // 获取 TinyMCE 内容
if (!content.trim()) {
alert('内容不能为空!');
return false; // 阻止表单提交
}
document.querySelector('textarea[name="content"]').value = content; // 同步到 textarea
} else if (mode === 'markdown' || mode === 'markdown2html') {
var markdown = document.getElementById('markdown').value;
if (!markdown.trim()) {
alert('Markdown 内容不能为空!');
return false; // 阻止表单提交
}
}
return true; // 允许表单提交
};

// 重置表单(仅清空内容,保留当前模式)
function resetForm() {
// 清空标题
document.getElementById('title').value = '';

// 清空 TinyMCE 编辑器内容
tinymce.get('content').setContent('');

// 清空 Markdown 输入框
document.getElementById('markdown').value = '';

// 清空标签
document.getElementById('tags').value = '';

// 重置分类到第一个选项
document.getElementById('category').selectedIndex = 0;
}
</script>
<style>
/* 样式保持不变 */
</style>
</head>
<body>
<div class="publish-tool">
<h1>文章发布工具</h1>
<form method="POST" action="" novalidate>
<?php wp_nonce_field('markdown_publish_action', 'markdown_publish_nonce'); ?>
<div class="mode-selector">
<label>
<input type="radio" name="publish_mode" value="wysiwyg" checked onchange="togglePublishMode()"> 所见即所得
</label>
<label>
<input type="radio" name="publish_mode" value="markdown2html" onchange="togglePublishMode()"> Markdown2html
</label>
<label>
<input type="radio" name="publish_mode" value="markdown" onchange="togglePublishMode()"> Markdown
</label>
</div>
<div>
<label for="title">标题:</label>
<input type="text" name="title" id="title" required>
</div>
<div id="wysiwyg-section">
<label for="content">内容:</label>
<textarea name="content" id="content" placeholder="在此输入内容..."></textarea>
</div>
<div id="markdown-section" style="display: none;">
<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>

关键修复点:

  1. 表单提交时同步 TinyMCE 内容:确保所见即所得模式下的内容被正确同步到 <textarea>
  2. PHP 处理逻辑中检查内容字段:确保内容字段被正确获取和处理。
  3. 调试输出:添加了调试输出,方便排查问题。

测试步骤:

  1. 在所见即所得模式下输入内容并发布,检查文章内容是否正确。
  2. 在 Markdown 或 Markdown2html 模式下输入内容并发布,检查文章内容是否正确。
  3. 检查调试输出,确保所有字段都被正确获取。

如果问题仍然存在,请检查 WordPress 的错误日志或启用调试模式,查看是否有其他错误信息。

时间: 2025-02-26T23:21:31


👤 用户

恩,上述修改过后确实有文章内容了。就是发布文章内容后加载的页面似乎有误,且重置按钮失去了作用。此外,帮我加上之前的css

时间: 2025-02-26T23:32:56


🤖 DeepSeek (deepseek-chat)

好的,我们来解决以下问题:

  1. 发布文章后加载的页面有误:可能是发布成功后跳转的链接有问题,或者是页面加载逻辑有误。
  2. 重置按钮失去作用:可能是重置按钮的逻辑没有正确绑定或执行。
  3. 加上之前的 CSS:确保样式表完整。

1. 修复发布后加载页面问题

发布成功后,页面会显示一个成功消息,并提供一个链接查看文章。如果链接有问题,可能是 get_permalink($post_id) 返回了错误的 URL。我们可以通过以下方式修复:

  • 确保 get_permalink($post_id) 返回正确的 URL。
  • 如果仍然有问题,可以手动构建文章链接。

修改发布成功后的代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
if ($post_id) {
// 分配标签
if (!empty($tags)) {
wp_set_post_tags($post_id, $tags);
}

// 获取文章链接
$post_url = get_permalink($post_id);
if (is_wp_error($post_url)) {
$post_url = home_url('/?p=' . $post_id); // 如果 get_permalink 失败,手动构建链接
}

echo '<div class="success">文章发布成功!<a href="' . esc_url($post_url) . '">查看文章</a></div>';
} else {
echo '<div class="error">文章发布失败,请重试。</div>';
}

2. 修复重置按钮失效问题

重置按钮的逻辑需要确保以下几点:

  • 清空所有输入字段。
  • 保留当前的发布模式。
  • 重置 TinyMCE 编辑器和 Markdown 输入框的内容。

以下是修复后的 resetForm() 函数:

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
// 重置表单(仅清空内容,保留当前模式)
function resetForm() {
// 清空标题
document.getElementById('title').value = '';

// 清空 TinyMCE 编辑器内容
if (tinymce.get('content')) {
tinymce.get('content').setContent('');
}

// 清空 Markdown 输入框
document.getElementById('markdown').value = '';

// 清空标签
document.getElementById('tags').value = '';

// 重置分类到第一个选项
document.getElementById('category').selectedIndex = 0;

// 不改变当前的发布模式
var currentMode = document.querySelector('input[name="publish_mode"]:checked').value;
if (currentMode === 'wysiwyg') {
// 确保所见即所得编辑器显示
document.getElementById('wysiwyg-section').style.display = 'block';
document.getElementById('markdown-section').style.display = 'none';
} else {
// 确保 Markdown 输入框显示
document.getElementById('wysiwyg-section').style.display = 'none';
document.getElementById('markdown-section').style.display = 'block';
}
}

3. 加上之前的 CSS

以下是完整的 CSS 样式,确保所有样式都被正确加载:

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
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #f5f5f5;
}
.publish-tool {
max-width: 800px;
margin: 40px auto;
padding: 30px;
background-color: #fff;
border-radius: 8px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}
.publish-tool h1 {
font-size: 24px;
margin-bottom: 20px;
color: #333;
text-align: center;
}
.publish-tool label {
display: block;
font-size: 14px;
font-weight: bold;
margin-bottom: 8px;
color: #555;
}
.publish-tool input[type="text"],
.publish-tool select,
.publish-tool textarea {
width: 100%;
padding: 10px;
font-size: 14px;
border: 1px solid #ddd;
border-radius: 4px;
margin-bottom: 20px;
box-sizing: border-box;
}
.publish-tool textarea[name="markdown"] {
height: 300px; /* 调整 Markdown 输入框高度 */
}
.publish-tool input[type="text"]:focus,
.publish-tool select:focus,
.publish-tool textarea:focus {
border-color: #0073aa;
outline: none;
}
.publish-tool .buttons {
display: flex;
justify-content: flex-end;
gap: 10px;
margin-top: 20px;
}
.publish-tool .buttons button {
padding: 10px 20px;
font-size: 14px;
border: none;
border-radius: 4px;
cursor: pointer;
transition: background-color 0.3s ease;
}
.publish-tool .buttons button[type="submit"] {
background-color: #0073aa;
color: #fff;
}
.publish-tool .buttons button[type="submit"]:hover {
background-color: #005177;
}
.publish-tool .buttons button[type="button"] {
background-color: #f0f0f0;
color: #333;
}
.publish-tool .buttons button[type="button"]:hover {
background-color: #ddd;
}
.success {
color: #28a745;
margin: 10px 0;
padding: 10px;
background-color: #e6ffed;
border: 1px solid #b2f2bb;
border-radius: 4px;
}
.error {
color: #dc3545;
margin: 10px 0;
padding: 10px;
background-color: #fff3f3;
border: 1px solid #ffc9c9;
border-radius: 4px;
}
.mode-selector {
margin-bottom: 20px;
}
.mode-selector label {
margin-right: 10px;
}
</style>

完整代码

以下是修复后的完整代码:

<?php
// 加载 WordPress 环境
require_once('../wp-load.php');

// 引入 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 '<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; // 分类 ID
        $tags = isset($_POST['tags']) ? sanitize_text_field($_POST['tags']) : ''; // 标签

        // 判断发布方式
        $publish_mode = isset($_POST['publish_mode']) ? sanitize_text_field($_POST['publish_mode']) : 'wysiwyg';

        // 处理内容
        if ($publish_mode === 'markdown2html') {
            // Markdown 转换为 HTML 发布模式
            $markdown = isset($_POST['markdown']) ? $_POST['markdown'] : '';
            $parsedown = new Parsedown();
            $content = $parsedown->text($markdown); // 将 Markdown 转换为 HTML
        } elseif ($publish_mode === 'markdown') {
            // 直接发布 Markdown 原文
            $content = isset($_POST['markdown']) ? sanitize_textarea_field($_POST['markdown']) : '';
        } else {
            // 所见即所得发布模式
            $content = isset($_POST['content']) ? wp_kses_post($_POST['content']) : '';
        }

        // 发布文章
        $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);
            }

            // 获取文章链接
            $post_url = get_permalink($post_id);
            if (is_wp_error($post_url)) {
                $post_url = home_url('/?p=' . $post_id); // 如果 get_permalink 失败,手动构建链接
            }

            echo '<div class="success">文章发布成功!<a href="' . esc_url($post_url) . '">查看文章</a></div>';
        } 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>
    <script>
        tinymce.init({
            selector: '#content', // 绑定到 textarea
            height: 500, // 编辑器高度
            menubar: false, // 隐藏菜单栏
            plugins: [
                'advlist autolink lists link image charmap print preview anchor',
                'searchreplace visualblocks code fullscreen',
                'insertdatetime media table paste code help wordcount'
            ],
            toolbar: 'undo redo | formatselect | bold italic backcolor | \
                       alignleft aligncenter alignright alignjustify | \
                       bullist numlist outdent indent | removeformat | help',
            init_instance_callback: function(editor) {
                console.log('TinyMCE 初始化完成'); // 检查初始化是否成功
            }
        });

        // 切换发布模式
        function togglePublishMode() {
            var mode = document.querySelector('input[name="publish_mode"]:checked').value;
            var wysiwygSection = document.getElementById('wysiwyg-section');
            var markdownSection = document.getElementById('markdown-section');

            if (mode === 'wysiwyg') {
                wysiwygSection.style.display = 'block';
                markdownSection.style.display = 'none';
            } else {
                wysiwygSection.style.display = 'none';
                markdownSection.style.display = 'block';
            }
        }

        // 表单提交时同步 TinyMCE 内容
        document.querySelector('form').onsubmit = function() {
            var mode = document.querySelector('input[name="publish_mode"]:checked').value;
            if (mode === 'wysiwyg') {
                var content = tinymce.get('content').getContent(); // 获取 TinyMCE 内容
                if (!content.trim()) {
                    alert('内容不能为空!');
                    return false; // 阻止表单提交
                }
                document.querySelector('textarea[name="content"]').value = content; // 同步到 textarea
            } else if (mode === 'markdown' || mode === 'markdown2html') {
                var markdown = document.getElementById('markdown').value;
                if (!markdown.trim()) {
                    alert('Markdown 内容不能为空!');
                    return false; // 阻止表单提交
                }
            }
            return true; // 允许表单提交
        };

        // 重置表单(仅清空内容,保留当前模式)
        function resetForm() {
            // 清空标题
            document.getElementById('title').value = '';

            // 清空 TinyMCE 编辑器内容
            if (tinymce.get('content')) {
                tinymce.get('content').setContent('');
            }

            // 清空 Markdown 输入框
            document.getElementById('markdown').value = '';

            // 清空标签
            document.getElementById('tags').value = '';

            // 重置分类到第一个选项
            document.getElementById('category').selectedIndex = 0;

            // 不改变当前的发布模式
            var currentMode = document.querySelector('input[name="publish_mode"]:checked').value;
            if (currentMode === 'wysiwyg') {
                // 确保所见即所得编辑器显示
                document.getElementById('wysiwyg-section').style.display = 'block';
                document.getElementById('markdown-section').style.display = 'none';
            } else {
                // 确保 Markdown 输入框显示
                document.getElementById('wysiwyg-section').style.display = 'none';
                document.getElementById('markdown-section').style.display = 'block';
            }
        }
    </script>
    <style>
        /* 之前的 CSS 样式 */
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f5f5f5;
        }
        .publish-tool {
            max-width: 800px;
            margin: 40px auto;
            padding: 30px;
            background-color: #fff;
            border-radius: 8px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }
        .publish-tool h1 {
            font-size: 24px;
            margin-bottom: 20px;
            color: #333;
            text-align: center;
        }
        .publish-tool label {
            display: block;
            font-size: 14px;
            font-weight: bold;
            margin-bottom: 8px;
            color: #555;
        }
        .publish-tool input[type="text"],
        .publish-tool select,
        .publish-tool textarea {
            width: 100%;
            padding: 10px;
            font-size: 14px;
            border: 1px solid #ddd;
            border-radius: 4px;
            margin-bottom: 20px;
            box-sizing: border-box;
        }
        .publish-tool textarea[name="markdown"] {
            height: 300px; /* 调整 Markdown 输入框高度 */
        }
        .publish-tool input[type="text"]:focus,
        .publish-tool select:focus,
        .publish-tool textarea:focus {
            border-color: #0073aa;
            outline: none;
        }
        .publish-tool .buttons {
            display: flex;
            justify-content: flex-end;
            gap: 10px;
            margin-top: 20px;
        }
        .publish-tool .buttons button {
            padding: 10px 20px;
            font-size: 14px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            transition: background-color 0.3s ease;
        }
        .publish-tool .buttons button[type="submit"] {
            background-color: #0073aa;
            color: #fff;
        }
        .publish-tool .buttons button[type="submit"]:hover {
            background-color: #005177;
        }
        .publish-tool .buttons button[type="button"] {
            background-color: #f0f0f0;
            color: #333;
        }
        .publish-tool .buttons button[type="button"]:hover {
            background-color: #ddd;
        }
        .success {
            color: #28a745;
            margin: 10px 0;
            padding: 10px;
            background-color: #e6ffed;
            border: 1px solid #b2f2bb;
            border-radius: 4px;
        }
        .error {
            color: #dc3545;
            margin: 10px 0;
            padding: 10px;
            background-color: #fff3f3;
            border: 1px solid #ffc9c9;
            border-radius: 4px;
        }
        .mode-selector {
            margin-bottom: 20px;
        }
        .mode-selector label {
            margin-right: 10px;
        }
    </style>
</head>
<body>
    <div class="publish-tool">
        <h1>文章发布工具</h1>
        <form method="POST" action="" novalidate>
            <?php wp_nonce_field('markdown_publish_action', 'markdown_publish_nonce'); ?>
            <div class="mode-selector">
                <label>
                    <input type="radio" name="publish_mode" value="wysiwyg" checked onchange="togglePublishMode()"> 所见即所得
                </label>
                <label>
                    <input type="radio" name="publish_mode" value="markdown2html" onchange="togglePublishMode()"> Markdown2html
                </label>
                <label>
                    <input type="radio" name="publish_mode" value="markdown" onchange="togglePublishMode()"> Markdown
                </label>
            </div>
            <div>
                <label for="title">标题:</label>
                <input type="text" name="title" id="title" required>
            </div>
            <div id="wysiwyg-section">
                <label for="content">内容:</label>
                <textarea name="content" id="content" placeholder="在此输入内容..."></textarea>
            </div>
            <div id="markdown-section" style="display: none;">
                <label for

**时间**: `2025-02-26T23:32:56`

---



---

## 对话统计

- **消息总数**: 8 条
- **处理状态**: 成功转换