你好。我有两段代码,主要是通过其向wordpress发布文章内容

服务器繁忙,请稍后再试。

时间: 2025-02-26T22:29:48


👤 用户

你好。我有两段代码,主要是通过其向wordpress发布文章内容的,我想把他合成一个程序,然后弄个勾选框,勾选所见及所得发布或者markdown转换为html发布,这样就合并在一起了。最好再给我加个markdown格式直接发布。两端代码我将在下面发给你。

时间: 2025-02-26T21:56:14


🤖 DeepSeek (deepseek-chat)

你好!很高兴能帮你解决这个问题。你可以将两段代码合并成一个程序,并通过勾选框来选择发布方式(所见即所得或Markdown转换为HTML)。以下是实现这个功能的步骤:

1. 合并代码并添加勾选框

假设你已经有了两段代码,一段是所见即所得发布,另一段是Markdown转换为HTML发布。我们可以将它们合并,并通过勾选框来选择发布方式。

2. 添加Markdown格式直接发布

你还可以添加一个选项,允许用户直接以Markdown格式发布文章。

3. 示例代码

以下是一个示例代码,假设你已经有了两段代码的基本逻辑:

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
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
import tkinter as tk
from tkinter import messagebox
import markdown

# 假设这是所见即所得发布的函数
def publish_wysiwyg(content):
# 这里是所见即所得发布的逻辑
print("发布所见即所得内容:", content)

# 假设这是Markdown转换为HTML发布的函数
def publish_markdown_to_html(content):
# 将Markdown转换为HTML
html_content = markdown.markdown(content)
# 这里是发布HTML内容的逻辑
print("发布Markdown转换为HTML内容:", html_content)

# 假设这是直接发布Markdown的函数
def publish_markdown_directly(content):
# 这里是直接发布Markdown内容的逻辑
print("直接发布Markdown内容:", content)

# 创建主窗口
root = tk.Tk()
root.title("WordPress 文章发布工具")

# 创建勾选框变量
wysiwyg_var = tk.BooleanVar()
markdown_to_html_var = tk.BooleanVar()
markdown_direct_var

**时间**: `2025-02-26T21:56:14`

---


## 👤 **用户**

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

// 检查是否提交了表单
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['content']) && 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']);
$content = wp_kses_post($_POST['content']); // 清理 HTML 内容
$category = isset($_POST['category']) ? intval($_POST['category']) : 0; // 分类 ID
$tags = isset($_POST['tags']) ? sanitize_text_field($_POST['tags']) : ''; // 标签

// 发布文章
$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 初始化完成'); // 检查初始化是否成功
}
});

// 表单提交时同步 TinyMCE 内容
document.querySelector('form').onsubmit = function() {
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 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;
}
</style>
<script>
// 重置表单
function resetForm() {
tinymce.get('content').setContent(''); // 清空 TinyMCE 内容
document.querySelector('input[name="title"]').value = '';
document.querySelector('select[name="category"]').selectedIndex = 0;
document.querySelector('input[name="tags"]').value = '';
}
</script>
</head>
<body>
<div class="publish-tool">
<h1>所见即所得发布工具</h1>
<form method="POST" action="" novalidate>
<?php wp_nonce_field('markdown_publish_action', 'markdown_publish_nonce'); ?>
<div>
<label for="title">标题:</label>
<input type="text" name="title" id="title" required>
</div>
<div>
<label for="content">内容:</label>
<textarea name="content" id="content" placeholder="在此输入内容..."></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>

**时间**: `2025-02-26T21:56:52`

---


## 👤 **用户**

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

// 引入 Parsedown 库(用于解析 Markdown)
require_once 'Parsedown.php';

// 检查是否提交了表单
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['markdown']) && 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']);
$markdown = $_POST['markdown'];
$category = isset($_POST['category']) ? intval($_POST['category']) : 0; // 分类 ID
$tags = isset($_POST['tags']) ? sanitize_text_field($_POST['tags']) : ''; // 标签

