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

WordPress1 266阅读模式

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

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

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

// 添加添加面板和保存选项
if ( is_admin() ) {
	add_action( 'admin_init', 'zm_edit_in_html_create_options_box' );
	add_action( 'admin_head', 'zm_edit_in_html_handler' );
	add_action( 'save_post', 'zm_edit_in_html_save_postdata', $post );
}

// 关闭可视化
function zm_edit_in_html_handler() {
	global $post;

	// 检查这里是否有post对象,否则返回
	if( $post == null ) return;

	// 获取meta值并检查它是否已打开文本模式
	$editInHTML = zm_edit_in_html_get_html_edit_status( $post->ID );
	if ( $editInHTML ){
		// 隐藏“可视化”选项卡
		echo '<style type="text/css">';
		echo '#content-tmce.wp-switch-editor.switch-tmce{display:none;}';
		echo '</style>';

		// 将编辑器设置为文本模式
		add_filter( 'wp_default_editor', function () {
			return 'html';
		});
	}
}

// 将选项面板添加到所有编辑页面中
function zm_edit_in_html_create_options_box() {
	// 添加到页面
	add_meta_box( 'zm-edit-in-html', '文本模式', 'zm_edit_in_html_custom_box', 'page', 'side' );

	// 添加到文章
	add_meta_box( 'zm-edit-in-html', '文本模式', 'zm_edit_in_html_custom_box', 'post', 'side' );

	// 添加到所有文章类型
	$args = array(
		'public'   => true,
		'_builtin' => false
	);
	$post_types = get_post_types( $args, 'names' );

	foreach ( $post_types as $post_type ) {
		add_meta_box( 'zm-edit-in-html', '文本模式', 'zm_edit_in_html_custom_box', $post_type,'side' );
	}
}

//创建选项框
function zm_edit_in_html_custom_box( $post ){
	// 检查数据是否来自当前文章
	wp_nonce_field( basename( __FILE__ ), 'zm_edit_in_html_noncename' );

	// 获取当前文章的当前状态
	$editInHTML = zm_edit_in_html_get_html_edit_status( $post->ID );

	// 功能说明
	echo '<p>当前文章始终保持在文本编辑模式</p>';
	echo '<lazml class="selectit for="zm_edit_in_html">';
	echo '<input class="checkbox" type="checkbox" id="zm_edit_in_html" name="zm_edit_in_html" value="on" ';

	// If the option is currently zming used then check the options box
	if ( $editInHTML ){
		echo 'checked="checked"';
	}
	echo ' />';
	echo '文本模式</lazml>';
}

// 获取总是在HTML中编辑选项字段,并检查它是否已设置
function zm_edit_in_html_get_html_edit_status( $id ) {
	$editInHTML=get_post_meta( $id, 'editInHTML', true );

	if( $editInHTML === "on" ) {
		return true;
	}
	else{
		return false;
	}
}

// 保存选项
function zm_edit_in_html_save_postdata( $post_id ) {
	// 快速检查以确保数据属于当前文章
	if( !isset($_POST['zm_edit_in_html_noncename']) || !wp_verify_nonce( $_POST['zm_edit_in_html_noncename'], basename(__FILE__) ) ) {
		return $post_id;
	}

	// 对于自动保存操作不执行任何操作
	if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
		return $post_id;
	}

    // 检测有更新文章的权限
	if( 'page' === $_POST['post_type'] ){
		if( !current_user_can( 'edit_page', $post_id ) ) {
			return $post_id;
		}
	} else {
		if( !current_user_can( 'edit_post', $post_id ) ) {
			return $post_id;
		}
	}

    // 所有检查完成,保存选项。
	if( isset( $_POST['zm_edit_in_html'] ) ) {
		update_post_meta( $post_id, 'editInHTML', 'on' );
    } else {
		update_post_meta( $post_id, 'editInHTML', 'off' );
	}

	// 返回 $post_id 以保留其他过滤器。
	return $post_id;
}

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

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

代码源自Always Edit In HTML插件

本站文章大部分始于原创,用于个人学习记录,可能对您有所帮助,仅供参考!

weinxin
我的微信
版权声明
本站原创文章转载请注明文章出处及链接,谢谢合作!
 
知更鸟
    • hklbt
      hklbt 0

      学习学习…

    匿名

    发表评论

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

    拖动滑块以完成验证