問題描述
嘿,我已經將 TinyMCE 新增到自定義元框中顯示的某些文字區域。所有的格式化工作都是完美的,除了編輯器不會儲存<p> 或<br/> 標籤。它不保留換行符。
TinyMCE 設定如下:
wp_tiny_mce(true, array('editor_selector' => $field['class'] ) );
'<textarea name="', $field['id'], '" class="', $field['class'], '" id="', $field['id'], '" cols="60" rows="8" style="width:97%">', $meta ? esc_html($meta) : $field['std'], '</textarea>';
這一切都很好。除了<P> 和<BR> 標籤之外,所有格式化按鈕都可以正常工作。
我不知道編輯器是否在儲存帖子元素之前或之後剝離它們。
想法?
最佳解決方案
我最近有這個工作。您應該使用您的元框名稱搜尋並替換 metaname 。
儲存格式的關鍵是在儲存資料時使用 wpautop(); 。
add_action( 'add_meta_boxes', 'add_metaname_box');
add_action( 'save_post', 'metaname_save');
function add_metaname_box() {
add_meta_box(
'metaname_id',
__( 'metaname text', 'metaname_textdomain'),
'metaname_custom_box',
'page'
);
}
function metaname_custom_box() {
global $post;
wp_nonce_field( plugin_basename( __FILE__ ), 'metaname_noncename' );
$data = get_post_meta($post->ID, 'metaname_custom_box', true);
echo <<<EOT
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery("#metaname_custom_box").addClass("mceEditor");
if ( typeof( tinyMCE ) == "object" &&
typeof( tinyMCE.execCommand ) == "function" ) {
tinyMCE.execCommand("mceAddControl", false, "metaname_custom_box");
}
});
</script>
<textarea id="metaname_custom_box" name="metaname_custom_box">$data</textarea>
EOT;
}
function metaname_save($post_id) {
global $post;
// Verify
if ( !wp_verify_nonce( $_POST['metaname_noncename'], plugin_basename(__FILE__) )) {
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;
}
$key = 'metaname_custom_box';
$data = wpautop($_POST[$key]);
// New, Update, and Delete
if(get_post_meta($post_id, $key) == "")
add_post_meta($post_id, $key, $data, true);
elseif($data != get_post_meta($post_id, $key, true))
update_post_meta($post_id, $key, $data);
elseif($data == "")
delete_post_meta($post_id, $key, get_post_meta($post_id, $key, true));
}
次佳解決方案
這是 (pared-down 版本) 我用於 custom-configure TinyMCE:
// http://tinymce.moxiecode.com/wiki.php/Configuration
function cbnet_tinymce_config( $init ) {
// Don't remove line breaks
$init['remove_linebreaks'] = false;
// Pass $init back to WordPress
return $init;
}
add_filter('tiny_mce_before_init', 'cbnet_tinymce_config');
我以為這是你試過的?
編輯:
您可能需要包括一些其他配置更改,例如:
// Convert newline characters to BR tags
$init['convert_newlines_to_brs'] = true;
// Do not remove redundant BR tags
$init['remove_redundant_brs'] = false;
使用 TinyMCE configuration parameters 玩,並找到需要更改的那個。
第三種解決方案
發現這可能是一個更簡單的解決方法:
在實際的模板上,更改:
<?php echo get_the_content());?>
到這個:
<?php echo wpautop(get_the_content());?>
這樣 wpautop() 將 TinyMCE 剝離的標籤新增到模板的模板上。
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。