0

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

提前致谢

4

3 回答 3

1

我必须使用几个过滤器才能在 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. 声明自定义网站图标控件

    /*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);
    
于 2014-08-28T07:56:03.190 回答
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 '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,
) ) );
于 2013-12-25T10:13:27.903 回答
0

这对我有用。

add_filter('upload_mimes', 'myfile_upload');

function myfile_upload( $mimes ) {
  //Only allow SVG upload by admins
  if ( !current_user_can( 'administrator' ) ) {
    return $mimes;
  }
    $mimes['ico'] = 'image/vnd.microsoft.icon';
    $mimes['ico'] = 'image/x-icon';
    $mimes['ico'] = 'mage/ico';

  
  return $mimes;
}
于 2021-10-25T19:25:35.213 回答