最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

metabox - wp is not defined error using wp.media to create a custom image uploader

matteradmin9PV0评论

I'm trying to create a custom media uploader in a custom meta box on a custom post type. That's a lot of custom. I have a javascript file enqueued which runs the wp.media class when the Add Image button is clicked. When I tested the button I receive the following error: "Uncaught ReferenceError: wp is not defined" I found several resources that solved this problem by adding wp_enqueue_media(); but when I attempt to do so it doesn't fix the problem. Please help?

Meta box creation/enqueued script:

function cns_add_custom_metabox(){
    add_meta_box( 'fet-media-fields', 'Listing Media', 'cns_display_media' );
}
add_action( 'add_meta_boxes', 'cns_add_custom_metabox' ); 

function cns_display_media( $post ){ 

    ?>

    <div id="metabox-wrapper">
        <img id="image-tag">
        <input type="hidden" id="img-hidden-field" name="custom_image_data">
        <input type="button" id="image-upload-button" class="button" value="Add Image">
        <input type="button" id="image-delete-button" class="button" value="Remove Image">

    </div>


    <?php
}

function register_admin_script() {
    wp_enqueue_script( 'cns-image-upload.js', get_stylesheet_directory_uri() . '/js/cns-image-upload.js', array('jquery') );
}

add_action( 'admin_enqueue_scripts', 'register_admin_script' );

Javascript file - cns-image-upload.js:

var addButton = document.getElementById( 'image-upload-button' );
var deleteButton = document.getElementById( 'image-delete-button' );
var img = document.getElementById( 'image-tag' );
var hidden = document.getElementById( 'img-hidden-field' );
var customUploader = wp.media({
    title: 'Select and Image',
    button:{
        text: 'User this Image'
    },
    multiple: false
});

addButton.addEventListener( 'click', function(){
    if( customUploader ){
        customUploader.open();
    }
} );

I'm trying to create a custom media uploader in a custom meta box on a custom post type. That's a lot of custom. I have a javascript file enqueued which runs the wp.media class when the Add Image button is clicked. When I tested the button I receive the following error: "Uncaught ReferenceError: wp is not defined" I found several resources that solved this problem by adding wp_enqueue_media(); but when I attempt to do so it doesn't fix the problem. Please help?

Meta box creation/enqueued script:

function cns_add_custom_metabox(){
    add_meta_box( 'fet-media-fields', 'Listing Media', 'cns_display_media' );
}
add_action( 'add_meta_boxes', 'cns_add_custom_metabox' ); 

function cns_display_media( $post ){ 

    ?>

    <div id="metabox-wrapper">
        <img id="image-tag">
        <input type="hidden" id="img-hidden-field" name="custom_image_data">
        <input type="button" id="image-upload-button" class="button" value="Add Image">
        <input type="button" id="image-delete-button" class="button" value="Remove Image">

    </div>


    <?php
}

function register_admin_script() {
    wp_enqueue_script( 'cns-image-upload.js', get_stylesheet_directory_uri() . '/js/cns-image-upload.js', array('jquery') );
}

add_action( 'admin_enqueue_scripts', 'register_admin_script' );

Javascript file - cns-image-upload.js:

var addButton = document.getElementById( 'image-upload-button' );
var deleteButton = document.getElementById( 'image-delete-button' );
var img = document.getElementById( 'image-tag' );
var hidden = document.getElementById( 'img-hidden-field' );
var customUploader = wp.media({
    title: 'Select and Image',
    button:{
        text: 'User this Image'
    },
    multiple: false
});

addButton.addEventListener( 'click', function(){
    if( customUploader ){
        customUploader.open();
    }
} );
Share Improve this question asked Nov 1, 2016 at 14:12 ChristyChristy 872 silver badges9 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 3

Figured it out! I didn't have the javascript code wrapped with the following:

jQuery(document).ready( function($){ });

Post a comment

comment list (0)

  1. No comments so far