// 使用 Parsedown 解析 Markdown
$parsedown = new Parsedown();
$content = $parsedown->text($markdown); // 直接保存为 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>Markdown 发布工具</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #f5f5f5;
}
.markdown-publish {
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);
}
.markdown-publish h1 {
font-size: 24px;
margin-bottom: 20px;
color: #333;
text-align: center;
}
.markdown-publish label {
display: block;
font-size: 14px;
font-weight: bold;
margin-bottom: 8px;
color: #555;
}
.markdown-publish input[type="text"],
.markdown-publish select,
.markdown-publish textarea {
width: 100%;
padding: 10px;
font-size: 14px;
border: 1px solid #ddd;
border-radius: 4px;
margin-bottom: 20px;
box-sizing: border-box;
}
.markdown-publish input[type="text"]:focus,
.markdown-publish select:focus,
.markdown-publish textarea:focus {
border-color: #0073aa;
outline: none;
}
.markdown-publish .buttons {
display: flex;
justify-content: flex-end;
gap: 10px;
margin-top: 20px;
}
.markdown-publish .buttons button {
padding: 10px 20px;
font-size: 14px;
border: none;
border-radius: 4px;
cursor: pointer;
transition: background-color 0.3s ease;
}
.markdown-publish .buttons button[type="submit"] {
background-color: #0073aa;
color: #fff;
}
.markdown-publish .buttons button[type="submit"]:hover {
background-color: #005177;
}
.markdown-publish .buttons button[type="button"] {
background-color: #f0f0f0;
color: #333;
}
.markdown-publish .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;
}
</style>
<script>
// 重置表单
function resetForm() {
document.querySelector('textarea[name="markdown"]').value = '';
document.querySelector('input[name="title"]').value = '';
document.querySelector('select[name="category"]').selectedIndex = 0;
document.querySelector('input[name="tags"]').value = '';
}
</script>
</head>
<body>
<div class="markdown-publish">
<h1>Markdown 发布工具</h1>
<form method="POST">
<?php wp_nonce_field('markdown_publish_action', 'markdown_publish_nonce'); ?>
<div>
<label for="title">标题:</label>
<input type="text" name="title" id="title" required>
</div>
<div>
<label for="markdown">Markdown 内容:</label>
<textarea name="markdown" id="markdown" placeholder="在此输入 Markdown 文本..." required></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>

**时间**: `2025-02-26T21:57:15`

---


## 👤 **用户**

好了。两段代码都已经发给你了,你帮看一下 ,如何合并

**时间**: `2025-02-26T21:57:53`

---


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

好的!请将两段代码发给我,我会帮你分析并合并它们。你可以直接将代码粘贴在这里,我会根据代码的结构和逻辑进行合并,并添加勾选框功能来实现所见即所得发布或Markdown转换为HTML发布的功能。

请将代码发给我吧!

**时间**: `2025-02-26T21:57:53`

---


## 👤 **用户**

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

// 引入 Parsedown 库(用于解析 Markdown)
require_once 'Parsedown.php';

// 检查是否提交了表单
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['markdown']) && 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']);
$markdown = $_POST['markdown'];
$category = isset($_POST['category']) ? intval($_POST['category']) : 0; // 分类 ID
$tags = isset($_POST['tags']) ? sanitize_text_field($_POST['tags']) : ''; // 标签

// 使用 Parsedown 解析 Markdown
$parsedown = new Parsedown();
$content = $parsedown->text($markdown); // 直接保存为 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>Markdown 发布工具</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #f5f5f5;
}
.markdown-publish {
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);
}
.markdown-publish h1 {
font-size: 24px;
margin-bottom: 20px;
color: #333;
text-align: center;
}
.markdown-publish label {
display: block;
font-size: 14px;
font-weight: bold;
margin-bottom: 8px;
color: #555;
}
.markdown-publish input[type="text"],
.markdown-publish select,
.markdown-publish textarea {
width: 100%;
padding: 10px;
font-size: 14px;
border: 1px solid #ddd;
border-radius: 4px;
margin-bottom: 20px;
box-sizing: border-box;
}
.markdown-publish input[type="text"]:focus,
.markdown-publish select:focus,
.markdown-publish textarea:focus {
border-color: #0073aa;
outline: none;
}
.markdown-publish .buttons {
display: flex;
justify-content: flex-end;
gap: 10px;
margin-top: 20px;
}
.markdown-publish .buttons button {
padding: 10px 20px;
font-size: 14px;
border: none;
border-radius: 4px;
cursor: pointer;
transition: background-color 0.3s ease;
}
.markdown-publish .buttons button[type="submit"] {
background-color: #0073aa;
color: #fff;
}
.markdown-publish .buttons button[type="submit"]:hover {
background-color: #005177;
}
.markdown-publish .buttons button[type="button"] {
background-color: #f0f0f0;
color: #333;
}
.markdown-publish .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;
}
</style>
<script>
// 重置表单
function resetForm() {
document.querySelector('textarea[name="markdown"]').value = '';
document.querySelector('input[name="title"]').value = '';
document.querySelector('select[name="category"]').selectedIndex = 0;
document.querySelector('input[name="tags"]').value = '';
}
</script>
</head>
<body>
<div class="markdown-publish">
<h1>Markdown 发布工具</h1>
<form method="POST">
<?php wp_nonce_field('markdown_publish_action', 'markdown_publish_nonce'); ?>
<div>
<label for="title">标题:</label>
<input type="text" name="title" id="title" required>
</div>
<div>
<label for="markdown">Markdown 内容:</label>
<textarea name="markdown" id="markdown" placeholder="在此输入 Markdown 文本..." required></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>

