Skip to content

Instantly share code, notes, and snippets.

@dstollie
Last active September 15, 2017 14:13
Show Gist options
  • Save dstollie/a5641375c5cbe784d6c8 to your computer and use it in GitHub Desktop.
Save dstollie/a5641375c5cbe784d6c8 to your computer and use it in GitHub Desktop.
Using Themosis templates in ACF
<?php
/**
* ACF has this fancy option to show ACF fields based on pages templates.
* One problem: Themosis templates are not registered as "default" WordPress templates.
* This file hooks into the acf filters and allows ACF to use the Themosis templates.
*/
// add themosis templates
add_filter('acf/location/rule_values/page_template', 'add_themosis_templates_to_acf_rules');
function add_themosis_templates_to_acf_rules($choices)
{
$key = 'app';
$configFile = 'templates';
$configTemplates = include(themosis_path($key) . 'config' . DS . $configFile . CONFIG_EXT);
$templates = array();
foreach ($configTemplates as $configTemplate) {
$prettyTemplateName = str_replace(array('-', '_'), ' ', ucfirst(trim($configTemplate)));
$templates[$configTemplate] = $prettyTemplateName;
}
return array_merge(array('none' => __('- None -')), $templates);
}
// get themosis templates
add_filter('acf/location/rule_match/page_template', 'get_themosis_templates_from_acf_rules', 11, 3);
function get_themosis_templates_from_acf_rules($match, $rule, $options)
{
// vars
$page_template = $options['page_template'];
// get page template
if (!$page_template && $options['post_id']) {
$page_template = get_post_meta($options['post_id'], '_themosisPageTemplate', true);
}
// compare
if ($rule['operator'] == "==") {
$match = ($page_template === $rule['value']);
} elseif ($rule['operator'] == "!=") {
$match = ($page_template !== $rule['value']);
}
// return
return $match;
}
@basbitfactory
Copy link

hi @sergiohidalgo,

your "fix" stopped working after the last ACF update... Any updates?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment