加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – WordPress:在管理员选项页面上传图片

发布时间:2020-12-13 22:40:53 所属栏目:PHP教程 来源:网络整理
导读:我正在开发我的第一个wordpress插件.它只需要允许用户更改自定义模板中的徽标并更改自定义模板中的颜色方案. 我已经创建了一个管理选项页面,现在想要添加一个字段以允许用户上传图像.如何将图像上传到wp-content / uploads文件夹.到目前为止,我在一张表中有
我正在开发我的第一个wordpress插件.它只需要允许用户更改自定义模板中的徽标并更改自定义模板中的颜色方案.

我已经创建了一个管理选项页面,现在想要添加一个字段以允许用户上传图像.如何将图像上传到wp-content / uploads文件夹.到目前为止,我在一张表中有这个:

<td><input name="logo_image" type="file" id="logo_image" value="" /></td>

这是正确的方法吗?如果是这样,我如何将文件定向到正确的文件夹? wordpress是否有自己处理文件上传的方式?

将此代码添加到全局自定义选项功能中.
if(function_exists( 'wp_enqueue_media' )){
    wp_enqueue_media();
}else{
    wp_enqueue_style('thickbox');
    wp_enqueue_script('media-upload');
    wp_enqueue_script('thickbox');
}

<p><strong>Header Logo Image URL:</strong><br />
                <img class="header_logo" src="<?php echo get_option('header_logo'); ?>" height="100" width="100"/>
                <input class="header_logo_url" type="text" name="header_logo" size="60" value="<?php echo get_option('header_logo'); ?>">
                <a href="#" class="header_logo_upload">Upload</a>

</p>    


<script>
    jQuery(document).ready(function($) {
        $('.header_logo_upload').click(function(e) {
            e.preventDefault();

            var custom_uploader = wp.media({
                title: 'Custom Image',button: {
                    text: 'Upload Image'
                },multiple: false  // Set this to true to allow multiple files to be selected
            })
            .on('select',function() {
                var attachment = custom_uploader.state().get('selection').first().toJSON();
                $('.header_logo').attr('src',attachment.url);
                $('.header_logo_url').val(attachment.url);

            })
            .open();
        });
    });
</script>

More info

要么

Media uploader in theme and plugin

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读