Make WordPress Core

Opened 3 years ago

Last modified 3 years ago

#54994 new defect (bug)

Action Hook admin_enqueue_scripts and wp_enqueue_scripts

Reported by: smusman98's profile smusman98 Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 5.9
Component: Plugins Keywords: reporter-feedback
Focuses: multisite Cc:

Description

Hey,
I'm a developer, I wanted to know if it's a bug, or am I doing it wrong way?
When using admin_enqueue_scripts (both methods static in the same class, I can not use it this way [ ( new self() ), 'admin_enqueue_scripts' ], It will throw errors).

<?php
public static function load_front_scripts()
{
    $_this = self::wp_enqueue_scripts();

    add_action( 'wp_enqueue_scripts', 'self::wp_enqueue_scripts' );
}
public static function wp_enqueue_scripts()
{

}

On the other side when using wp_enqueue_scripts same scenario, I'm strict to use it this way, can not use it that way ('self::wp_enqueue_scripts'), It will throw error,

<?php
public static function load_admin_scripts()
{
    add_action( 'admin_enqueue_scripts', [ ( new self() ), 'admin_enqueue_scripts' ] );
}


public static function admin_enqueue_scripts()
{
    wp_enqueue_script( 'cpbwc-admin', CPBWC_PLUGIN_URL . '/assets/js/admin.min.js', array( 'jquery' ), 1.0, true );
    wp_enqueue_style( 'cpbwc-admin', CPBWC_PLUGIN_URL . '/assets/css/admin.min.css', '', 1.0 );
    wp_enqueue_media();
}

Conclusion:
On both action hooks callbacks I'm strict to use it the way I'm using, Unable to call call-backs same way.

Change History (2)

#1 @SergeyBiryukov
3 years ago

  • Component changed from External Libraries to Plugins
  • Keywords reporter-feedback added

Hi there, welcome to WordPress Trac! Thanks for the ticket.

Moving this to the Plugins component, as it seems more related to Plugin API functions, e.g. add_action() and do_action(), than a specific external library. It's also possible that Script Loader would be the right component.

Could you share the error messages you get with these callbacks?

#2 @dd32
3 years ago

add_action( 'wp_enqueue_scripts', 'self::wp_enqueue_scripts' );

add_action() accepts callables, but they must be a global callable acceptable by call_user_func() which is run within WP_Hook, self:: would refer to WP_Hook in that instance, not your custom class. To use that static-call syntax, you'd need to pass MyClassName::myClassMethod or [ 'MyClassName', 'myClassMethod' ].

add_action( 'admin_enqueue_scripts', [ ( new self() ), 'admin_enqueue_scripts' ] );

That should work, as would $obj = new self(); add_action( ..., [ $obj, 'method' ] ); which is syntactically the same.

If you can provide the error messages, it should point closer to the error in calling it.

Note: See TracTickets for help on using tickets.