WordPress WP_Customize_Image_Control()无法使用icon(.ico)文件
我在主题定制器中添加了一个图片控件来更改收藏夹图标。问题是我无法上传.ico文件。它可以与其他文件格式(.jpg, .png)配合使用。现在, 我正在使用WP_Customize_Upload_Control()更改收藏夹图标。我想知道有什么方法可以使用WP_Customize_Image_Control()上传.ico文件。在Codex中没有此类的文档。
提前致谢
#1我的解决方案是扩展WP_Customize_Image_Control类以能够接受.ico文件。为此, 请使用以下代码创建一个名为”
customize-favicon.php”
的新文件:
<
?php
class SO_Customize_Favicon_Control extends WP_Customize_Image_Control {
public function __construct( $manager, $id, $args ) {$this->
extensions[] = 'ico';
return parent::__construct( $manager, $id, $args );
}
}
?>
然后, 在你的custom_register回调函数中, 在新文件中添加一个include, 并将标准图像控件类替换为刚创建的类:
include 'customize-favicon.php';
$wp_manager->
add_control( new Custom_Customize_Favicon_Control( $wp_manager, 'favicon_image', array(
'label'=>
'Favicon Image (16x16 px or 32x32 px)', 'section' =>
'some_global_section', 'settings'=>
'favicon_image', 'priority' =>
1, ) ) );
#2我必须使用几个过滤器来允许WP Media Library中的.ico MIME类型。然后, 我为Favicon上传创建了一个自定义设置处理程序和控件。
【WordPress WP_Customize_Image_Control()无法使用icon(.ico)文件】在媒体库中允许.ico
add_filter( 'getimagesize_mimes_to_exts', array($this, 'add_ico_mime'), 99999 );
add_filter( 'upload_mimes', array($this, 'add_ico_ext'), 99999 );
/*Add Ico Mime Type to Allowed Extension*/
public function add_ico_mime( $mime ) {
$mime[ 'image/vnd.microsoft.icon' ] = "ico";
$mime[ 'image/x-icon' ]= "ico";
$mime[ 'image/ico' ]= "ico";
return $mime;
}/*Add Ico File Extension to Allowed Mimes*/
public function add_ico_ext( $site_mimes ) {
if (isset($site_mimes['ico']) === false) $site_mimes['ico'] = 'image/vnd.microsoft.icon';
return $site_mimes;
}
设置处理程序
/*Setup WP Customizer Options*/
public $transport = 'refresh';
// 'PostMessage';
public $option_type = 'theme_mod';
/*Handle Uploader Settings*/
public function add_favicon_control($setting_id, $section_id, $label = '', $priority = 0) {
global $wp_customize, $theme_namespace;
$setting_args = array(
'type'=>
$this->
option_type, 'capability'=>
'edit_theme_options', 'transport'=>
$this->
transport, 'sanitize_callback' =>
'esc_url_raw', );
$wp_customize->
add_setting( $setting_id, $setting_args );
$favicon_control = new Favicon_Control( $wp_customize, $setting_id, array(
'label'=>
__( $label, $theme_namespace ), 'section'=>
"ds_theme_{$section_id}_section_id", 'priority'=>
$priority, 'settings'=>
$setting_id, 'context'=>
'favicon'
) );
$wp_customize->
add_control( $favicon_control );
return $favicon_control;
}
声明自定义图标控件
/*Custom Favicon Control Class*/
class Favicon_Control
extends WP_Customize_Image_Control {
public $setting_id;
public function __construct( $manager, $id, $args = array() ) {
$this->
extensions[] = 'ico';
$this->
setting_id = $id;
parent::__construct( $manager, $id, $args );
}public function tab_uploaded() {
$my_context_uploads = get_posts( array(
'post_type'=>
'attachment', 'meta_key'=>
'_wp_attachment_context', 'meta_value' =>
$this->
context, 'orderby'=>
'post_date', 'nopaging'=>
true, ) );
?>
<
div class="uploaded-target">
<
/div>
<
?php
if ( empty( $my_context_uploads ) )
return;
?>
<
div class="uploaded-favicons" data-controller="<
?php esc_attr_e( $this->
setting_id );
?>
">
<
?php
foreach ( (array) $my_context_uploads as $my_context_upload ) {
$this->
print_tab_image( esc_url_raw( $my_context_upload->
guid ) );
}
?>
<
/div>
<
?php
}
}
最后, 初始化处理程序
/*Initialize Favicon Handler*/
$this->
add_favicon_control($setting_id, $section_id, $label, 80);
推荐阅读
- WordPress wp_list_categories()格式
- WordPress(Visual Composer卡在加载中)
- WordPress(仅将其用作新闻CMS())
- WordPress使用index.php而不是single.php来显示帖子
- WordPress的url_to_postid不适用于非默认的永久链接
- WordPress和Twitter Bootstrap图像出现拉伸或最大宽度问题(100%无法正常工作)
- JAVA SE—— 异常
- #yyds干货盘点#SpringCloud技术专题「Gateway网关系列」微服务网关服务的Gateway全流程开发实践指南(2.2.X)
- featuretools完全指南-可自动构造机器学习特征的Python库