Template parts/block patterns sync/php execution at the same time
-
I am struggling to make wordpress work exactly as I want, but I’m not sure if I’m doing someting wrong or it is impossible. I want 4 conditions to be met at once:
1) I’m using block themes with Full Site Editing feature.
2) I want to have some sort of entity that can be inserted into any template, say, a custom header block. Tat entity needs to ne stored in a file, and when the file is updated, I want the changes to be reflected on every page using the template with this entity.
3) The templates themselves, however, should be stored in the database, so I can edit them via the Site editor
4) I need to execute php from inside these entities.So that’s what I’ve done up to that moment:
1) Created a child theme and a file there, /patterns/custom-header.php. This is how the file starts:<?php
/**
* Title: Custom header
* Slug: fitness-fse-child/custom-header
* Categories: fitness-fse
*/
?>This is followed by a mix of html and php code.
2) created another file there, /parts/custom-header.php, with this code:<!-- wp:pattern {"slug":"fitness-fse-child/custom-header"} /-->
3) Added that content as a template part to my template. Everything’s fine except for the main thing: when I change custom-header.php, these changes are not reflected on the pages which use that template. Deleting-refreshing-readding this template part don’t help.
4) Following the LLM’s advice, I tried to register this template part in the functions.php:if (!function_exists('fitness_fse_child_register_patterns')) {
function fitness_fse_child_register_patterns()
{
register_block_pattern(
'fitness-fse-child/custom-header',
array(
'title' => __('Custom Header', 'fitness-fse-child'),
'categories' => array('fitness-fse'),
'content' => file_get_contents(get_theme_file_path('/patterns/custom-header.php')),
)
);
}
add_action('init', 'fitness_fse_child_register_patterns');
}after that, the part started syncing as I wanted to. But unfortunately, this prevents the php code inside the custom-header.php from executing. For example, if I have
<img
class="nav-logo-img"
src="<?php echo get_stylesheet_directory_uri() ?>/assets/img/LH_LOGO-02.png"
alt=""
/>the way this will be interpreted is pure html:
https://localhost/site.local/%3C?php%20echo%20get_stylesheet_directory_uri()%20?%3E/assets/img/LH_LOGO-02.pngSo the question is, what am I doing wrong and is this even possible to make it sync, php executable, with templates in the database and their parts in files at the same time?
- You must be logged in to reply to this topic.