【问题标题】:Wordpress: Upload Image in Admin Options PageWordpress:在管理员选项页面中上传图片
【发布时间】:2014-05-17 10:56:40
【问题描述】:

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

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

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

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

【问题讨论】:

  • 它将上传媒体目录创建为空,但不呈现默认媒体弹出部分

标签: php wordpress image forms upload


【解决方案1】:

将此代码添加到您的全局自定义选项函数中。

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

【讨论】:

【解决方案2】:

你可以使用wordpress的内置功能

<?php wp_handle_upload( $file, $overrides, $time ); ?>

这会自动将文件移动到上传文件夹

或者

您可以编写自己的PHP 函数。

更多细节可以在这里找到 -> Wordpress File Upload

【讨论】:

    猜你喜欢
    • 2011-01-24
    • 1970-01-01
    • 1970-01-01
    • 2013-09-07
    • 1970-01-01
    • 2017-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多