plugins - wp_enqueue_style on template_redirect level?

I am using the following function, which works fine, in order to provide a minimal output of HTML at a WordPress install

I am using the following function, which works fine, in order to provide a minimal output of HTML at a WordPress installation of mine:

add_action( 'template_redirect', 'test' );

function test()
{ 
?>
<!doctype html>
<html lang="en-US">
<head>
<link rel='stylesheet' type='text/css' href='<?php echo home_url() ?>/wp-content/plugins/myplugin/style.css'>
</head>
<body>
</body>
</html>
<?php 
    exit();
}

It does exactly what I want, but I'm considering of loading my style the proper way, using wp_enqueue_style.

Since im not loading wp_head, I tried it and it seems not possible on template_redirect level.

Is there any way to enqueue my style on my function without hardcoding it?

I am using the following function, which works fine, in order to provide a minimal output of HTML at a WordPress installation of mine:

add_action( 'template_redirect', 'test' );

function test()
{ 
?>
<!doctype html>
<html lang="en-US">
<head>
<link rel='stylesheet' type='text/css' href='<?php echo home_url() ?>/wp-content/plugins/myplugin/style.css'>
</head>
<body>
</body>
</html>
<?php 
    exit();
}

It does exactly what I want, but I'm considering of loading my style the proper way, using wp_enqueue_style.

Since im not loading wp_head, I tried it and it seems not possible on template_redirect level.

Is there any way to enqueue my style on my function without hardcoding it?

Share Improve this question edited May 4, 2019 at 12:23 cjbj 15k16 gold badges42 silver badges89 bronze badges asked May 4, 2019 at 12:10 JohnnyBratsoniJohnnyBratsoni 32 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

The reason to use wp_enqueue_style is to allow WordPress to manage dependencies between stylesheets. Since you're only outputting a barebones page here, there really is no point to use it.

That said, if you look at the hook order, you see that template_redirect is actually called quite late in the process, just before the scripts and styles are enqueued, but before wp_head. As you can see in the list of default filters/actions (and as you found out) the scripts and styles are only printed at the wp_head stage, with this line of code:

add_action( 'wp_head', 'wp_print_styles', 8 );

So, if you want to enqueue a file at the template_redirect stage you will have to cheat by running both the enqueueing and printing process early. That is not too difficult, though, because WordPress has already been fully loaded at this stage. All functions are available, including the styles class. In stead of your line <link rel = ... > you could execute the styles chain of command early, like this:

wp_styles (); // initialize the styles object
wp_enqueue_style ('my_handle', 'path-to-file'); // make the file ready for printing
wp_print_styles (); // print the enqueued style

I didn't test this, because it doesn't really make sense, but I hope you get the point.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745523952a4631384.html

相关推荐

  • plugins - wp_enqueue_style on template_redirect level?

    I am using the following function, which works fine, in order to provide a minimal output of HTML at a WordPress install

    12小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信