最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - wp_loaded hook block script enquequing

programmeradmin5浏览0评论

I have a cutsom maintenenace screen generated by a plugin for non admin users. I need to setup a countdown timer on it if an option on the related settings page is enabled. I've implemented the Settings API correctly I'm able to save and retrive the checkbox selected or not option value and other stuff. I need some help to enqueue the front-end style, I need to pass to the javascript file some data outputted by the back-end settings page, I want to avoid inline scripting. I've tried using the normal wp way to load scripts wp_enqueue_script, but it will not work, I'm not sure why? I'm using the following code to show the maintenance page. Is possible that the wp_loaded hook will block the styles and scripts enquequing?

public function maintenance_mode()
  {
    global $pagenow;
    if( $pagenow !== 'wp-login.php' && !current_user_can('manage_options') && !is_admin() ){
      header( $_SERVER['SERVER_PROTOCOL'] . '503 Service Temporarily Unavailable', true, 503 );
      header( 'Content-Type: text/html; charset=utf-8' );
      if( file_exists( plugin_dir_path( __FILE__ ) . 'assets/maintenance.php' ) ) {
              require_once plugin_dir_path( __FILE__ ) . 'assets/maintenance.php';
          }
      die();
    }
  }
// the add action is in a separate method
add_action( 'wp_loaded', array($this, 'maintenance_mode') );

I have a cutsom maintenenace screen generated by a plugin for non admin users. I need to setup a countdown timer on it if an option on the related settings page is enabled. I've implemented the Settings API correctly I'm able to save and retrive the checkbox selected or not option value and other stuff. I need some help to enqueue the front-end style, I need to pass to the javascript file some data outputted by the back-end settings page, I want to avoid inline scripting. I've tried using the normal wp way to load scripts wp_enqueue_script, but it will not work, I'm not sure why? I'm using the following code to show the maintenance page. Is possible that the wp_loaded hook will block the styles and scripts enquequing?

public function maintenance_mode()
  {
    global $pagenow;
    if( $pagenow !== 'wp-login.php' && !current_user_can('manage_options') && !is_admin() ){
      header( $_SERVER['SERVER_PROTOCOL'] . '503 Service Temporarily Unavailable', true, 503 );
      header( 'Content-Type: text/html; charset=utf-8' );
      if( file_exists( plugin_dir_path( __FILE__ ) . 'assets/maintenance.php' ) ) {
              require_once plugin_dir_path( __FILE__ ) . 'assets/maintenance.php';
          }
      die();
    }
  }
// the add action is in a separate method
add_action( 'wp_loaded', array($this, 'maintenance_mode') );
Share Improve this question asked Jan 22, 2020 at 17:35 sialfasialfa 32910 silver badges29 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

You can't enqueue because wp_loaded is too early.

To enqueue scripts and styles, you need to do it on the wp_enqueue_scripts type hooks, but because your exiting on wp_loaded, none of those hooks have fired yet.

Additionally, you need to have the necessary function calls in the template for it to enqueue them into, such as wp_head or wp_footer, otherwise there is no opportunity to generate the script and link tags

So your options are:

  • use a later hook than wp_loaded that happens after the scripts have been enqueued
  • include the scripts manually inline
发布评论

评论列表(0)

  1. 暂无评论