{"id":4512,"date":"2023-07-12T04:46:41","date_gmt":"2023-07-12T04:46:41","guid":{"rendered":"https:\/\/academy.bricksbuilder.io\/?post_type=bricks_article&p=4512"},"modified":"2023-11-19T10:25:04","modified_gmt":"2023-11-19T10:25:04","slug":"filter-bricks-active_templates","status":"publish","type":"bricks_article","link":"https:\/\/academy.bricksbuilder.io\/article\/filter-bricks-active_templates\/","title":{"rendered":"Filter: bricks\/active_templates"},"content":{"rendered":"\n

Modify the templates applied on a page programmatically (@since 1.8.4<\/code>) <\/p>\n\n\n\n

This is an alternative to setting an active template via the bricks\/screen_conditions\/scores<\/a> filter.<\/p>\n\n\n\n

Please note that this filter is executed after bricks\/screen_conditions\/scores<\/code><\/p>\n\n\n\n

Example: Do not use the Single template if the post has been edited with Bricks<\/h3>\n\n\n\n

In this example, we want to exclude the single post from utilizing a template if it contains Bricks data (Edit with Bricks).<\/p>\n\n\n\n

add_filter( 'bricks\/active_templates', 'set_my_active_templates', 10, 3 );\nfunction set_my_active_templates( $active_templates, $post_id, $content_type ) {\n  \/\/ Only run my logic on the frontend\n  if ( ! bricks_is_frontend() ) {\n    return $active_templates;\n  }\n\n  \/\/ Return if single post $content_type is not 'content'\n  if ( $content_type !== 'content' ) {\n    return $active_templates;\n  }\n\n  \/\/ Return: Current post type is not 'post'\n  $post_type = get_post_type( $post_id );\n\n  if ( $post_type !== 'post' ) {\n    return $active_templates;\n  }\n\n  \/**\n   * $active_templates is an array with different important keys\n   * \n   * $active_templates['header'] is the header template ID, set it to 0 if do not want to use any template\n   * $active_templates['content'] is the content template ID, set it to current post ID if do not want to use any template\n   * $active_templates['footer'] is the footer template ID, set it to 0 if do not want to use any template\n   * \n   * $active_templates['search'] is the search template ID, will only be used if $content_type is 'search'\n   * $active_templates['archive'] is the archive template ID, will only be used if $content_type is 'archive'\n   * $active_templates['error] is the error template ID, will only be used if $content_type is 'error'\n  *\/\n\n  \/\/ Check if the current post has Bricks data, return value is an array\n  $bricks_data = \\Bricks\\Database::get_data( $post_id, 'content' );\n\n  if ( count( $bricks_data ) > 0 ) {\n    \/\/ Has Bricks data: Don't use any template, set the $active_templates['content'] to current post ID\n    $active_templates['content'] = $post_id;\n\n    \/\/ To disable header & footer (e.g. landing page) set $active_templates['header'] & $active_templates['footer'] to 0\n    $active_templates['header'] = 0;\n    $active_templates['footer'] = 0;\n  }\n\n  return $active_templates;\n}<\/code><\/pre>\n\n\n\n

Example: Change a single template based on a custom field<\/h3>\n\n\n\n

There is a scenario like having multiple single templates for a custom post type. Using this filter, you can decide which template to apply based on a custom field.<\/p>\n\n\n\n

add_filter( 'bricks\/active_templates', 'set_active_templates_by_custom_field', 10, 3 );\n\nfunction set_active_templates_by_custom_field( $active_templates, $post_id, $content_type ) {\n  \/\/ Only run my logic on the frontend\n  if ( ! bricks_is_frontend() ) {\n    return $active_templates;\n  }\n\n  \/\/ Return if single post $content_type is not 'content'\n  if ( $content_type !== 'content' ) {\n    return $active_templates;\n  }\n\n  \/\/ Return: Current post type is not 'project'\n  $post_type = get_post_type( $post_id );\n\n  if ( $post_type !== 'project' ) {\n    return $active_templates;\n  }\n\n  \/\/ Get the custom field value from Metabox\n  $value = absint( rwmb_meta( 'use_template_id' ) );\n\n  \/\/ Value not empty: Set $active_templates['content'] to the value\n  if ( $value > 0 ) {\n    $active_templates['content'] = $value;\n    \/\/ If single template, the page settings will be used, so no need to set header and footer\n  }\n\n  return $active_templates;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"

Modify the templates applied on a page programmatically (@since 1.8.4) This is an alternative to setting an active template via the bricks\/screen_conditions\/scores filter. Please note that this filter is executed after bricks\/screen_conditions\/scores Example: Do not use the Single template if the post has been edited with Bricks In this example, we want to exclude the […]<\/p>\n","protected":false},"author":6,"featured_media":0,"menu_order":38,"template":"","format":"standard","meta":{"footnotes":""},"_links":{"self":[{"href":"https:\/\/academy.bricksbuilder.io\/api\/wp\/v2\/bricks_article\/4512"}],"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\/6"}],"version-history":[{"count":12,"href":"https:\/\/academy.bricksbuilder.io\/api\/wp\/v2\/bricks_article\/4512\/revisions"}],"predecessor-version":[{"id":5274,"href":"https:\/\/academy.bricksbuilder.io\/api\/wp\/v2\/bricks_article\/4512\/revisions\/5274"}],"wp:attachment":[{"href":"https:\/\/academy.bricksbuilder.io\/api\/wp\/v2\/media?parent=4512"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}