Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add_action( 'get_footer'... in 3rd party plugin: the added action is not being called #2420

Open
code-en-design opened this issue Jan 27, 2021 · 10 comments
Assignees
Labels
docs help wanted Do you know computer? Then lend a hand and have your code live in infamy!

Comments

@code-en-design
Copy link

In Grids plugin (https://justevolve.it/grids/) this:
add_action( 'get_footer', array( $this, 'styles' ), 100 );
did not work, the added footer action is not called by Timber

My temporary solution, I added:
get_footer();
on the end of the index.php and page.php, and added an empty footer.php

Expected behavior

add_action( 'get_footer', array( $this, 'styles' ), 100 ); in the Grid plugin should output some css in the (Timber) footer

Actual behavior

The css is not being rendered, because the added 'styles' action from the plugin is not being called

Steps to reproduce behavior

Install Grid and try a Timber Theme

What version of WordPress, PHP and Timber are you using?

WordPress 5.6, PHP 7.4.12, Timber 1.18.2

How did you install Timber? (for example, from GitHub, Composer/Packagist, WP.org?)

Installed with Composer

@acobster
Copy link
Collaborator

I'm not sure I understand your issue.

My temporary solution, I added:
get_footer();
on the end of the index.php and page.php, and added an empty footer.php

Do you mean that without calling get_footer() from your theme, your added styles action never runs? That sounds like expected behavior to me, but it's difficult to say what's going on without seeing the full code of the PHP template and corresponding Twig template having the issue.

Install Grid and try a Timber Theme

Do you mean the Timber Starter Theme?

@code-en-design
Copy link
Author

Do you mean that without calling get_footer() from your theme, your added styles action never runs?

Yes, the added action from: add_action( 'get_footer', array( $this, 'styles' ), 100 ); in the 'grid' plugin does not run.
I work with the composer version of Timber, but I also tried it with the Timber Starter Theme'. It only works when I add get_footer in page.php:

// I added this:
get_header();

$context = Timber::context();

$timber_post     = new Timber\Post();
$context['post'] = $timber_post;
Timber::render( array( 'page-' . $timber_post->post_name . '.twig', 'page.twig' ), $context );

// I added this:
get_footer();

I would expect that the get_footer() was already called by Timber? I guess there will be more plugins that count on add_action( 'get_footer', ... ?
...
I see this remark in footer.php now:

  • Third party plugins that hijack the theme will call wp_head() to get the header template. etc...

To make this work I had to add get_header(); and get_footer(); in the page.php
I can live with that, but it's a bit unexpected, I was assuming that Timber was handling the standard calls from WP, like get_footer. But if this is expected behavoir, then it's not really clear in the documentation I think.

A big thanks for responding to my question!

@acobster
Copy link
Collaborator

I would expect that the get_footer() was already called by Timber?

The Timber starter theme does call it from base.twig, here. Is it possible you modified page.twig or base.twig in a way that prevents this call?

So, to answer your question, you typically won't call wp_head() or wp_footer() from your PHP template directly. Instead, the convention is to call it from your shared layout file, in this case base.twig. I agree it's not particularly clear from the docs that that's what's supposed to happen though.

@jarednova @gchtr not sure where the best place in the 1.x would be to add this info, but in 2.x I think the Template Inheritance section of the Getting Started guide would make sense.

@gchtr
Copy link
Member

gchtr commented Jan 28, 2021

I can live with that, but it's a bit unexpected, I was assuming that Timber was handling the standard calls from WP, like get_footer. But if this is expected behavoir, then it's not really clear in the documentation I think.

Totally agree that this is not clear enough. It’s one of those things that easily get forgotten for us folks who have been using Timber for quite some time. Thanks for calling us out on that, @code-en-design! 👍

not sure where the best place in the 1.x would be to add this info, but in 2.x I think the Template Inheritance section of the Getting Started guide would make sense.

Yep, that’s one section where we can add it. It’s currently mentioned in the Functions Guide as an example. We can definitely improve that.

I’ll leave this open so I can work on the proper documentation.

@gchtr gchtr self-assigned this Jan 28, 2021
@gchtr gchtr added the docs label Jan 28, 2021
@code-en-design
Copy link
Author

Thanks @acobster, thats exactly what I did in my theme.
I'm not sure the ob_start() / ob_end_clean() trick in header.php / footer.php is really needed in the starter theme when the {{ function('wp_footer') }} is in base.twig. I my case it was enough to have an empty footer.php.

Thanks also @gchtr , it's great to get detailed feedback!

@Levdbas
Copy link
Member

Levdbas commented May 18, 2023

To chime in on this:

Since I added the following snippets in my timber projects, I never ran into compatibility issues again. The do action hooks fixed some compat issues with the Rank Math SEO plugin for example.

{# head.twig in the <head> #}
{% do action('get_header') %}
{{ function('wp_head') }}
{# base.twig just before the closing body tag. #}
 {% do action('get_footer') %}
{{ function('wp_footer') }}

@gchtr gchtr removed their assignment Oct 17, 2023
@gchtr gchtr added the help wanted Do you know computer? Then lend a hand and have your code live in infamy! label Oct 17, 2023
@gchtr gchtr added the 1.x label Oct 24, 2023
@Levdbas Levdbas added 2.0 and removed 1.x 2.0 labels Dec 13, 2023
@Levdbas
Copy link
Member

Levdbas commented Feb 27, 2024

To chime in on this:

Since I added the following snippets in my timber projects, I never ran into compatibility issues again. The do action hooks fixed some compat issues with the Rank Math SEO plugin for example.

{# head.twig in the <head> #}
{% do action('get_header') %}
{{ function('wp_head') }}
{# base.twig just before the closing body tag. #}
 {% do action('get_footer') %}
{{ function('wp_footer') }}

@timber/rangers , how have you solved this on a day to day basis? Because I would like to add this to the starter theme/docs.

@gchtr
Copy link
Member

gchtr commented Feb 27, 2024

@Levdbas I never ran into a situation where I had to add the get_footer or get_header hooks. But to be more compatible with WordPress, it makes total sense to do it this way. I’d say we should add that to the documentation where we can.

We also have the footer.php from the starter theme in our themes, but I doubt that this is actually ever used in our websites. I don’t know of any plugin that does this. We could possibly get rid of this and instead add this solution to the documentation somewhere.

@Levdbas Levdbas self-assigned this Feb 27, 2024
@Levdbas
Copy link
Member

Levdbas commented Feb 27, 2024

Gotcha, in my case, I never uses the footer.php solution in my 100+ timber projects :D So I think running those two hooks mimics that. I self-assigned this to add this to the docs.

Btw, in starter we also have the header.php besides the footer.php. I never used that as well. Shall I create a PR implementing my suggested code and get rid of those two files?

@gchtr
Copy link
Member

gchtr commented Feb 27, 2024

Gotcha, in my case, I never uses the footer.php solution in my 100+ timber projects :D So I think running those two hooks mimics that. I self-assigned this to add this to the docs.

👍

Btw, in starter we also have the header.php besides the footer.php. I never used that as well. Shall I create a PR implementing my suggested code and get rid of those two files?

Yes, I’d be all for that. From comments I have seen in various places, it’s not clear to everyone what these files do. So if we can have a simpler solution, let’s do that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs help wanted Do you know computer? Then lend a hand and have your code live in infamy!
Projects
None yet
Development

No branches or pull requests

4 participants