Issue with $post usage and third party plugins (Yoast, Learndash) #78
Comments
|
I've created a pull request for this, hope it helps! |
|
Thanks @stephenniemans! I had followed those issues, but had determined these to be on Yoast's side of the ball. That said, if a small tweak to template code can mitigate, I'm all for it. What doesn't quite make sense to me though is that in the starter theme files, ( That said, this is WordPress and PHP so there are plenty of ghosts in the machine. Have you confirmed that this tweak resolves the issue with Yoast? |
|
PhpStorm/Xdebug combo would easily illuminate the situation details (i.e. not about ghosts), but I'm not equipped w/ Twig to do this work (just a lurker here). Maybe someone else? |
I wanted to check this for myself as well, so I did another clean install with only timber and the starter theme installed. I generated the 'unit tested plugin' with WP-CLI and had it output the class of global $post through a shortcode: // Your code starts here.
function timber_test_shortcode(){
global $post;
echo '$post class: ' . get_class($post);
}
add_shortcode('timber_test', 'timber_test_shortcode');This is a recording of the behavior I observed. You can see the output of
I don't use Yoast so I am unfortunately unable to test it properly, they have a number of separate issues related to this, with some partial fixes throughout their version history. This makes it hard to pin down to what extent their team has resolved it. Myself, I ran into my issues using Learndash for a site, and googling the issue brought up the Yoast issue so I thought to include it in my description. I can confirm at least that the change solved my Learndash issues. Niche as my case may be, I suspect it could pop up unexpectedly in other situations if plugin devs implement checks against the class of
I think the docs show the same example, if it helps I'd be happy to submit a PR for that as well (I updated my previous because I overlooked changing the line of code below). |
|
Wrt So if the controller rendering function has And as we all probably know, in The Loop, |
Description of the issue
In the starter theme and documentation, the following code is included:
This seems to override the global
$postobject (codex reference). I ran into issues with various issues with third party plugins that perform safety checks of$postto see if the class of it is equal toWP_Post, where it will fail because$postis now of classTimber\Post. Examples of this include:and
This causes errors and erratic behavior, as reported here for example:
Example 1
Example 2
Example 3
Example 4
Proposed change
Please correct me if I'm mistaken, but since Timber templating tends to refer to the
$context['post']rather than the$post, we could probably rewrite the code in the doc examples and starter theme as:This would save issues for user of these plugins and any others that expect
$postto be the global variable described in the WP Codex.The text was updated successfully, but these errors were encountered: