我为wordpress创建了一个主题,在管理面板上,我需要通过管理站点从媒体库中选择图像(Logo)。我使用此代码但不起作用:
<?php
add_action ('admin_enqueue_scripts', function() {
if(is_admin())
wp_enqueue_media();
});
?>
<form method="post">
<input type="text" class="process_custom_images" id="process_custom_images" name="selected_logo" value="" placeholder="http://">
<button class="set_custom_logo button" style="vertical-align: middle;">Select Logo</button>
</form>
<script>
//“Add Media” button
jQuery(document).ready(function() {
var $ = jQuery;
if ($('.set_custom_logo').length > 0) {
if ( typeof wp !== 'undefined' && wp.media && wp.media.editor) {
$(document).on('click', '.set_custom_logo', function(e) {
e.preventDefault();
var button = $(this);
var id = button.prev();
wp.media.editor.send.attachment = function(props, attachment) {
id.val(attachment.url);
};
wp.media.editor.open(button);
return false;
});
}
}
});
</script>
但是当我在form
标记内插入代码时,上面的代码运行良好。
<div><?php wp_editor("", 'postcontent', $settings = array('textarea_rows'=> '10')); ?></div>
但我不需要文字编辑器! 请帮我。 感谢&#39; S
答案 0 :(得分:1)
首先wp_enqueue_media();
应该在你的functions.php文件中,看起来应该是这样的:
function misha_include_script() {
if ( ! did_action( 'wp_enqueue_media' ) ) {
wp_enqueue_media();
}
}
add_action( 'admin_enqueue_scripts', 'misha_include_script' );
其次,管理区域中的jQuery调用最适合以这种方式开始:
jQuery(function( $ ){
// your jQuery is here
});
我建议尝试以上提示,如果它没有帮助,我希望本教程对您有用https://rudrastyh.com/wordpress/customizable-media-uploader.html
答案 1 :(得分:0)
您应该使用wp.media来使用WordPress Media Manager对话框。 首先,您需要排队这些脚本:
// As you are dealing with plugin settings,
// I assume you are in admin side
add_action( 'admin_enqueue_scripts', 'load_wp_media_files' );
function load_wp_media_files( $page ) {
// change to the $page where you want to enqueue the script
if( $page == 'options-general.php' ) {
// Enqueue WordPress media scripts
wp_enqueue_media();
// Enqueue custom script that will interact with wp.media
wp_enqueue_script( 'myprefix_script', plugins_url( '/js/myscript.js' , __FILE__ ), array('jquery'), '0.1' );
}
}
您的HTML可能是这样的(请注意,我的代码在插件设置中使用附件ID代替了您在答案中所做的图像url,我认为它会好得多。例如,使用ID可以获取不同的图像尺寸(当您需要它们时))
$image_id = get_option( 'myprefix_image_id' );
if( intval( $image_id ) > 0 ) {
// Change with the image size you want to use
$image = wp_get_attachment_image( $image_id, 'medium', false, array( 'id' => 'myprefix-preview-image' ) );
} else {
// Some default image
$image = '<img id="myprefix-preview-image" src="https://some.default.image.jpg" />';
}
echo $image; ?>
<input type="hidden" name="myprefix_image_id" id="myprefix_image_id" value="<?php echo esc_attr( $image_id ); ?>" class="regular-text" />
<input type='button' class="button-primary" value="<?php esc_attr_e( 'Select a image', 'mytextdomain' ); ?>" id="myprefix_media_manager"/>
myscript.js
jQuery(document).ready( function($) {
jQuery('input#myprefix_media_manager').click(function(e) {
e.preventDefault();
var image_frame;
if(image_frame){
image_frame.open();
}
// Define image_frame as wp.media object
image_frame = wp.media({
title: 'Select Media',
multiple : false,
library : {
type : 'image',
}
});
image_frame.on('close',function() {
// On close, get selections and save to the hidden input
// plus other AJAX stuff to refresh the image preview
var selection = image_frame.state().get('selection');
var gallery_ids = new Array();
var my_index = 0;
selection.each(function(attachment) {
gallery_ids[my_index] = attachment['id'];
my_index++;
});
var ids = gallery_ids.join(",");
jQuery('input#myprefix_image_id').val(ids);
Refresh_Image(ids);
});
image_frame.on('open',function() {
// On open, get the id from the hidden input
// and select the appropiate images in the media manager
var selection = image_frame.state().get('selection');
var ids = jQuery('input#myprefix_image_id').val().split(',');
ids.forEach(function(id) {
var attachment = wp.media.attachment(id);
attachment.fetch();
selection.add( attachment ? [ attachment ] : [] );
});
});
image_frame.open();
});
});
// Ajax request to refresh the image preview
function Refresh_Image(the_id){
var data = {
action: 'myprefix_get_image',
id: the_id
};
jQuery.get(ajaxurl, data, function(response) {
if(response.success === true) {
jQuery('#myprefix-preview-image').replaceWith( response.data.image );
}
});
}
And the Ajax action to refresh the image preview:
// Ajax action to refresh the user image
add_action( 'wp_ajax_myprefix_get_image', 'myprefix_get_image' );
function myprefix_get_image() {
if(isset($_GET['id']) ){
$image = wp_get_attachment_image( filter_input( INPUT_GET, 'id', FILTER_VALIDATE_INT ), 'medium', false, array( 'id' => 'myprefix-preview-image' ) );
$data = array(
'image' => $image,
);
wp_send_json_success( $data );
} else {
wp_send_json_error();
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>