2010-08-16 59 views

回答

0

首先安裝TinyMCE的高級插件。 第二個add 「theEditor」 類,你的textarea這樣

<textarea class="theEditor" name="custom_meta_box"></textarea> 

完蛋了 ;)

納比爾

25

http://codex.wordpress.org/Function_Reference/wp_editor是迄今爲止我發現最簡單的方法,因爲3.3內置到WordPress的(所以升級;-))

+0

不要忘記將'id'參數設置爲與空字符串不同的東西,否則它將無法工作... – 2012-08-08 14:45:53

3

但您需要用nl2br()函數替換演示文稿作爲textarea在自定義模板中有toogle JS問題,它將刪除您的所有<P> a nd <br/>標籤,因此所有換行符。

1

可以使用

add_action('edit_page_form', 'my_second_editor'); 
function my_second_editor() { 
    // get and set $content somehow... 
    wp_editor($content, 'mysecondeditor'); 
} 
27

這裏是全碼例如:

add_action('add_meta_boxes', function() { 
    add_meta_box('html_myid_61_section', 'TITLEEEEE', 'my_output_function'); 
}); 

function my_output_function($post) { 
    wp_editor(htmlspecialchars_decode(get_post_meta($post, 'SMTH_METANAME' , true)), 'mettaabox_ID', $settings = array('textarea_name'=>'MyInputNAME')); 
} 

add_action('save_post', function($post_id) { 
    if (!empty($_POST['MyInputNAME'])) { 
     $datta=htmlspecialchars($_POST['MyInputNAME']); 
     update_post_meta($post_id, 'SMTH_METANAME', $datta); 
    } 
}); 

P.S.從我的經驗必備建議:

忘記添加自定義代碼,使用Advanced Custom Fields,這是非常好的,並簡化你的生活。

+0

但是對於高級自定義字段,您必須爲中繼器字段付費30美元。我寧願做我自己的。 – Matthew 2017-06-25 20:14:03

+0

@Mthethew你寧願自己編碼,而不願付30美元?你認爲這需要多久才能實現? – 2017-07-04 14:54:07

+1

@DannyCoulombe它並沒有太長時間。這也是一個很好的學習經歷!如果你好奇,你可以看看[這裏](https://github.com/MatthewKosloski/wp-metabox-constructor-class) – Matthew 2017-07-14 20:24:15

1
// for custom post type 

function wo_second_editor($post) { 

    echo "<h3>Write here your text for the blue box on the right:</h3>"; 
    $content = get_post_meta($post->ID, 'wo_blue_box' , true) ; 
    wp_editor(htmlspecialchars_decode($content), 'wo_blue_box', array("media_buttons" => false)); 
} 

add_action('edit_form_advanced', 'wo_second_editor'); 


function wo_save_postdata($post_id, $post, $update) { 

    //... 

    if (!empty($_POST['wo_blue_box'])) { 
    $data=htmlspecialchars($_POST['wo_blue_box']); 
    update_post_meta($post_id, 'wo_blue_box', $data); 
    } 
} 

add_action('save_post', 'wo_save_postdata'); 


// Theme: 

<div class="blue"> 
    <?php 
    $content = get_post_meta(get_the_ID(), 'wo_blue_box' , true); 
    $content = htmlspecialchars_decode($content); 
    $content = wpautop($content); 
    echo $content; 
    ?> 
</div>