Code Monkey home page Code Monkey logo

Comments (2)

gmazzap avatar gmazzap commented on August 11, 2024 1

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'
        ]
    ));

from cortex.

wujekbogdan avatar wujekbogdan commented on August 11, 2024

@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 :)

from cortex.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.