无法在我的插件页面中加载脚本

时间:2021-04-30 作者:p.perfetto993

当我停留在插件选项页面时,我试图包括两个脚本,wp\\u media和my personal,但Wordpress没有包括它。有人帮忙吗?


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


}

1 个回复
最合适的回答,由SO网友:anton 整理而成

Wordpress火灾admin_enqueue_scripts 挂起时间早于add_options_page, 你把你的admin_enqueue_scripts.

解决方案很简单,您需要admin_enqueue_scripts 钩在add_options_page 函数回调。

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

相关推荐

Why won't my scripts load?

在函数中加载脚本时遇到问题。php我以前做过这件事,所以我肯定我忽略了一些东西,但就我所知,这是正确的,我尝试过从其他答案中复制和粘贴。我没有收到任何控制台错误。我的任何脚本都没有加载到源代码中。感谢您的帮助。functions.php/*SCRIPTS*/ add_action( \'wp_enqueue_scripts\', \'theme_js\' ); function theme_js(){ wp_register_script( \