原始程式。可以在此下載
<?php /* Plugin Name: PowenKo Page POST Meta Plugin URI: httpss://wordpress.org/plugins/hello-dolly/ Description: This is not just a plugin, it symbolizes the hope and enthusiasm of an entire generation summed up in two words sung most famously by Louis Armstrong: Hello, Dolly. When activated you will randomly see a lyric from <cite>Hello, Dolly</cite> in the upper right of your admin screen on every page. Author: Powen Ko Version: 1.6 Author URI: httpss://ma.tt/ */ /* 小範例:建立英文選單標題 */ add_action('admin_init', 'admin_init_fn'); // 指定後台初始化時要執行我們自訂的函式admin_init_fn function admin_init_fn() { // 使用add_meta_box來增加一個「自訂區塊」,在callback的參數位置,放上我們自訂的函式 EnTitleCB_fn // add_meta_box('EnTitle', 'Powen Ko 英文標題', 'EnTitleCB_fn', 'page', 'normal', 'high', null); // add_meta_box('EnTitle', 'Powen Ko 英文標題', 'EnTitleCB_fn', 'post', 'normal', 'high', null); $types = array( 'post', 'page', 'custom-type' ); foreach( $types as $type ) { // add_meta_box('EnTitle', 'Powen Ko 英文標題', 'EnTitleCB_fn', $type, 'side'); add_meta_box('EnTitle', 'Powen Ko 英文標題', 'EnTitleCB_fn', $type, 'normal', 'high', null); } } // 當WP要畫出自訂區塊時,就會執行add_meta_box指定的這個callback函式 function EnTitleCB_fn() { // 需要取得目前編輯的post資訊,如id global $post; // 把我們自訂的Post Meta從資料庫取出,待會放進欄位 // 看你需要多少欄位,就要執行幾次get_post_meta()來取值 $entitle_cnt = get_post_meta( $post->ID, 'EnTitle', true ); ?> <th scope="row">主選單英文標題:</th> <td> <!-- 開始畫出UI,看你需要多少欄位,就畫多少input出來 --> <label for="EnTitle"> <input id="EnTitle" type="text" size="75" name="EnTitle" value="<?php echo $entitle_cnt;?>" /> <br /><em>說明:設定分頁出現在選單時的英文翻譯。</em> </label> </td> </tr> <?php } /////////////// add_action('wp_insert_post', 'insert_post_fn', 10, 2); // 讓WP在新增文章內容時,也執行我們定義的insert_post_fn function insert_post_fn($post_id, $post = null) { // 'EnTitle'是我們定義的input的name,如果還有其他的欄位,就把它們的name加入。 // 如 array("EnTitle","aaa","bbb"); 下方的程式碼就會以迴圈來處理陣列中的每個input值 $meta_post_fields = array("EnTitle"); $goloop = true; // 如果是我們要處理的內容型別-page 就處理它 if ($post->post_type == "page") { $mfs = $meta_post_fields; $goloop = true; } if ($post->post_type == "post") { $mfs = $meta_post_fields; $goloop = true; } // $mfs = $meta_post_fields; if ($goloop) { // 取得Post進來的欄位資料,針對我們自訂的欄位$meta_fields foreach ($mfs as $key) { // 如果有空值就不進行處理 $value = @$_POST[$key]; if (empty($value)) { delete_post_meta($post_id, $key); continue; } // 如果丟過來的參數不是陣列 if (!is_array($value)) { // 更新資料,若無法更新,代表是要新增資料 if (!update_post_meta($post_id, $key, $value)) { add_post_meta($post_id, $key, $value); } } else { // 如果丟過來的參數是陣列,先刪除之前的值,再一個個建進相同名稱的post meta delete_post_meta($post_id, $key); // Loop through the array adding new values to the post meta as different entries with the same name foreach ($value as $entry) { add_post_meta($post_id, $key, $entry); } } } } } /////////////// function remove_post_custom_fields() { remove_meta_box( 'postcustom' , 'post' , 'normal' ); } add_action( 'admin_menu' , 'remove_post_custom_fields' ); // This just echoes the chosen line, we'll position it later function hello_dolly($input) { // 需要取得目前編輯的post資訊,如id global $post; echo $input ; echo '主選單英文標題:'.get_post_meta( $post->ID, 'EnTitle', true ); } add_filter( 'the_content', 'hello_dolly' ); ?>