admin管理员组文章数量:1310455
I'd like to save one image in my custom settings page. i.e. this image should be the logo which should get print to the pdf files I generate. Thats why I want to store the image (the image link) in the wp_options table.
I want to use the default wordpress image attachment funktions to select / upload the image. So I want the usability like the user adding a thumbnail to his post.
Can somebody help me?
Thanks.
I'd like to save one image in my custom settings page. i.e. this image should be the logo which should get print to the pdf files I generate. Thats why I want to store the image (the image link) in the wp_options table.
I want to use the default wordpress image attachment funktions to select / upload the image. So I want the usability like the user adding a thumbnail to his post.
Can somebody help me?
Thanks.
Share Improve this question asked Dec 1, 2015 at 15:37 Carsten SchmittCarsten Schmitt 7711 bronze badges 1 |1 Answer
Reset to default 1After some time I found out how to solve my Problem.
In short, I have to use Javascript to open the WP-Media Uploader. And in my options I only save the attachment ID.
Here is my code:
HTML / PHP:
<?php
$options = get_option( 'mysettings' );
$image_id = esc_attr__( $options['image_id']);
?>
<div id="logo">
<?php
$hasImage = false;
$image = wp_get_attachment_image_src( $image_id, 'medium' );
$image_url = $image[0];
if (!is_null($image_id) && $image_id !== "" && $image_id > 0) {
$hasImage = true;
}
?>
<div>
<label for="image_url">Image</label>
<input type="hidden" name="mysettings[image_id]" id="option_image_id" class="regular-text">
<input id="upload_img-btn" type="button" name="upload-btn" class="button-secondary" value="Upload Image">
</div>
<div id="logo_container">
<?php if ($hasImage) { ?>
<img class="logo" src="<?php echo $image_url; ?>" />
<?php } ?>
</div>
<input id="delete_img-btn" type="button" name="delete-btn" class="button-secondary" value="Remove Image" <?php if (!$hasImage) echo 'style="display: none;"'; ?>>
</div>
and my Javascript Code:
jQuery(document).ready(function($) {
$("#delete_img-btn").on("click", function(e) {
e.preventDefault();
$('#logo_container').html("");
$("#delete_img-btn").hide();
});
$('#upload_img-btn').on("click", function(e) {
e.preventDefault();
var $el = jQuery( this );
var optionImageFrame = wp.media({
title: $el.data( 'choose' ),
button: {
text: $el.data( 'update' )
},
states: [
new wp.media.controller.Library({
title: $el.data( 'choose' ),
filterable: 'all',
// mutiple: true if you want to upload multiple files at once
multiple: false
})
]
});
optionImageFrame.on('select', function(e){
// This will return the selected image from the Media Uploader, the result is an object
var uploaded_image = optionImageFrame.state().get('selection').first();
// We convert uploaded_image to a JSON object to make accessing it easier
// Output to the console uploaded_image
var attachment = uploaded_image.toJSON();
var image_url = attachment.url;
var image_id = attachment.id;
$('#option_image_id').val(image_id);
$('#logo_container').append('<img class="logo" src="' + image_url + '" />');
$("#delete_img-btn").show();
});
optionImageFrame.open();
});
});
本文标签: optionsSave Image in wpoptionsTable
版权声明:本文标题:options - Save Image in wp_options-Table 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741825738a2399629.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
meta_box
and add a upload possibility. After selecting you can specify where to save the image in your database. Start here code.tutsplus/tutorials/… – Interactive Commented Dec 1, 2015 at 16:29