0

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">';
    }


}

5
  • It's just my guess, but put your load_wp_media function with add_action outside waterstamp_options function. If you need to load these scripts only on your own page, you need to check it inside load_wp_media
    – anton
    Apr 30 at 16:47
  • It works outside but I don't understand qhy. Thanks May 3 at 12:27
  • My english is not so perfect unfortunately. I think wordpress triggers admin_enqueue_scripts action first, then add_options_page. But you put admin_enqueue_scripts action inside add_options_page action, so when add_options_page action triggers, it's to late for admin_enqueue_scripts.
    – anton
    May 3 at 14:42
  • So much thanks! May 3 at 15:15
  • You're welcome, I will post it as an answer if it helped you)
    – anton
    May 3 at 15:23
0

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' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.