媒体库中的 Wordpress select 图片
Wordpress select image from Media Library
我为 wordpress 和管理面板创建了一个主题,我需要通过管理站点从媒体库中 select 图片(徽标)。我使用此代码但不起作用:
<?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>
但我不需要文本编辑器!
请帮我。
谢谢
首先,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
您应该使用 wp.media 来使用 WordPress 媒体管理器对话框。
首先,您需要对脚本进行排队:
// 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>
我为 wordpress 和管理面板创建了一个主题,我需要通过管理站点从媒体库中 select 图片(徽标)。我使用此代码但不起作用:
<?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>
但我不需要文本编辑器! 请帮我。 谢谢
首先,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
您应该使用 wp.media 来使用 WordPress 媒体管理器对话框。 首先,您需要对脚本进行排队:
// 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>