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
1 Answer
Reset to default 1You 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