Like adding Google Analytics code, and some other things surrounding the <head>
tag, how's all this done in FSE as there's no header.php
? Should I use functions.php
for everything?
(Also can somebody make a fse tag in stackexchange?)
As far as I understand the wp_head
and wp_footer
actions should still work under FSE in WP 5.9+, through the template canvas PHP file that's loaded through the locate_block_template()
. That PHP file contains the basic HTML structure and there we find e.g. the familiar wp_head()
and wp_footer()
function calls that we usually had in the old footer.php
and header.php
theme files. The current block template HTML from get_the_block_template_html()
is now displayed within the body tag from the template canvas.
So in FSE use e.g. the theme's functions.php
file or a custom plugin to add your wp_head
and wp_footer
actions in PHP.
When I was creating a FSE theme I had the same question. I found that the Shorcode Block is an easy way to insert coding into any template. So for a shortcode [custom_content] this is the coding required in the functions file or move the code to a custom.php file in another directory.
/assets/custom.php
function add_custom_content() {
...
return [new code...];
}
add_shortcode('custom_content', 'add_custom_content');
functions.php
require get_template_directory() . '/assets/custom.php';
Edit: Ok so FSE templates are part of the <body> so this didn't answer the question.
To get code into the <head> - this works
function add_to_head() {
echo '<script> /* code... */ </script>';
}
add_action( 'wp_head', 'add_to_head' );
wp_head
action still work?funcitons.php
will be kinda ugly.