PHP class for 2 body classes

Hi, roots noob here…

I actually found Roots after looking at Lumberjack which is built on Bedrock & Timber and I’m really liking how it is structured, but I have a question regarding the PHP Class structure based on the Body classes

as illustrated here:

https://discourse.roots.io/uploads/default/original/2X/b/b1e63bb1399a1a3962141ab4702fb6877961eb56.png

what would be best approach for creating a PHP Class that targeted 2 body classes (so in that case “location-template-default single”)

I understand I can have separate controllers Single and LocationTemplateDefault, which will both run, but what if I need a single class that runs when both of those 2 classes are present

Not necessarily the best example and I don’t have a current use case, but I could see where this might potentially be required.

(I guess this would apply to the javascript routing as well)

thanks for any suggestions
J

This is a difficult question to answer without a use case. The body classes are just a simple way to answer the question “What is this?” Using them to target controllers is mostly a matter of convenience, since WordPress is doing a lot of work for us. If we find ourselves in a situation where no single body class represents the thing we want to target, we can just create our own: https://github.com/roots/sage/blob/19057f60be1a5ecc466b2a0a1cb0eca0d367bb6e/app/filters.php#L8 In most cases, though, I feel like your issue would boil down to “I want to share functionality I wrote for ControllerA with ControllerB.” In this case, your best path would just be to extract that functionality from ControllerA so that it can be used in multiple places (Traits can be a good solution to this problem).

If you can provide a more concrete use case we may be able to give you a better answer.

1 Like

Hi thanks for the quick reply… you’re right,I’m best coming back to this when I have an actual practical use case

I was just wondering how to approach when I have body class=“foo bar…” and I want to run a FooBar class (which has unique functionality that Foo and Bar individually don’t)

I guess this falls outside the role of this body class system and I’d need to take a separate approach

Thanks again

J

If I understand the question correctly, one way to go about it could be to use the filter ’body_class’ (with a high value for priority so it fires after all other classes has been added) and check if foo and bar is present. If they are, add foo-bar to the body classes array.

2 Likes

thanks @folbert, i had assumed it was probably a case of checking the existing classes and adding a new one. so yes adding foo-bar would allow the FooBar class to run.

do we have any control over the order they are executed from the body class?

I mean ideally i’d want to have an architecture where the order of Foo, Bar and FooBar didn’t matter, but just wondering if it specifically reads and processes left to right etc?

thanks
J

This topic was automatically closed after 42 days. New replies are no longer allowed.