{"id":3340,"date":"2022-10-13T06:48:47","date_gmt":"2022-10-13T06:48:47","guid":{"rendered":"https:\/\/academy.bricksbuilder.io\/?post_type=bricks_article&p=3340"},"modified":"2022-10-13T07:01:00","modified_gmt":"2022-10-13T07:01:00","slug":"filter-bricks-screen_conditions-scores","status":"publish","type":"bricks_article","link":"https:\/\/academy.bricksbuilder.io\/article\/filter-bricks-screen_conditions-scores\/","title":{"rendered":"Filter: bricks\/screen_conditions\/scores"},"content":{"rendered":"\n

Bricks selects the template & theme style for a specific page according to the conditions you’ve defined.<\/p>\n\n\n\n

Internally this is done via a scoring system from 0 to 10. 0 is the least specific. 10 being the most specific (e.g. specific post ID).<\/p>\n\n\n\n

For each template\/theme style condition that could apply to a certain context, the template\/theme style earns a specific score. After analyzing all templates\/theme styles, Bricks chooses the one with the highest score.<\/p>\n\n\n\n

If you need to add new template conditions using the filter builder\/settings\/template\/controls_data<\/a><\/code> or bricks\/theme_styles\/controls<\/code> for theme styles, you then need to hook into the scoring logic to score the templates\/theme styles based on the custom conditions. <\/p>\n\n\n\n

This is where the bricks\/screen_conditions\/scores<\/code> filter comes in handy, like so:<\/p>\n\n\n\n

add_filter( 'bricks\/screen_conditions\/scores', function( $scores, $condition, $post_id, $preview_type ) {\n  \/\/ Run custom logic to score the template\/theme style $condition\n  \/\/ $scores[] = 5; \n \n  return $scores;\n}, 10, 4 );<\/code><\/pre>\n\n\n\n

Example 1: Add the score for a specific author role in an author archive template<\/strong><\/p>\n\n\n\n

After adding the control using the builder\/settings\/template\/controls_data<\/a><\/code> (check example 1), we now need to hook in bricks\/screen_conditions\/scores<\/code> to score the template based on the condition, like so:<\/p>\n\n\n\n

add_filter( 'bricks\/screen_conditions\/scores', function( $scores, $condition, $post_id, $preview_type ) {\n  if ( is_author() && $condition['main'] === 'archiveType' && isset( $condition['archiveType'] ) && in_array( 'author', $condition['archiveType'] ) && isset( $condition['archiveAuthorRoles'] ) ) { \n    $user = get_queried_object();\n\n    if ( ! empty( $user->roles ) && is_array( $user->roles ) ) {\n      foreach ( $user->roles as $role_name ) {\n        if ( in_array( $role_name, $condition['archiveAuthorRoles'] ) ) {\n          $scores[] = 9;\n        }\n      }\n    }\n  }\n\n  return $scores;\n}, 10, 4 );<\/code><\/pre>\n\n\n\n

<\/p>\n","protected":false},"excerpt":{"rendered":"

Bricks selects the template & theme style for a specific page according to the conditions you’ve defined. Internally this is done via a scoring system from 0 to 10. 0 is the least specific. 10 being the most specific (e.g. specific post ID). For each template\/theme style condition that could apply to a certain context, […]<\/p>\n","protected":false},"author":1,"featured_media":0,"menu_order":58,"template":"","format":"standard","meta":{"footnotes":""},"_links":{"self":[{"href":"https:\/\/academy.bricksbuilder.io\/api\/wp\/v2\/bricks_article\/3340"}],"collection":[{"href":"https:\/\/academy.bricksbuilder.io\/api\/wp\/v2\/bricks_article"}],"about":[{"href":"https:\/\/academy.bricksbuilder.io\/api\/wp\/v2\/types\/bricks_article"}],"author":[{"embeddable":true,"href":"https:\/\/academy.bricksbuilder.io\/api\/wp\/v2\/users\/1"}],"version-history":[{"count":7,"href":"https:\/\/academy.bricksbuilder.io\/api\/wp\/v2\/bricks_article\/3340\/revisions"}],"predecessor-version":[{"id":3351,"href":"https:\/\/academy.bricksbuilder.io\/api\/wp\/v2\/bricks_article\/3340\/revisions\/3351"}],"wp:attachment":[{"href":"https:\/\/academy.bricksbuilder.io\/api\/wp\/v2\/media?parent=3340"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}