I am trying to update the postmeta table when I switch themes. I am updating the metavalue to be template-name.themename.php.
However, this only works with the default themes in wordpress.
If I go to add a new theme, and activate that, it does not work.
The metavalue updates correctly, but the new theme is overriding the custom template.
Here is the code I have.
<?php
add_action('after_switch_theme', 'update_templates');
function update_templates() {
global $wpdb;
$new_template = "pl-template.". strtolower(trim(get_current_theme())) .".php";
$pl_pages = $wpdb->get_results(" SELECT * FROM {$wpdb->prefix}posts WHERE pevo_page = 1");
foreach ( $pl_pages as $pages) {
$post_id = $pages->ID;
$wpdb->query(
"
UPDATE {$wpdb->prefix}postmeta
SET meta_value = '$new_template'
WHERE post_id = $post_id
"
);
}
}
?>
The custom template, is just blank right now and just displays the page title
<?php
/*
Template Name: Template Name
*/
$the_title = get_the_title();
echo $the_title;
?>
working code thanks to the answers, I used an array because this is within my plugin class.
add_filter( 'template_include', array($this, 'plugin_page_templates'));
function plugin_page_templates() {
global $wpdb;
$pagename = get_the_title();
$ispluginpage = $wpdb->get_var("SELECT plugin_page FROM {$wpdb->prefix}posts WHERE post_title = '$pagename'");
if ($ispluginpage == 1) {
return plugin_dir_path( __FILE__ ) . 'includes/plugin-template.php';
}
return $template;
}