在自定义窗口小部件中加载本地WordPress图像上载程序

时间:2020-05-18 作者:zigojacko

有没有一种简单的方法可以将本机图像上传程序从Wordpress附带的“图像”小部件添加到自定义小部件?

这个WP_Widget_Media_Image

我本以为会有一种方法来调用它,而不必自己编写代码。

我试着搜索任何东西来给我一个想法,但这很荒谬,因为只会出现插件、插件和更多插件。

这正是我试图复制的基本内容:

Wordpress Image Widget Media Uploader

1 个回复
最合适的回答,由SO网友:user3135691 整理而成

你所要做的就是加载几个脚本,准确地说是四个。

首先注册您的widget类加载媒体库,传递一些变量以正确翻译内容(有关详细信息,请参阅WordPress的翻译标准)在顶部添加您自己的脚本以触发模式窗口并处理文件上载,首先将您的小部件注册为类,然后放入脚本:

<?php

    // We register our widget 
    function wpse_register_my_custom_widget(){
        register_widget(\'The_Custom_Image_Upload_Widget\');
    }
    add_action(\'widgets_init\', \'wpse_register_my_custom_widget\');

    // in your plugin main file or functions.php when in a theme ..
    function wpse_load_media_library(){
        // Set $pagenow to global to check, where to load the scripts..
        global $pagenow;

        // make a variable for the path in case you don\'t have it
        $dir = plugins_url(\'/my-plugin-folder\');

       // register your own script first
       wp_register_script(
           // WP internal name (so called "handle") this is important as you need to relate to this name later on
           \'my-js-file-with-code\',
            $dir . \'/components/js/the-custom-image-upload-actions.js\',
            array(\'jquery\'),
            false,
            true
        );

        // Now let\'s kick in our scripts
        if($pagenow === \'widgets.php\'){

            // Load/enqueue the media library, this is what you didn\'t find and struggled with..
            wp_enqueue_media();

            // load your own script with the trigger of the modal
            wp_enqueue_script(\'my-js-file-with-code\');

            // Then pass some variables to wordpress in order to translate things in Javascript
            wp_localize_script(
                // WP Handle/ID of the script to translate
                \'my-js-file-with-code\',
                // random varname (array) which holds the strings
                \'scripttext\',
                    array(          // array with strings
                        \'title\' => __(\'Logo upload\', \'textdomain\'),
                        \'button\' => __(\'use this logo\', \'textdomain\'),
                        \'multiple\' => __(\'use these images\', \'textdomain\')
                    )
            );

     }
     add_action(\'admin_enqueque_scripts\', \'wpse_load_media_library\');
?>

// My JS file with code
<script>
   // image Upload
   $(\'#trigger_to_open_the_modal\').click(function(e){
       var single_image_frame;
       e.preventDefault();
       if ( single_image_frame ) {
            single_image_frame.open();
            return;
        }
        single_image_frame = wp.media.frames.single_image_frame = wp.media({
            title: scripttext.title,
            button: { text:  scripttext.button },
            library: { type: \'image\' }
        });
        single_image_frame.on(\'select\', function(){
            var media_attachment = single_image_frame.state().get(\'selection\').first().toJSON();
            // Adjust this to your needs, pops in the url to the img tag
            $(\'#custom-upload-logo\').attr(\'src\', media_attachment.url);
            $(\'#logo_url\').val(media_attachment.url);
        });
        single_image_frame.open();
    });
   </script>
因为我不知道您的标记,所以我没有编写任何小部件的HTML示例。我写这篇文章花了大约一个小时,希望你不要介意。

相关推荐

My widgets do not save

每次我保存我的小部件并离开页面时,我的小部件都会消失。侧边栏已完全清空,不会保存任何更改。控制台或PHP日志中没有任何错误。如果我将小部件直接复制并保存在数据库中widgets_text, 它们将被显示,但我仍然无法在侧边栏中添加或删除任何内容。这只发生在我的右侧边栏上,左侧边栏工作正常,但它们都以相同的方式注册。这是我注册侧边栏的方式:function my_widgets_init() { register_sidebar( array ( \'name\'