• Resolved shawmutsteve

    (@shawmutsteve)


    Hi

    I wrote a plugin that displays dynamic, non-WP (real estate) content in pages and posts. I allow the admin to select which theme template to use so that they can decide if my dynamic content will be full page or have a sidebar.

    My plugin renders a few different types of dynamic content. Real Estate listing pages display correctly using the specified template. However, the Property Detail page is not displaying with the specified template – always defaulting to the theme default template. My debug clearly shows that I am correctly specifying the template to use.

    I was originally using this to tell WP which template to use:

    update_post_meta($posts[0]->ID, '_wp_page_template', $roverTemplate);

    I saw this problem, and wondered if it wasn’t working because I was using a bogus page_id. So I created a record in wp_posts, got the page_id (ID) and used it. Same results. Hmmh.

    I was just wondering if anyone had any suggestions on how to troubleshoot this.

    Thank you
    Steve

Viewing 15 replies - 1 through 15 (of 18 total)
  • Hi Steve,

    Looking at the one line of code, it is a little difficult to provide you with any insight, but I’ll give it a try.
    What does the array $posts contain exactly? If it is an array of the posts (in this case, pages) that will be using the $roverTemplate, a foreach could come in handy here.
    Would that make sense?

    Thread Starter shawmutsteve

    (@shawmutsteve)

    Hi Marventus

    $posts is an array of exactly one item: ‘ID’ => -1

    As I mentioned, this works for some of my pages. I’m not thinking that this code is faulty, but rather there are characteristics of WP template selection logic that I’m not fully understanding. So I’m really just looking for more debug to turn on to figure out what WP is doing behind the scenes.

    Thanks !
    Steve

    I see what you mean. It is indeed puzzling, since AFAIK your code should be working. Since you mentioned that this works in another page, are there any significant differences between both templates that might be causing update_post_meta to fail?
    The other thing that crossed my mind, but it might be a little far-fetched, is that perhaps there is some sort of permissions (or other) issue on that one table entry that is blocking the update process. If that were the case, you could try to create a new page and paste the contents of the non-working one inside, and then check if the meta info gets updated.
    That’s pretty much all I got. I know it’s not much, but I guess I’m as puzzled as you are about this. I’ll keep thinking see if I come up with something.

    Edit: How about using add_post_meta instead, with the unique parameter set to false? That should force an overwrite of the existing page template value, right?

    Thread Starter shawmutsteve

    (@shawmutsteve)

    This is increasingly fascinating. Your suggestion of using add_post_meta worked for the Property Details page, but still doesn’t work for the Control Panel page.

    This is a three shower problem, I think.

    Thanks so much – good progress !

    Glad it worked a little… Hmmm. Indeed it is fascinating. As a last resort, but this is pretty “hardcoDe” (hehehe), you could try an SQL UPDATE statement directly on the post_meta table, instead of doing it through a default WP function.
    There is a lot of debate on the forums about whether direct DB manipulation is desirable, but most objections have to do with resource usage on high traffic sites, which wouldn’t be a problem in this case since the values in question would only be updated from time to time by administrators.
    So I say: go for it!

    Thread Starter shawmutsteve

    (@shawmutsteve)

    Figured it out.

    For this one page that my plugin was creating, WP was considering it a single post (is_single === true). Therefore, even though I was setting the template via update_post_meta, it was quietly being ignored in favor in template-loader.php with the value of get_single_template().

    The fix is simple -> set $wp_query->is_single when creating the post.

    Oh! that makes sense, although it is pretty strange that WP would treat a page as a post.
    Anywaym glad you were able to figure it out.
    Good luck with your plugin.
    Cheers!

    Hi,

    Could you please mark this topic as resolved?

    Thanks!

    Hey shawmutsteve please elaborate on “The fix is simple -> set $wp_query->is_single when creating the post.” I’m stuck on trying to get a plugin to register a template based on the page i can do it no prob for single posts using

    //add proposal custom post template
    
    function get_proposal_post_type_template($single_template) {
    
         global $post;
    
         if ($post->post_type == 'proposals') {
    
              $single_template = dirname( __FILE__ ) . '/themefiles/single-proposals.php';
    
         }
    
         return $single_template;
    
    }
    
    add_filter( "single_template", "get_proposal_post_type_template" ) ;

    but for this custom page myaccount i’m using

    //add myaccount page
    
    register_activation_hook(  __FILE__ , 'my_account_post' );
    function my_account_post() {
    
    	global $post;
    
    	$my_account_post = array();
    
    	$my_account_post['post_title'] = 'myAccount';
    
    	$my_account_post['post_content'] = 'This is a fake page used for myAccount. This content is not displayed, but DO NOT DELETE this post_content unless you know what you are doing.';
    
    	$my_account_post['post_status'] = 'publish';
    
    	$my_account_post['post_author'] = '1';
    
    	$my_account_post['post_type'] = 'page';
    
    	$my_account_post = wp_insert_post($my_account_post);
    
    	update_post_meta($my_account_post, '_wp_page_template', (plugins_url() . '/themefiles/myAccount.php'), true);
    }

    and it doesn’t seem to work and i’m stuck

    Thread Starter shawmutsteve

    (@shawmutsteve)

    Hi

    I cannot tell you that you are creating the post incorrectly, but it is different from how I am creating mine. I’m using this http://plugins.svn.wordpress.org/g-lock-double-opt-in-manager/trunk/fakepage.php as guidance.

    I did notice that you pass $my_account_post to update_post_meta() . I think you need to change this to $my_account_post[0]->ID (which of course you’ll need to set).

    Good luck !

    somehow a few characters got lost in cutting and pasting
    it was supposed to read

    update_post_meta($my_account_post_id, '_wp_page_template', (plugins_url() . '/themefiles/myAccount.php'), true);
    }

    i think i have a few variations in scratch paper format
    this one seemed promising but there’s something i’m overlooking somewhere

    update_post_meta($my_account_post_id, '_wp_page_template', ( dirname( __FILE__ ) . 'my_plugin_folder/themefiles/myAccount.php' ));

    i actually pulled this from a pretty good theme’s function file and it halfway works because i want the plugin to create the fake page at startup and load the page template using _wp_page_template.

    I just wish there was a simple hook like how there is for single custom posts that worked!

    i also found an entirely different function to do this, but it just doesn’t work right for me, it loads the template on all pages instead of just the myaccount login page. basically i’m using another non public custom post type called “clients” to manage usernames and passwords for accessing posts specific to that client. the myaccount page isn’t a post at all and it all works great within a theme. but i want it in a plugin, i like switching themes every so often without hard coding and ftping.

    //Add Page and Post Template Files to Current Theme 
    
    add_action("template_redirect", 'my_theme_redirect');
    
    function my_theme_redirect() {
        global $wp;
        global $wp_query;
        global $post;
        $plugindir = dirname( __FILE__ );
    
    //Set myAccount Custom Page Template SEMIWORKING blankets all pages
      if (isset($wp->query_vars['pagename'] ) == "myaccount") {
            $templatefilename = 'myAccount.php';
            if (file_exists(TEMPLATEPATH . '/' . $templatefilename)) {
                $return_template = TEMPLATEPATH . '/' . $templatefilename;
            } else {
                $return_template = $plugindir . '/themefiles/' . $templatefilename;
            }
            do_theme_redirect($return_template);
    
    }
    }
    
    //Finishing setting templates
    function do_theme_redirect($url) {
        global $post, $wp_query;
        if (have_posts()) {
            include($url);
            die();
        } else {
            $wp_query->is_404 = true;
        }
    }

    i had a hard time finding much about custom page templates outside of a theme folder and in the plugin folder. i’m sure i have the syntax wrong somehow, maybe there a better function altogether, but i’m sure there’s bound to be a better way of doing this.

    okay so i got this figured out

    this creates a single custom post template when ran in a plugin file

    //add proposal custom post template
    
    function get_proposal_post_type_template($single_template) {
    
         global $post;
    
         if ($post->post_type == 'proposals') {
    
              $single_template = dirname( __FILE__ ) . '/themefiles/single-proposals.php';
    
         }
    
         return $single_template;
    
    }
    
    add_filter( "single_template", "get_proposal_post_type_template" )

    this creates a custom page on plugin activation

    register_activation_hook(  __FILE__ , 'my_account_post' );
    function my_account_post() {
    
    	global $post;
    
    	$my_account_post = array();
    
    	$my_account_post['post_title'] = 'myAccount';
    
    	$my_account_post['post_content'] = 'This is a fake page used for myAccount. This content is not displayed, but DO NOT DELETE this post_content unless you know what you are doing.';
    
    	$my_account_post['post_status'] = 'publish';
    
    	$my_account_post['post_author'] = '1';
    
    	$my_account_post['post_type'] = 'page';
    
    	$my_account_post = wp_insert_post($my_account_post);

    and this creates the custom page template when ran in a plugin file

    //add myAccount custom post template
    
    function get_myaccount_template($page_template) {
    
         global $post;
    
         if ($post->post_name == 'myaccount') {
    
              $page_template = dirname( __FILE__ ) . '/themefiles/myAccount.php';
    
         }
    
         return $page_template;
    
    }
    
    add_filter( "page_template", "get_myaccount_template" ) ;

    with that in my main plugin file and all my auxilary plugin files in their respective subfolders i have custom post types with a custom login page working without manually adding files or hardcoding any values. i just need to clean it all up and style everything.

    sorry to revamp an old question, but I am also having trouble attaching a theme to a created page. so this is my code

    get_currentuserinfo();
    $loggedInNow = $current_user->ID;
    global $wp_query;
    $wp_query->is_single = false;
    $post_id = wp_insert_post(array(
    	'comment_status' => 'closed',
    	'post_title' => $_POST['passcattopostfunction'],
    	'post_content' => $_POST['contentsubmit'],
    	'post_author' => $loggedInNow,
    	'post_status' => 'publish',
    	'post_type' => 'page'
    	)
    );
    add_post_meta($post_id,' _wp_page_template', 'page-response.php', false);

    Everything works correctly except for the adding the template. I look in the database when I create a page and the meta data actually says ‘page-response.php’, but when I go to the backend I see under the Page Attributes that the template is still ‘Default Template’, then I navigate to the page and it’s definitely the default template. So I go and manually change the template to ‘Response Template’ (that’s what page-response.php is) and then it updates and the page is definitely under the theme that I want. I go to the database and I see a second entry for the page id in the wp_postmeta table with a new meta id. This happens with add_post_meta AND update_post_meta. I’m following wordpress documentation exactly, so I don’t know what it could be.

    Any help would be appreciated!

    sorry, for the

    global $wp_query;
    $wp_query->is_single = false;

    I was trying to use shawmutsteve’s suggestion, but I don’t really know where to put it or how to implement it. The same things happen wether that code is there or not.

Viewing 15 replies - 1 through 15 (of 18 total)
  • The topic ‘update_post_meta( $id, '_wp_page_template', '_myTemplate') working inconsistentl’ is closed to new replies.