生活随笔
收集整理的這篇文章主要介紹了
WordPress中使主题支持小工具以及添加插件启用函数
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
https://www.jb51.net/article/76810.htm
?
這篇文章主要介紹了WordPress中使主題支持widget以及添加插件啟用函數(shù)的方法,使WP可以使用小工具widget與通過register_activation_hook()來添加啟用插件的函數(shù),需要的朋友可以參考下
?
讓主題支持小工具
WordPress 的小工具(widget)是一大特色,它讓用戶自由拖動組合內容,而且任何插件和主題都可以添加一個額外的小工具,增加擴展性。
默認情況下,一個主題并不會支持小工具,需要主題開發(fā)者啟用小工具功能并把小工具在相應的前臺位置調用出來,這樣用戶才能在后臺直接拖動生成側邊欄。
本文就來教你如何激活小工具功能,并且添加一個側邊欄,最后在前臺顯示出來。
注冊側邊欄
默認的,后臺外觀下是沒有 “小工具” 這個菜單按鈕的,如果想要讓他出現(xiàn),就至少需要注冊一個側邊欄,否則即使顯示出來,也沒有用。
注冊一個側邊欄需要使用 register_sidebar() 函數(shù),用法比較簡單,只有一個屬性,填上需要的信息就行了。
1.在主題function中添加以下代碼
<?php
/**在function中添加以下代碼
*WordPress添加額外選項字段到常規(guī)設置頁面
* http://www.wpdaxue.com/add-field-to-general-settings-page.html
*/
$new_general_setting = new new_general_setting();
class new_general_setting {
function new_general_setting(){
add_filter('admin_init', array(&$this ,'register_logo'));
}
function register_logo(){
//需要'js/uploader.js組件
wp_enqueue_script('fli-upload-js', $this->url .'js/uploader.js', array('jquery','media-upload','thickbox'));
wp_enqueue_style('thickbox');
wp_enqueue_style('fli-upload-css', $this->url .'css/uploader.css');
register_setting('general','logo','esc_attr');
add_settings_field('logo','<label for="logo">'.__('網站Logo').'</label>', array(&$this,'logo_fields_html'),'general');
}
function logo_fields_html(){
$value = get_option('logo','');
echo '<input type="text" class="regular-text ltr" id="logo" name="logo" maxlength="200" value="'. $value .'" readonly/> <input type="button" id="general_logo" class="button insert-media add_media" value="上傳">';
}
}
//自定義后臺Css和Js
add_action('admin_enqueue_scripts','myAdminScripts');
function myAdminScripts(){
//主題下加載admin.js
wp_register_script('default', get_template_directory_uri().'/admin.js', array(),'','all');
wp_enqueue_script('default');
wp_register_style('default', get_template_directory_uri().'/admin.css', array(),'','all');
wp_enqueue_style('default');
}
?>
2.在主題admin.js中添加代碼
options_general();
//在常規(guī)選項頁面添加自定義信息
function options_general (){
if(!Islocatl_pathname('options-general.php'))return;
//點擊上傳按鈕或input元素時打開上傳窗口
jQuery('#general_logo,#logo').click(function(){
//打開上傳窗口需要js/uploader.js組件
tb_show('','media-upload.php?type=image&TB_iframe=true');
returnfalse;
});
//圖片上傳頁面回傳
//html:為選擇的圖片元素
window.send_to_editor =function(html){
imgurl = jQuery(html).attr('src');
// 保存值并寫入optuions表
jQuery('#logo').val(imgurl);
//刪除圖片上傳窗口
tb_remove();
returnfalse;
}//end send_to_editor
}
//當前頁面是否是指定的頁面
functionIslocatl_pathname(pathname){
return location.pathname.indexOf(pathname)>=0;
}//end 當前頁面是否是指定的頁面
?
效果圖:
?
參考:
進階教程(三十四):調用新版的媒體中心上傳圖片
定制和使用WordPress圖片上傳功能
WordPress 添加額外選項字段到常規(guī)設置頁面
?
來自為知筆記(Wiz)
?
總結
以上是生活随笔為你收集整理的WordPress中使主题支持小工具以及添加插件启用函数的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。