Wordpress:在管理员选项页面上传图片 [英] Wordpress: Upload Image in Admin Options Page

查看:203
本文介绍了Wordpress:在管理员选项页面上传图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发我的第一个wordpress插件。它只需要允许用户更改自定义模板中的徽标并更改自定义模板中的颜色方案。

I am developing my first wordpress plugin. It only needs to allow a user to change a logo in a custom template and change a color scheme in the custom template.

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

I have made an admin options page and now want to add a field to allow a user to upload an image. How can I upload an image to the wp-content/uploads folder. So far, I have this within a table:

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

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

Is this the right approach? If so, how do I direct the file to the right folder? Doesn't wordpress have it's own way of handling file uploads?

推荐答案

将此代码添加到全局自定义选项功能。

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>

更多信息

主题和插件中的媒体上传者

这篇关于Wordpress:在管理员选项页面上传图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