Question

I'm trying to include two scripts, wp_media and my personal when I stay in my plugin options page, but Wordpress don't include it. Someone help?


/**
 * Activate the plugin.
 */
function waterstampPrefix_activate() { 
    // Trigger our function that registers the custom post type plugin.
    waterstampPrefix_setup_object_menu(); 
    // Clear the permalinks after the post type has been registered.
    flush_rewrite_rules(); 
}
register_activation_hook( __FILE__, 'waterstampPrefix_activate' );



/**
 * Deactivation plugin.
 */
function waterstampPrefix_deactivate() {
    // Unregister the post type, so the rules are no longer in memory.
    remove_menu_page( 'Waterstamp options' );
    // Clear the permalinks to remove our post type's rules from the database.
    flush_rewrite_rules();
}
register_deactivation_hook( __FILE__, 'waterstampPrefix_deactivate' );



 /* Register the "waterstamp" menu object
 */
add_action( 'init', 'waterstampPrefix_setup_object_menu' );

function waterstampPrefix_setup_object_menu() {
        /** Step 2 (from text above). */
    add_action( 'admin_menu', 'waterstamp_menu' );



    /** Step 1. */
    function waterstamp_menu() {
        add_options_page( 'Waterstamp options', 'Waterstamp', 'manage_options', 'waterstamp-options', 'waterstamp_options' );
    }



    /** Step 3. */
    function waterstamp_options() {
        if ( !is_admin() )  {
            wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
        }
        echo '<div class="wrap">';
        echo '<h1>Waterstamp</h1>';
        echo '<p>Select the photos you would put the watermark</p>';

        //wp_die(plugin_dir_url( __FILE__ ) . 'js/app.js');

        function load_wp_media(){
            wp_enqueue_media();
            wp_enqueue_script( 'waterStampScript', plugin_dir_url( __FILE__ ) . 'js/app.js', array('jquery'));
        }

        add_action( 'admin_enqueue_scripts', 'load_wp_media' );

        if( wp_script_is( 'waterStampScript', 'enqueued' ) ){
            wp_die('incluso!!!');
        }        

        $image_id = get_option( 'myprefix_image_id' );

        if( intval( $image_id ) > 0 ) {
            // Change with the image size you want to use
            $image = wp_get_attachment_image( $image_id, 'medium', false, array( 'id' => 'myprefix-preview-image' ) );
        } else {
            // Some default image
            $image = '<img id="myprefix-preview-image" src="http://beepeers.com/assets/images/commerces/default-image.jpg">';
        }

        echo $image;

        echo '<input type="hidden" name="myprefix_image_id" id="myprefix_image_id" value="'; echo esc_attr( $image_id ); echo '" class="regular-text">';
        echo '<input type="button" class="button-primary" value="Select a image" id="myprefix_media_manager">';
    }


}

Was it helpful?

Solution

Wordpress fires admin_enqueue_scripts hook earlier, than add_options_page, where you put your admin_enqueue_scripts.

Solution is simple, you need to put your admin_enqueue_scripts hook outside of the add_options_page function callback.

function load_wp_media(){
    wp_enqueue_media();
    wp_enqueue_script( 'waterStampScript', plugin_dir_url( __FILE__ ) . 'js/app.js', array('jquery'));
}

add_action( 'admin_enqueue_scripts', 'load_wp_media' );
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top