• I am writing a plugin that creates some pages. I have the pages created and the content of the pages set correctly. Now I want to give the admins the capability of setting the template for the created pages. First, I need to get a list of the templates that are included with the current theme. I know this is done on the Edit Page page. After that, I need to set the template on the pages I created.

    Any help??

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter lcwakeman

    (@lcwakeman)

    To assign a template to a post, I need to add (edit if they already exist) two entries to the postmeata table, meta_key = _edit_last, meta_value = user id and meta_key = _wp_page_template and meta_value = the filename of the template.

    As for getting a list of the templates, I think I am stuck with poking through the php files in the template directory looking for template files.

    Any other ideas?

    Thread Starter lcwakeman

    (@lcwakeman)

    The following will get a list of available templates:

    // get a list of templates
    	        $templates = array('0' => 'default');
    	        $dh = opendir(get_template_directory());
                while ($file = readdir($dh)) {
                    if (strpos(strtoupper($file), 'PHP')) {
                        $contents = file_get_contents(get_template_directory().'/'.$file);
                        if ($location = strpos(strtoupper($contents),'TEMPLATE NAME:')) {
                            $temp = explode("\n", substr($contents, $location));
                            $temp = explode(':', $temp['0']);
                            $temp = explode(' ', trim($temp['1']));
                            $templates[$file] = $temp['0'];
                        }
                    }
                }
    	        closedir($dh);
    Thread Starter lcwakeman

    (@lcwakeman)

    I made a slight change to the code in the previous replu as it wouldn’t deal well with spaces in template names:

    [Code moderated as per the Forum Rules. Please use the pastebin]

    I did the following to update the template for a page(The $page array contains a description of the page being modified. It is an array that can be used to insert the page in the posts table:

    [Code moderated as per the Forum Rules. Please use the pastebin]

    An entry for the $page array is as follows:

    'home-screen' => array(
        		'post_title' => 'PM Home Screen',
                'post_type' 	=>	'page',
        		'post_name' => 'home-screen',
        		'comment_status'=>	'closed',
        		'ping_status' 	=>	'closed',
        		'post_content' => '[tspm_homescreen]',
        		'post_status' 	=>	'publish',
        		'post_author' 	=>	1,
        		'menu_order'	=>	0
        	    ),

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Need to get a list of tehe templates’ is closed to new replies.