码迷,mamicode.com
首页 > Web开发 > 详细

WordPress 后台上传自定义网站Logo

时间:2015-02-01 20:24:58      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:

需求:
众所周知一般网站的logo都是固定的所以我在做网站时也是使用的静态logo文件,但最近用wp给一个客户做的网站时,因为网站现在的logo可能会需要重新设计,所以客户提出了需要在后台可以自己修改网站logo,接收需求后就在网络上找如何解决,但找了一圈都没有找到想要的效果(都是如何修改wp的登录logo),还好找到两篇相关的文章,最后根据这两篇文章自己Codeing最终实现了功能代码:
1.在function中添加以下代码
  1. <?php
  2. /**在function中添加以下代码
  3. * WordPress 添加额外选项字段到常规设置页面
  4. * http://www.wpdaxue.com/add-field-to-general-settings-page.html
  5. */
  6. $new_general_setting = new new_general_setting();
  7. class new_general_setting {
  8. function new_general_setting( ) {
  9. add_filter( ‘admin_init‘ , array( &$this , ‘register_logo‘ ) );
  10. }
  11. function register_logo(){
  12. //需要‘js/uploader.js组件
  13. wp_enqueue_script( ‘fli-upload-js‘, $this->url . ‘js/uploader.js‘, array( ‘jquery‘, ‘media-upload‘, ‘thickbox‘ ) );
  14. wp_enqueue_style( ‘thickbox‘ );
  15. wp_enqueue_style( ‘fli-upload-css‘, $this->url . ‘css/uploader.css‘ );
  16. register_setting( ‘general‘, ‘logo‘, ‘esc_attr‘ );
  17. add_settings_field(‘logo‘, ‘<label for="logo">‘.__(‘网站Logo‘ ).‘</label>‘ , array(&$this, ‘logo_fields_html‘) , ‘general‘ );
  18. }
  19. function logo_fields_html() {
  20. $value = get_option( ‘logo‘, ‘‘ );
  21. 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="上传">‘;
  22. }
  23. }
  24. // 自定义后台CssJs
  25. add_action( ‘admin_enqueue_scripts‘, ‘myAdminScripts‘ );
  26. function myAdminScripts() {
  27. //主题下加载admin.js
  28. wp_register_script( ‘default‘, get_template_directory_uri() . ‘/admin.js‘, array(), ‘‘, ‘all‘ );
  29. wp_enqueue_script( ‘default‘ );
  30. wp_register_style( ‘default‘, get_template_directory_uri() . ‘/admin.css‘, array(), ‘‘, ‘all‘ );
  31. wp_enqueue_style( ‘default‘ );
  32. }
  33. ?>
2.在主题admin.js中添加代码
  1. options_general();
  2. //在常规选项页面添加自定义信息
  3. function options_general () {
  4. if(!Islocatl_pathname(‘options-general.php‘))return;
  5. //点击上传按钮或input元素时打开上传窗口
  6. jQuery(‘#general_logo,#logo‘).click(function() {
  7. //打开上传窗口需要js/uploader.js组件
  8. tb_show(‘‘, ‘media-upload.php?type=image&TB_iframe=true‘);
  9. return false;
  10. });
  11. //图片上传页面回传
  12. //html:为选择的图片元素
  13. window.send_to_editor = function(html) {
  14. imgurl = jQuery(html).attr(‘src‘);
  15. // 保存值并写入optuions表
  16. jQuery(‘#logo‘).val(imgurl);
  17. //删除图片上传窗口
  18. tb_remove();
  19. return false;
  20. } //end send_to_editor
  21. }
  22. //当前页面是否是指定的页面
  23. function Islocatl_pathname (pathname) {
  24. return location.pathname.indexOf(pathname)>=0;
  25. }//end 当前页面是否是指定的页面
效果图:
技术分享
技术分享
参考:




WordPress 后台上传自定义网站Logo

标签:

原文地址:http://www.cnblogs.com/huangtailang/p/4265999.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!