I am building a custom theme which has an insane amount of templates. I have created an options page to allow the user to enable the page templates they wish to use. The plan is to then create a function to loop through the templates and add the ones they have enabled. I use the following function when creating a plugin but this is not being used in a plugin. I need something like the code below to work within the functions.php document.
add_filter( 'page_template', 'template_selection' );
function template_selection( $page_templates ){
$templates = get_option('templates');
if(in_array('1',$templates)){$page_template[] = dirname( __FILE__ ) . '/template1.php';}
if(in_array('2',$templates)){$page_template[] = dirname( __FILE__ ) . '/template2.php';}
return $page_templates;
}
The issue here is that the filter is not working. Any help with this matter is greatly appreciated.
page_template
filter runs when the page template is loaded for a page request.functions.php
. I get twoin_array
errors from$templates
being false instead of an array. It seems your problem is elsewhere.