让WordPress文章始终保持在文本编辑模式—转载

木子 电脑网络评论163阅读模式

有时需要在文章输入一些HTML代码,一般我们会切换到文本编辑模式,但当你切换到可视化模式发表更新文章后,这些 HTML 代码会被WordPress过滤掉而丢失,那么如何防止误操作切换到可视化模式呢,WordPress本身也为我们准备了选项,进入人个资料 → 可视化编辑器,勾选“撰写文章时不使用可视化编辑器 ”。

这样编辑文章时仅有文本模式了,编辑其它文章时想使用可视化模式,还要改回来,那如何仅让指定的文章使用文本模式,而不影响其它文章使用可视化模式呢?可以用下面的代码实现。

将下面代码添加到当前主题函数模板functions.php中:

  1. // 添加添加面板和保存选项
  2. if ( is_admin() ) {
  3. add_action( 'admin_init', 'zm_edit_in_html_create_options_box' );
  4. add_action( 'admin_head', 'zm_edit_in_html_handler' );
  5. add_action( 'save_post', 'zm_edit_in_html_save_postdata', $post );
  6. }
  7.  
  8. // 关闭可视化
  9. function zm_edit_in_html_handler() {
  10. global $post;
  11.  
  12. // 检查这里是否有post对象,否则返回
  13. if( $post == null ) return;
  14.  
  15. // 获取meta值并检查它是否已打开文本模式
  16. $editInHTML = zm_edit_in_html_get_html_edit_status( $post->ID );
  17. if ( $editInHTML ){
  18. // 隐藏“可视化”选项卡
  19. echo '<style type="text/css">';
  20. echo '#content-tmce.wp-switch-editor.switch-tmce{display:none;}';
  21. echo '</style>';
  22.  
  23. // 将编辑器设置为文本模式
  24. add_filter( 'wp_default_editor', function () {
  25. return 'html';
  26. });
  27. }
  28. }
  29.  
  30. // 将选项面板添加到所有编辑页面中
  31. function zm_edit_in_html_create_options_box() {
  32. // 添加到页面
  33. add_meta_box( 'zm-edit-in-html', '文本模式', 'zm_edit_in_html_custom_box', 'page', 'side' );
  34.  
  35. // 添加到文章
  36. add_meta_box( 'zm-edit-in-html', '文本模式', 'zm_edit_in_html_custom_box', 'post', 'side' );
  37.  
  38. // 添加到所有文章类型
  39. $args = array(
  40. 'public' => true,
  41. '_builtin' => false
  42. );
  43. $post_types = get_post_types( $args, 'names' );
  44.  
  45. foreach ( $post_types as $post_type ) {
  46. add_meta_box( 'zm-edit-in-html', '文本模式', 'zm_edit_in_html_custom_box', $post_type,'side' );
  47. }
  48. }
  49.  
  50. //创建选项框
  51. function zm_edit_in_html_custom_box( $post ){
  52. // 检查数据是否来自当前文章
  53. wp_nonce_field( basename( __FILE__ ), 'zm_edit_in_html_noncename' );
  54.  
  55. // 获取当前文章的当前状态
  56. $editInHTML = zm_edit_in_html_get_html_edit_status( $post->ID );
  57.  
  58. // 功能说明
  59. echo '<p>当前文章始终保持在文本编辑模式</p>';
  60. echo '<lazml class="selectit for="zm_edit_in_html">';
  61. echo '<input class="checkbox" type="checkbox" id="zm_edit_in_html" name="zm_edit_in_html" value="on" ';
  62.  
  63. // If the option is currently zming used then check the options box
  64. if ( $editInHTML ){
  65. echo 'checked="checked"';
  66. }
  67. echo ' />';
  68. echo '文本模式</lazml>';
  69. }
  70.  
  71. // 获取总是在HTML中编辑选项字段,并检查它是否已设置
  72. function zm_edit_in_html_get_html_edit_status( $id ) {
  73. $editInHTML=get_post_meta( $id, 'editInHTML', true );
  74.  
  75. if( $editInHTML === "on" ) {
  76. return true;
  77. }
  78. else{
  79. return false;
  80. }
  81. }
  82.  
  83. // 保存选项
  84. function zm_edit_in_html_save_postdata( $post_id ) {
  85. // 快速检查以确保数据属于当前文章
  86. if( !isset($_POST['zm_edit_in_html_noncename']) || !wp_verify_nonce( $_POST['zm_edit_in_html_noncename'], basename(__FILE__) ) ) {
  87. return $post_id;
  88. }
  89.  
  90. // 对于自动保存操作不执行任何操作
  91. if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
  92. return $post_id;
  93. }
  94.  
  95. // 检测有更新文章的权限
  96. if( 'page' === $_POST['post_type'] ){
  97. if( !current_user_can( 'edit_page', $post_id ) ) {
  98. return $post_id;
  99. }
  100. } else {
  101. if( !current_user_can( 'edit_post', $post_id ) ) {
  102. return $post_id;
  103. }
  104. }
  105.  
  106. // 所有检查完成,保存选项。
  107. if( isset( $_POST['zm_edit_in_html'] ) ) {
  108. update_post_meta( $post_id, 'editInHTML', 'on' );
  109. } else {
  110. update_post_meta( $post_id, 'editInHTML', 'off' );
  111. }
  112.  
  113. // 返回 $post_id 以保留其他过滤器。
  114. return $post_id;
  115. }

上述代码,会在经典编辑器右侧出现文本模式面板,勾选“文本模式”,更新发表文章后,再次编辑该文章,将始终处在文本编辑模式,而并不会影响其它文章,在可视化和文本间切换。

我的微信
微信扫一扫
weinxin
13832413877
我的微信公众号
微信扫一扫
weinxin
我的公众号
 
匿名

发表评论

匿名网友
:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

拖动滑块以完成验证