Загружать скрипты в админке wordpress

Я не понимаю, как загружать скрипты в 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, поэтому, когда я нажимаю кнопку метабокса, чтобы загрузить скрипт, он не загружает толстый ящик.

У кого-нибудь есть идея?

Спасибо за помощь.


person Community    schedule 27.05.2014    source источник


Ответы (1)


  • Два admin_enqueue_scripts не нужны.

  • Скрипты media-upload и thickbox добавляются как зависимости от вашего my-upload, поэтому нет необходимости ставить их в очередь.

  • Это wp_enqueue_script('my-upload'), а не wp_enqueue_media.

  • Используйте следующий стиль jQuery в WordPress:

    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');
}
person brasofilo    schedule 08.06.2014