【问题标题】:Load scripts to admin wordpress将脚本加载到管理员 wordpress
【发布时间】:2014-05-27 17:07:30
【问题描述】:

我对如何将脚本加载到 wp admin 感到困惑。我创建了一个元框上传图像,并将 wordpress 厚框上传器附加到它。 要显示 wordpress 上传器厚框,我使用 jquery:

$(function() {

var formfield = null;

$('#upload_image_button').click(function() {

    $('html').addClass('Image');

    formfield = $(this).prev('input').attr('name');  
    formfield_id = $(this).prev('input').attr('id'); 

    tb_show( '', 'media-upload.php?type=image&TB_iframe=true' );
    return false;
});

// user inserts file into post.
// only run custom if user started process using the above process
// window.send_to_editor(html) is how wp normally handles the received data

window.original_send_to_editor = window.send_to_editor;
window.send_to_editor = function( html ) {
    var fileurl;

    if(formfield != null) {
        fileurl = $( 'img', html).attr('src');

        $( "#" + formfield_id ).val(fileurl);

        tb_remove();

        $('html').removeClass('Image');
        formfield = null;
    } else {
        window.original_send_to_editor(html);
    }
};
});

我的元盒

function upload_image(){
 echo '<input id="upload_image" type="text" size="36" name="_logo_agence" value="'.$logo_agence.'" />';
        echo '<input id="upload_image_button" type="button" value=" Logo" />';
        echo '<div>'.$image_logo.'</div>';

}

并附上javascript:

// Enqueue script
    function my_admin_scripts() {    
        wp_enqueue_media('media-upload');
        wp_enqueue_media('thickbox');
        wp_register_script('my-upload', get_stylesheet_directory_uri().'/js/metabox.js', array('jquery','media-upload','thickbox'));
        wp_enqueue_media('my-upload');
    }

    // Attacher le thickbox
    function my_admin_styles() {
        wp_enqueue_style('thickbox');
    }

// better use get_current_screen(); or the global $current_screen

    add_action('admin_enqueue_scripts', 'my_admin_scripts');
    add_action('admin_enqueue_scripts', 'my_admin_styles');

问题是这个脚本没有附加到 wordpress 管理员,当我使用元素检查器时,任何地方都没有 metabox.js,所以当我点击我的 metabox 按钮加载脚本时,它没有加载thickbox。

有人有想法吗?

感谢您的帮助。

【问题讨论】:

    标签: javascript php wordpress meta-boxes


    【解决方案1】:
    • 不需要两个admin_enqueue_scripts

    • 脚本media-uploadthickbox 被添加为my-upload 的依赖项,因此无需将它们排入队列。

    • 这是wp_enqueue_script('my-upload'),而不是wp_enqueue_media

    • 在 WordPress 中使用以下 jQuery 样式:

      jQuery(document).ready(function($){ /*my-code*/ });
      

    工作队列:

    add_action('admin_enqueue_scripts', 'my_admin_scripts');
    
    function my_admin_scripts($hook) 
    {
        # Not our screen, bail out
        if( 'post.php' !== $hook )
            return;
    
        # Not our post type, bail out
        global $typenow;
        if( 'post' !== $typenow )
            return;
    
        wp_register_script( 
            'my-upload', 
            get_stylesheet_directory_uri() . '/js/script.js', 
            array( 'jquery', 'media-upload', 'thickbox' )
        );
        wp_enqueue_script('my-upload');
        wp_enqueue_style('thickbox');
    }
    

    【讨论】:

      猜你喜欢
      • 2019-05-22
      • 2019-04-01
      • 2013-09-17
      • 1970-01-01
      • 1970-01-01
      • 2021-12-23
      • 1970-01-01
      • 2019-12-20
      • 1970-01-01
      相关资源
      最近更新 更多