Make WordPress Core

Opened 4 years ago

Closed 7 months ago

#54994 closed defect (bug) (invalid)

Action Hook admin_enqueue_scripts and wp_enqueue_scripts

Reported by: smusman98's profile smusman98 Owned by:
Milestone: Priority: normal
Severity: normal Version: 5.9
Component: Plugins Keywords: reporter-feedback close
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 (3)

#1 @SergeyBiryukov
4 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
4 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.

#3 @karmatosed
7 months ago

  • Keywords close added
  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed

Thank you for reporting this. As some time has passed since more information was requested, and this issue appears to be related to implementation, I recommend moving to close this ticket for now.

I am marking it as invalid as it seems to either be something to do with the way being used or other circumstances unable to be determined, overworks for me.

We can always reopen should more details emerge that need to move this to a bug or different state.

Note: See TracTickets for help on using tickets.