咱們在使用wordpress作網站的時候,不免有一些須要在後臺設置側欄菜單下添加自定義字段的狀況。下面就簡單說說一下,如何在後臺設置側欄菜單下添加自定義字段?php
在這裏咱們主要是使用wordpress的add_action(),下面經過本身的代碼來簡單說明一下。wordpress
個人作法是:首先在本身的模板中新建一個setContent.php文件,(不新建也能夠把代碼直接寫在functions.php裏)。函數
setContent.php代碼:網站
function customSetting(){ ?> <div class="wrap"> <h2>通用內容設置</h2> <?php if ($_POST['update_options']=='true') {//若提交了表單,則保存變量 update_option('site-content', $_POST['site-content']); //若值爲空,則刪除這行數據 if( empty($_POST['site-content']) ) delete_option('site-content' ); echo '<div id="message" class="updated below-h2"><p>Saved!</p></div>';//保存完畢顯示文字提示 } //下面開始界面表單 ?> <form method="POST" action=""> <input type="hidden" name="update_options" value="true" /> <table class="form-table"> <tr> <th scope="row">網站介紹</th> <td colspan="">網站描述: <textarea name="site-content"id="site-content" value="<?php echo get_option('site-content'); ?>"><?php echo get_option('site-content'); ?></textarea> </td> </tr> </table> <p><input type="submit" class="button-primary" name="admin_options" value="Update"/></p> </form> </div> <?php add_action('admin_menu', 'customSetting'); } ?>
functions.php代碼:spa
function options_admin_menu(){ add_submenu_page( 'options-general.php','通用內容設置', '通用內容設置', 'administrator', 'custom-setting', 'customSetting' ); } // 經過add_action來自動調用options_admin_menu函數 add_action('admin_menu', 'options_admin_menu'); include_once('setContent.php'); ?>
效果圖:orm
咱們在setContent.php自定義好字段之後,要在前臺頁面裏顯示出來,只需在你調用的地方使用blog
<?php echo get_option( ‘site-content’ );?>,那麼上圖中的網站描述就能夠顯示出來了。get