4

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?)

2
  • 1
    Does the wp_head action still work?
    – kero
    Commented Apr 13, 2022 at 11:14
  • @kero yes it does. Looks like my funcitons.php will be kinda ugly. Commented Apr 14, 2022 at 8:02

2 Answers 2

6

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.

3

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' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.