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

How to add body classes depending on a Cortex route - the right way. #24

Closed
wujekbogdan opened this issue Mar 10, 2018 · 2 comments
Closed
Assignees
Labels

Comments

@wujekbogdan
Copy link

I know that I could use do_action('my_custom_action') / did_action('my_custom_action') check as follows:

add_action('cortex.routes', function (RouteCollectionInterface $routes) {
    $routes->addRoute(new QueryRoute(
        '/my/route',
        function (array $matches) {
            do_action('my_custom_action');
            return [
                // params
            ];
        }
    ));
});

add_filter('body_class', function ($classes) {
    if (!did_action('my_custom_action')) {
        return $classes;
    }

    return array_merge($classes, ['my_class']);
});

or I could add some custom query variable and then add custom classes depending on the query variable existence... but none of these methods sound right to me.

Is there a more elegant way?

@gmazzap
Copy link
Collaborator

gmazzap commented Mar 20, 2018

Hi @wujekbogdan

If it something that you want to do for all matched routes you could use 'cortex.matched' or 'cortex.matched-after', something similar to:

use Brain\Cortex\Router\MatchingResult;

add_action('cortex.matched', function(MatchingResult $results) {

    $route = $results->route();
    $matches = $results->matches();
    $matchedPath = $results->matchedPath();
    $template = $results->template();

    // calculate classes here based  on info above...
    $customClasses = [];

    add_filter('body_class', function ($classes) use ($customClasses) {
        return array_merge($classes, $customClasses);
    });

});

If it is something that you want to do only for specific routes, that could be done via an "after" Route callback.

namespace Wujekbogdan\Project;

/**
 * @param array $vars
 * @param \WP $wp
 * @param string|false $template
 * @param array $matches
 */
function setupBodyClassesForRoute(array $vars, \WP $wp, $template, array $matches) {

    // calculate classes here based parameters
    $customClasses = [];

    add_filter('body_class', function ($classes) use ($customClasses) {
        return array_merge($classes, $customClasses);
    });
}

$routes->addRoute(new QueryRoute(
        '/my/route',
        function (array $matches) {
            return [
                // params
            ];
        },
        [
            'after' => 'Wujekbogdan\Project\setupBodyClassesForRoute'
        ]
    ));

@gmazzap gmazzap self-assigned this Mar 20, 2018
@wujekbogdan
Copy link
Author

@gmazzap
Thank you very much for a very comprehensive response. The second solution is exactly what I was looking for - much cleaner than a solution based on did_action that I came up with :)

@gmazzap gmazzap closed this as completed Jul 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants