WordPress WP_Customize_Image_Control()不使用图标(.ico)文件

时间:2013-08-01 06:26:49

标签: php wordpress wordpress-plugin wordpress-theming

我在主题定制器中添加了一个图像控件来更改图标。问题是我无法上传.ico文件。它适用于其他文件格式(.jpg,.png)。现在我使用WP_Customize_Upload_Control()来更改fav图标。我想知道有没有办法使用WP_Customize_Image_Control()上传.ico文件。此课程Codex中没有可用的文档。

提前致谢

2 个答案:

答案 0 :(得分:0)

我的解决方案是扩展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 );
    }
}
?>

然后,在customize_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,
) ) );

答案 1 :(得分:0)

我不得不使用几个过滤器来允许WP媒体库中的.ico mime类型。然后我为Favicon上传创建了自定义设置处理程序和控件。

  1. 在媒体库中允许.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;
    }
    
  2. 设置处理程序

    /*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;
    }
    
  3. 声明自定义Favicon控件

    /*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
            }
        }
    
  4. 最后,初始化处理程序

    /*Initialize Favicon Handler*/
    $this->add_favicon_control($setting_id, $section_id, $label, 80);
    
相关问题