0

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;
          }

1 Answer 1

2

I am going to guess that you have your code in the theme, probably functions.php. That code will only load when the theme is active, and after_switch_theme only runs after the theme change. There is no before_switch_theme that I am aware of.

You will need to have this code in a plugin or a mu-plugin file in order to have it work for all themes.

But don't...

This kind of template highjacking is going to make whatever you are doing very unpopular. Not to mention that it is a very messy way to do things. You are altering database values that you are going to have to eventually clean up again.

Though it is not clear why you need to do this, or even exactly what the end result is supposed to be, I am pretty sure that they right way to do it is with the template_include and/or template_redirect hooks.

For usage examples:

https://wordpress.stackexchange.com/search?q=user%3A21376+template_include
https://wordpress.stackexchange.com/search?q=user%3A21376+template_redirect

4
  • this page is unimportant and only serves as one use, also it is inside of a plugin, no themes, I will try those hooks you mentioned.
    – charles
    Commented May 16, 2014 at 13:32
  • Ok. So I am sure you are doing it wrong. If this is a template required by a plugin you should be using hooks.
    – s_ha_dum
    Commented May 16, 2014 at 13:35
  • thanks for helping, it looks like the template_include hook is working, I was over complicating it.
    – charles
    Commented May 16, 2014 at 13:37
  • yay, got it working, and I only done this for pages created by the plugin, exactly what I wanted, I updated with the working code.
    – charles
    Commented May 17, 2014 at 0:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.