第二段

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

// 检查是否提交了表单
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['content']) && 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']);
$content = wp_kses_post($_POST['content']); // 清理 HTML 内容
$category = isset($_POST['category']) ? intval($_POST['category']) : 0; // 分类 ID
$tags = isset($_POST['tags']) ? sanitize_text_field($_POST['tags']) : ''; // 标签

// 发布文章
$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 初始化完成'); // 检查初始化是否成功
}
});

// 表单提交时同步 TinyMCE 内容
document.querySelector('form').onsubmit = function() {
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 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;
}
</style>
<script>
// 重置表单
function resetForm() {
tinymce.get('content').setContent(''); // 清空 TinyMCE 内容
document.querySelector('input[name="title"]').value = '';
document.querySelector('select[name="category"]').selectedIndex = 0;
document.querySelector('input[name="tags"]').value = '';
}
</script>
</head>
<body>
<div class="publish-tool">
<h1>所见即所得发布工具</h1>
<form method="POST" action="" novalidate>
<?php wp_nonce_field('markdown_publish_action', 'markdown_publish_nonce'); ?>
<div>
<label for="title">标题:</label>
<input type="text" name="title" id="title" required>
</div>
<div>
<label for="content">内容:</label>
<textarea name="content" id="content" placeholder="在此输入内容..."></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>

**时间**: `2025-02-26T21:58:34`

---


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

感谢提供代码!我已经理解了两段代码的功能:

1. **第一段代码**:支持通过 Markdown 格式发布文章,使用 `Parsedown` 库将 Markdown 转换为 HTML 后发布。
2. **第二段代码**:支持所见即所得(WYSIWYG)编辑器(TinyMCE)发布文章,直接发布 HTML 内容。

接下来,我会将这两段代码合并为一个程序,并添加一个勾选框,让用户选择发布方式(Markdown 或所见即所得)。以下是合并后的代码:

---

### 合并后的代码

```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 === 'markdown') {
// Markdown 发布模式
$markdown = $_POST['markdown'];
$parsedown = new Parsedown();
$content = $parsedown->text($markdown); // 将 Markdown 转换为 HTML
} 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 markdownSection = document.getElementById('markdown-section');
var wysiwygSection = document.getElementById('wysiwyg-section');

if (mode === 'markdown') {
markdownSection.style.display = 'block';
wysiwygSection.style.display = 'none';
} else {
markdownSection.style.display = 'none';
wysiwygSection.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 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="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 编辑器发布 HTML 内容。
      • Markdown:使用 Markdown 编辑器发布内容,并自动转换为 HTML。
  2. 动态切换

    • 当用户切换发布模式时,页面会动态显示对应的编辑器(TinyMCE 或 Markdown 输入框)。
  3. 表单提交

    • 根据用户选择的发布模式,处理不同的内容格式并发布文章。
  4. 安全性

    • 使用 WordPress 的 wp_verify_noncesanitize_text_field 等函数确保安全性。

希望这段代码能满足你的需求!如果有任何问题或需要进一步调整,请随时告诉我!

时间: 2025-02-26T21:58:34


👤 用户

帮我把Markdown选择框修改成Markdown2html,然后添加一个Markdown原文格式的发布功能,选择按钮就是Markdown,然后在根据下面我给的一段代码整合进去。
<?php
// 引入 Parsedown 库(用于解析 Markdown)
require_once ‘Parsedown.php’;

// 检查是否提交了 Markdown 文本
if ($_SERVER[‘REQUEST_METHOD’] === ‘POST’ && isset($_POST[‘markdown’])) {
$markdown = $_POST[‘markdown’];
// 使用 Parsedown 解析 Markdown
$parsedown = new Parsedown();
$html = $parsedown->text($markdown);
// 将 HTML 转换为纯文本
$text = strip_tags($html);
} else {
$markdown = ‘’;
$text = ‘’;
}
?>

Markdown 转换为文本