【问题标题】:Wordpress Custom Image Upload FieldWordpress 自定义图片上传字段
【发布时间】:2012-01-18 12:26:56
【问题描述】:

我目前正在开发一个需要自定义内容添加/编辑模块的 WordPress 网站。例如,我正在寻找 wordpress 中的图像上传选项。在我的表单中有一个名为“选择图像”的输入字段,当用户单击输入字段时,我希望弹出 wordpress 媒体库并能够从库中选择图像。并且一旦用户选择了图像,图像的完整 url 将被填充到输入字段中。我确信这是可以做到的,因为我曾经找到过代码,但我忘记了在哪里找到的。请大家帮忙

提前致谢..

【问题讨论】:

标签: php image wordpress upload


【解决方案1】:

下面是执行此操作的代码:

jQuery(document).ready(function() {

    var orig_send_to_editor = window.send_to_editor;

    jQuery('#upload_image_button').click(function() {
        formfield = jQuery(this).prev('input');
        tb_show('', 'media-upload.php?type=image&TB_iframe=true');

        window.send_to_editor = function(html) {
        var regex = /src="(.+?)"/;
        var rslt =html.match(regex);
        var imgurl = rslt[1];
        formfield.val(imgurl);
        tb_remove();
        jQuery('#show_'+formfield.attr('name')).html('<img src="'+imgurl+'" width="" />')

        window.send_to_editor = orig_send_to_editor;
    }

    return false;
});
});

这是与之配套的 HTML:

<input type="hidden" name="upload_image" />
<?php $post_img = get_post_meta($post->ID,'item_image',true); ?>
<input id="upload_image_button" type="button" value="<?php echo (empty($post_img)?'Upload Image':'Change Image') ?>" />
<div id="show_upload_image"><?php echo (!empty($post_img)?"<img src='$post_img' width='100' />":'')?></div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-26
    • 2018-08-12
    • 1970-01-01
    • 1970-01-01
    • 2016-08-25
    • 1970-01-01
    • 2015-02-10
    相关资源
    最近更新 更多