Page templates.
git-svn-id: http://svn.automattic.com/wordpress/trunk@1753 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
96a0b41202
commit
543714fbab
|
@ -755,6 +755,37 @@ function validate_current_theme() {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function get_page_templates() {
|
||||||
|
$themes = get_themes();
|
||||||
|
$theme = get_current_theme();
|
||||||
|
$templates = $themes[$theme]['Template Files'];
|
||||||
|
$page_templates = array();
|
||||||
|
|
||||||
|
foreach ($templates as $template) {
|
||||||
|
$template_data = implode('', file(ABSPATH . $template));
|
||||||
|
preg_match("|Template Name:(.*)|i", $template_data, $name);
|
||||||
|
preg_match("|Description:(.*)|i", $template_data, $description);
|
||||||
|
|
||||||
|
$name = $name[1];
|
||||||
|
$description = $description[1];
|
||||||
|
|
||||||
|
if (! empty($name)) {
|
||||||
|
$page_templates[trim($name)] = basename($template);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $page_templates;
|
||||||
|
}
|
||||||
|
|
||||||
|
function page_template_dropdown($default = '') {
|
||||||
|
$templates = get_page_templates();
|
||||||
|
foreach (array_keys($templates) as $template) :
|
||||||
|
if ($default == $templates[$template]) $selected = " selected='selected'";
|
||||||
|
else $selected = '';
|
||||||
|
echo "\n\t<option value='" . $templates[$template] . "' $selected>$template</option>";
|
||||||
|
endforeach;
|
||||||
|
}
|
||||||
|
|
||||||
function parent_dropdown($default = 0, $parent = 0, $level = 0) {
|
function parent_dropdown($default = 0, $parent = 0, $level = 0) {
|
||||||
global $wpdb;
|
global $wpdb;
|
||||||
$items = $wpdb->get_results("SELECT ID, post_parent, post_title FROM $wpdb->posts WHERE post_parent = $parent AND post_status = 'static' ORDER BY menu_order");
|
$items = $wpdb->get_results("SELECT ID, post_parent, post_title FROM $wpdb->posts WHERE post_parent = $parent AND post_status = 'static' ORDER BY menu_order");
|
||||||
|
|
|
@ -49,9 +49,9 @@ window.onload = focusit;
|
||||||
<div><input name="post_password" type="text" size="13" id="post_password" value="<?php echo $post_password ?>" /></div>
|
<div><input name="post_password" type="text" size="13" id="post_password" value="<?php echo $post_password ?>" /></div>
|
||||||
</fieldset>
|
</fieldset>
|
||||||
<fieldset id="pageparent">
|
<fieldset id="pageparent">
|
||||||
<legend><?php _e('Page Parent') ?></a></legend>
|
<legend><?php _e('Page Parent') ?></legend>
|
||||||
<div><select name="parent_id">
|
<div><select name="parent_id">
|
||||||
<option value='0'>Main Page (no parent)</option>
|
<option value='0'><?php _e('Main Page (no parent)'); ?></option>
|
||||||
<?php parent_dropdown($post_parent); ?>
|
<?php parent_dropdown($post_parent); ?>
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
|
@ -79,6 +79,22 @@ edCanvas = document.getElementById('content');
|
||||||
<input name="savepage" type="submit" id="savepage" tabindex="6" value="<?php _e('Create New Page') ?> »" />
|
<input name="savepage" type="submit" id="savepage" tabindex="6" value="<?php _e('Create New Page') ?> »" />
|
||||||
<input name="referredby" type="hidden" id="referredby" value="<?php if (isset($_SERVER['HTTP_REFERER'])) echo htmlspecialchars($_SERVER['HTTP_REFERER']); ?>" />
|
<input name="referredby" type="hidden" id="referredby" value="<?php if (isset($_SERVER['HTTP_REFERER'])) echo htmlspecialchars($_SERVER['HTTP_REFERER']); ?>" />
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
|
<fieldset id="pageoptions">
|
||||||
|
<legend><?php _e('Page Options') ?></legend>
|
||||||
|
<table width="100%" cellspacing="2" cellpadding="5" class="editform">
|
||||||
|
<tr valign="top">
|
||||||
|
<th scope="row"><?php _e('Page Template:') ?></th>
|
||||||
|
<td><div><select name="page_template">
|
||||||
|
<option value='default'><?php _e('Default Template'); ?></option>
|
||||||
|
<?php page_template_dropdown($page_template); ?>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
</table>
|
||||||
|
</fieldset>
|
||||||
|
|
||||||
<?php do_action('edit_page_form', ''); ?>
|
<?php do_action('edit_page_form', ''); ?>
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
|
|
|
@ -68,6 +68,7 @@ if ($user_level > 0) {
|
||||||
$ping_status = get_settings('default_ping_status');
|
$ping_status = get_settings('default_ping_status');
|
||||||
$post_pingback = get_settings('default_pingback_flag');
|
$post_pingback = get_settings('default_pingback_flag');
|
||||||
$post_parent = 0;
|
$post_parent = 0;
|
||||||
|
$page_template = 'default';
|
||||||
|
|
||||||
include('edit-page-form.php');
|
include('edit-page-form.php');
|
||||||
}
|
}
|
||||||
|
|
|
@ -200,6 +200,8 @@ case 'post':
|
||||||
|
|
||||||
if ($post_status = 'static') {
|
if ($post_status = 'static') {
|
||||||
generate_page_rewrite_rules();
|
generate_page_rewrite_rules();
|
||||||
|
|
||||||
|
add_post_meta($post_ID, '_wp_page_template', $_POST['page_template'], true);
|
||||||
}
|
}
|
||||||
|
|
||||||
exit();
|
exit();
|
||||||
|
@ -233,6 +235,8 @@ case 'edit':
|
||||||
$post_parent = $postdata->post_parent;
|
$post_parent = $postdata->post_parent;
|
||||||
|
|
||||||
if ($post_status == 'static') {
|
if ($post_status == 'static') {
|
||||||
|
$page_template = get_post_meta($post_ID, '_wp_page_template', true);
|
||||||
|
|
||||||
include('edit-page-form.php');
|
include('edit-page-form.php');
|
||||||
} else {
|
} else {
|
||||||
include('edit-form-advanced.php');
|
include('edit-form-advanced.php');
|
||||||
|
@ -438,6 +442,10 @@ $now_gmt = current_time('mysql', 1);
|
||||||
|
|
||||||
if ($post_status = 'static') {
|
if ($post_status = 'static') {
|
||||||
generate_page_rewrite_rules();
|
generate_page_rewrite_rules();
|
||||||
|
|
||||||
|
if ( ! update_post_meta($post_ID, '_wp_page_template', $_POST['page_template'])) {
|
||||||
|
add_post_meta($post_ID, '_wp_page_template', $_POST['page_template'], true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
do_action('edit_post', $post_ID);
|
do_action('edit_post', $post_ID);
|
||||||
|
|
|
@ -215,10 +215,9 @@ if ($pagenow == 'index.php') {
|
||||||
$wp_did_template_redirect = true;
|
$wp_did_template_redirect = true;
|
||||||
include("$wp_template_dir/single.php");
|
include("$wp_template_dir/single.php");
|
||||||
exit;
|
exit;
|
||||||
} else if (is_page() &&
|
} else if (is_page() && file_exists(get_page_template())) {
|
||||||
file_exists("$wp_template_dir/page.php")) {
|
|
||||||
$wp_did_template_redirect = true;
|
$wp_did_template_redirect = true;
|
||||||
include("$wp_template_dir/page.php");
|
include(get_page_template());
|
||||||
exit;
|
exit;
|
||||||
} else if (is_category() &&
|
} else if (is_category() &&
|
||||||
file_exists("$wp_template_dir/category.php")) {
|
file_exists("$wp_template_dir/category.php")) {
|
||||||
|
|
|
@ -447,12 +447,16 @@ AND meta_key = '$key' AND meta_value = '$value'");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
function get_post_meta($post_id, $key) {
|
function get_post_meta($post_id, $key, $single = false) {
|
||||||
global $wpdb, $post_meta_cache;
|
global $wpdb, $post_meta_cache;
|
||||||
|
|
||||||
if (isset($post_meta_cache[$post_id][$key])) {
|
if (isset($post_meta_cache[$post_id][$key])) {
|
||||||
|
if ($single) {
|
||||||
|
return $post_meta_cache[$post_id][$key][0];
|
||||||
|
} else {
|
||||||
return $post_meta_cache[$post_id][$key];
|
return $post_meta_cache[$post_id][$key];
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$metalist = $wpdb->get_results("SELECT meta_value FROM $wpdb->postmeta WHERE post_id = '$post_id' AND meta_key = '$key'", ARRAY_N);
|
$metalist = $wpdb->get_results("SELECT meta_value FROM $wpdb->postmeta WHERE post_id = '$post_id' AND meta_key = '$key'", ARRAY_N);
|
||||||
|
|
||||||
|
@ -463,12 +467,25 @@ function get_post_meta($post_id, $key) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($single) {
|
||||||
|
if (count($values)) {
|
||||||
|
return $values[0];
|
||||||
|
} else {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
} else {
|
||||||
return $values;
|
return $values;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function update_post_meta($post_id, $key, $value, $prev_value = '') {
|
function update_post_meta($post_id, $key, $value, $prev_value = '') {
|
||||||
global $wpdb, $post_meta_cache;
|
global $wpdb, $post_meta_cache;
|
||||||
|
|
||||||
|
if(! $wpdb->get_var("SELECT meta_key FROM $wpdb->postmeta WHERE meta_key
|
||||||
|
= '$key' AND post_id = '$post_id'") ) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
if (empty($prev_value)) {
|
if (empty($prev_value)) {
|
||||||
$wpdb->query("UPDATE $wpdb->postmeta SET meta_value = '$value' WHERE
|
$wpdb->query("UPDATE $wpdb->postmeta SET meta_value = '$value' WHERE
|
||||||
meta_key = '$key' AND post_id = '$post_id'");
|
meta_key = '$key' AND post_id = '$post_id'");
|
||||||
|
@ -1820,6 +1837,26 @@ function get_template_directory() {
|
||||||
return $template;
|
return $template;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function get_page_template() {
|
||||||
|
global $wp_query;
|
||||||
|
|
||||||
|
$id = $wp_query->post->ID;
|
||||||
|
$template_dir = get_template_directory();
|
||||||
|
$default = "$template_dir/page.php";
|
||||||
|
|
||||||
|
$template = get_post_meta($id, '_wp_page_template', true);
|
||||||
|
|
||||||
|
if (empty($template) || ($template == 'default')) {
|
||||||
|
return $default;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (file_exists("$template_dir/$template")) {
|
||||||
|
return "$template_dir/$template";
|
||||||
|
}
|
||||||
|
|
||||||
|
return $default;
|
||||||
|
}
|
||||||
|
|
||||||
// Borrowed from the PHP Manual user notes. Convert entities, while
|
// Borrowed from the PHP Manual user notes. Convert entities, while
|
||||||
// preserving already-encoded entities:
|
// preserving already-encoded entities:
|
||||||
function htmlentities2($myHTML) {
|
function htmlentities2($myHTML) {
|
||||||
|
|
Loading…
Reference in New Issue