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

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – WordPress:在管理员选项页面上传图片脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在开发我的第一个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

脚本宝典总结

以上是脚本宝典为你收集整理的php – WordPress:在管理员选项页面上传图片全部内容,希望文章能够帮你解决php – WordPress:在管理员选项页面上传图片所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。