I need to add an ACF field, callable on any blade template, that will inject editable code into the document head. (In this case, schema.org json LD schemas). I’m wondering if there’s a way I can call sage to do it at the index.php level like this:
I dug into this for a bit. I think because the ThemeServiceProvider is (as you called) booted inside the Roots\app instance, you’re going to have a tough time loading your views/templates outside of that instance.
If you were going to refactor everything inside the roots\app instance, you could probably just do this:
Then in site/web/app/themes/mytheme/resources/views/layouts/app.php
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
@yeild('head'); //This is, I think, the best way to add in blade template content to the <head>
</head>
<body <?php body_class(); ?>>
<?php wp_body_open(); ?> //Default WP Stuff
<?php do_action('get_header'); ?> //Default WP Stuff
<div id="app"> //Now we're back to the app
<main class="main">//This is normally in views.layouts.app
@yeild('content');
</main>
<?php do_action('get_footer'); ?>
@yield('footer') //Add in footer scripts, because why not!
<?php wp_footer(); ?>
</body>
Lastly, in one of your templates, like template.product-schema.blade.php
@extends('layouts.app')
@section('head')
//Your <head> content here
@endsection
@section ('content')
//Your <main> content here
@endsection
@section ('footer')
//Your pre-</body> content here
@endsection
I haven’t tested this, so your milage may vary. Still, I think this is the best way to make this work for you, if if you need it. ( Note: I don’t think you need it for schemas. My work with Shopify, et. al, makes me think it’s perfectly acceptable to include these outside of the <head>, specifically at the end of the <body>).
Not in the way you’re probably thinking of it, no. You can use the above technique to do stuff in the head, but it’s not “inside” of the Sage template renderer.
I would not recommend this. It is the way it is to allow various WordPress plugins that need to do things with the header and footer hooks to do those things…appropriately. If you put it all inside Sage you may run into issues with plugins not working in the way you expect, or not working at all.
Incidentally I did try a method that @evankford detailed, as it was along the same lines as my originally proposed solution. It did work, but now you’ve got me scared per your comment!:
So I refactored per your suggestion using the wp_head hook. It’s working perfectly well, thank you!
@evankford Thank you so much for looking into a solution, it seems we were thinking similarly. And thank you for the note about the schema being able to exist outside the head, that may make the <head> business unnecessary.