我正在编写一个自定义插件,我想添加“添加媒体”按钮。
我只需要上传媒体,而不是从上传的文件中检索任何内容/数据。
如何添加此按钮?
谢谢
我正在编写一个自定义插件,我想添加“添加媒体”按钮。
我只需要上传媒体,而不是从上传的文件中检索任何内容/数据。
如何添加此按钮?
谢谢
如果要将“添加媒体”按钮添加到admin 面板:
您需要使用wp\\u enqueue\\u media();
add_action ( \'admin_enqueue_scripts\', function () {
if (is_admin ())
wp_enqueue_media ();
} );
然后使用此js:jQuery(document).ready(function() {
var $ = jQuery;
if ($(\'.set_custom_images\').length > 0) {
if ( typeof wp !== \'undefined\' && wp.media && wp.media.editor) {
$(\'.set_custom_images\').on(\'click\', function(e) {
e.preventDefault();
var button = $(this);
var id = button.prev();
wp.media.editor.send.attachment = function(props, attachment) {
id.val(attachment.id);
};
wp.media.editor.open(button);
return false;
});
}
}
});
使用此html:<p>
<input type="number" value="" class="regular-text process_custom_images" id="process_custom_images" name="" max="" min="1" step="1">
<button class="set_custom_images button">Set Image ID</button>
</p>
Show thumbnail preview instead of number
作为一个调整,我做了这个。。。将数字输入更改为隐藏。
已添加:
$imgid =(isset( $instance[ \'imgid\' ] )) ? $instance[ \'imgid\' ] : "";
$img = wp_get_attachment_image_src($imgid, \'thumbnail\');
然后。。。隐藏字段上方。if($img != "") {
?>
<img src="<?= $img[0]; ?>" width="80px" /><br />
<?php
}
这将使缩略图在用户端可见,而不是数字:)我开发了两个插件,其中一个功能相同(相同的名称,相同的功能)。当试图激活两个插件时,Wordpress会抛出一个错误,因为它不允许我以相同的名称定义函数两次。有没有一种方法可以使这个函数只对插件私有,而不使用面向对象编程,也不简单地重命名函数?我不想使用OOP,因为我首先要学习它。此外,我不想重命名该函数,因为我可能也想在其他插件中使用它,而重命名感觉不太合适。