这篇文章有一些解决方案,但作者希望改进它
阅读了很多文档和论坛后,我无法找到解决问题的完美方案。出于某种原因,我想在管理面板中的主题选项中使用tinyMCE。我添加了很小的页面,它显示良好,但当我提交 - 它没有保存任何东西。当然,我检查了可靠的数据并确认没有提交新的数据。所以,我编写的解决方案只是将数据从tinyMCE复制到隐藏字段,然后将隐藏字段存储到数据库。
这里有一些代码:
主题options.php
<?php wp_editor($options['king_text1'], 'kingtext', array(
'wpautop' => 1,
'media_buttons' => 1,
'textarea_name' => '',
'textarea_rows' => 20,
'tabindex' => null,
'editor_css' => '',
'editor_class' => 'tiny_kingtext',
'teeny' => 0,
'dfw' => 0,
'tinymce' => 1,
'quicktags' => 1,
'drag_drop_upload' => false
) ); ?>
<textarea id="kingtext" class="large-text" cols="50" rows="10"><?php echo esc_attr_e( $options['king_text1'] ); ?> </textarea>
<input type="hidden" id="myText" name="king_theme_options[king_text1]">
然后我在主题选项文件的头部添加了添加自定义脚本到管理面板的功能:
function javascript_and_styles() {
wp_enqueue_script( 'admin-js', get_template_directory_uri() . '/js/admin-panel.js', array(), '', true );
}
add_action('admin_enqueue_scripts', 'javascript_and_styles');
之后我添加了一些脚本,在提交之前将数据从mce复制到隐藏输入:
jQuery(function() {
jQuery('input.button').click(function(e) {
e.preventDefault();
var content = tinyMCE.activeEditor.getContent();
jQuery('#myText').val(content);
jQuery('#myform').submit();
});
});
在此之后一切正常,但我不喜欢这段代码和这种解决问题的方法。如果有人知道更有趣的解决方案,请展示它。 最好的问候
答案 0 :(得分:0)
我刚刚发现了所有的错误。这个问题已经解决了!
清楚地了解wp_editor中的每个属性非常重要。我的问题是我使用了textarea和wp_editor,这就是为什么我遇到了上面描述过的麻烦。所以,如果有人和我有同样的情况,我想向你提出这个建议:
您不得在代码中使用真正的textarea。你可以这样开始wp_editor:
<?php wp_editor($options['king_text1'], 'kingtext', array(
'wpautop' => 1,
'media_buttons' => 1,
'textarea_name' => 'king_theme_options[king_text1]', //This is important line!!!
'textarea_rows' => 20,
'tabindex' => null,
'editor_css' => '',
'editor_class' => 'tiny_kingtext',
'teeny' => 0,
'dfw' => 0,
'tinymce' => 1,
'quicktags' => 1,
'drag_drop_upload' => false
) );
毕竟没有其他厚度是必要的。一切运作良好:打开,保存等。