Hello and thanks for the guidance!
Trying to load a custom .php file for only my home page (template is page-home.php) by using this code:
add_action('admin_init','load_home_meta');
function load_home_meta() {
global $pagenow, $post;
if ($pagenow=='edit.php' && '104' == $post->ID) {
include 'metaboxes/home-meta.php';
}
}
The page's unique ID is 104. I need this to work only on the home page since the nonce checks in the php file are breaking the ability to save other pages that don't use these custom meta fields.
Thanks!
Try Two:
Tried this:
add_action('admin_init','load_home_meta');
function load_home_meta() {
// Get the Post ID
if( isset( $_GET['post'] ) ) $post_id = $_GET['post'];
elseif( isset( $_POST['post_ID'] ) ) $post_id = $_POST['post_ID'];
if( !isset( $post_id ) ) return;
// Get the Page Template
$template_file = get_post_meta( $post_id, '_wp_page_template', true );
if($template_file == 'page-main-home.php'){
include '/metaboxes/home-main-home.php';
}
}
And seems to be partially working since I'm getting this error on the edit page in the admin section:
Warning: include(/metaboxes/home-main-home.php) [function.include]: failed to open stream: No such file or directory
Warning: include() [function.include]: Failed opening '/metaboxes/home-main-home.php' for inclusion (include_path='.:/usr/local/php-5.3.15/share/pear')
Why would the include work with the simple: include 'metaboxes/home-main-home.php';
?
I'm also tried the above code with a starting slash and without, both generating the missing/unable to find the file error.
Try Three:
Fixed my template typo error and am no longer not getting to the file with this:
add_action('admin_init','load_home_meta');
function load_home_meta() {
// Get the Post ID
if( isset( $_GET['post'] ) ) $post_id = $_GET['post'];
elseif( isset( $_POST['post_ID'] ) ) $post_id = $_POST['post_ID'];
if( !isset( $post_id ) ) return;
// Get the Page Template
$template_file = get_post_meta( $post_id, '_wp_page_template', true );
if($template_file == 'page-main-home.php'){
include (TEMPLATEPATH . '/metaboxes/home-meta.php');
}
}
But now in each meta-box I'm getting these errors:
Warning: Invalid argument supplied for foreach() in /nfs/c07/h04/mnt/111667/domains/staging.[site_name].org/html/wp-content/themes/[theme_name]/metaboxes/home-meta.php on line 184
But when I comment that out and just do a simple:
include 'metaboxes/home-meta.php';
The backend page renders fine... I'm lost as to what I'm doing incorrectly.
Try Four:
So, all the above still don't work.
Again, I'm trying to:
- Hide the editor on a specific page in the admin backend
- Include custom-fields using metaboxes for only the home (front) page.
- Only serve the home-meta.php file to the front page and no other pages/posts.
functions.php
:
/**
* Hide Editor
* @author Bill Erickson
* @link http://www.billerickson.net/code/hide-editor-on-specific-page-template/
*/
add_action( 'admin_init', 'hide_editor' );
function hide_editor() {
// Get the Post ID
if( isset( $_GET['post'] ) ) $post_id = $_GET['post'];
elseif( isset( $_POST['post_ID'] ) ) $post_id = $_POST['post_ID'];
if( !isset( $post_id ) ) return;
// Get the Page Template
$template_file = get_post_meta( $post_id, '_wp_page_template', true );
if($template_file == 'front-page.php'){
remove_post_type_support('page', 'editor');
// add custom .js to footer for uploading images
function custom_admin_js() {
$url = get_option('siteurl');
$url = get_bloginfo('template_directory') . '/library/js/libs/meta-content.js';
echo '"<script type="text/javascript" src="'. $url . '"></script>"';
}
add_action('admin_footer', 'custom_admin_js');
// include 'metaboxes/home-meta.php';
}
}
// Loading Home Page Meta Box Code from External .php file
add_action( 'add_meta_boxes_page','load_home_meta' );
function load_home_meta() {
global $post;
if ( $post->ID == get_option( 'page_on_front' ) ) {
include( get_template_directory_uri() . '/metaboxes/home-main-home.php' );
}
}
I changed my previous home template file to the WordPress recognizable front-page.php
since I'm trying to target the home (front) page.
home-meta.php
(located in metaboxes in my theme folder)
<?php
/**
* Home Page Custom Meta Content
*
**/
function add_home_meta_box() {
add_meta_box(
'home_meta_box', // $id
'Home Page Content', // $title
'show_home_meta_box', // $callback
'page', // $page
'normal', // $context
'high'); // $priority
add_meta_box(
'home_meta_box_lower_1', // $id
'Home Lower Left', // $title
'show_home_meta_box_lower_left', // $callback
'page', // $page
'normal', // $context
'high'); // $priority
add_meta_box(
'home_meta_box_lower_2', // $id
'Home Lower Center', // $title
'show_home_meta_box_lower_center', // $callback
'page', // $page
'normal', // $context
'high'); // $priority
add_meta_box(
'home_meta_box_lower_3', // $id
'Home Lower Right', // $title
'show_home_meta_box_lower_right', // $callback
'page', // $page
'normal', // $context
'high'); // $priority
}
add_action('add_meta_boxes', 'add_home_meta_box');
// Creating Array for Fields
$prefix = 'home_';
$home_meta_fields = array(
array(
'label' => 'Caption Title',
'desc' => 'Upper section H2 caption title.',
'id' => $prefix.'title',
'type' => 'text'
),
array(
'label' => 'Caption Sub Title',
'desc' => 'Upper section H3 caption title.',
'id' => $prefix.'sub_title',
'type' => 'text'
),
array(
'label'=> 'Caption',
'desc' => 'Caption text block.',
'id' => $prefix.'caption',
'type' => 'textarea'
),
array(
'label' => 'Caption Image',
'desc' => 'Upload a pre-cropped 1140px wide x 530px tall web-optimized image.',
'id' => $prefix.'image',
'type' => 'image'
)
);// end caption array
$prefix2 = 'home_lower_left_';
$home_meta_fields_lower_left = array(
array(
'label' => 'Column Title',
'desc' => 'H2 title for column.',
'id' => $prefix2.'title',
'type' => 'text'
),
array(
'label' => 'Column Image',
'desc' => 'Upload a pre-cropped 360px wide x 300px tall web-optimized image.',
'id' => $prefix2.'image',
'type' => 'image'
),
array(
'label' => 'Column Sub-Title',
'desc' => 'H3 sub-title above text block.',
'id' => $prefix2.'sub_title',
'type' => 'text'
),
array(
'label'=> 'Text block',
'desc' => 'Caption text block.',
'id' => $prefix2.'caption',
'type' => 'textarea'
),
array(
'label' => 'Button Label',
'desc' => 'Button link label (what user reads on button).',
'id' => $prefix2.'btn_label',
'type' => 'text'
),
array(
'label' => 'Button Title Tag',
'desc' => 'On hover and SEO text.',
'id' => $prefix2.'btn_title_tag',
'type' => 'text'
),
array(
'label' => 'Button URL',
'desc' => 'URL where button links to. Enter http:// to work.',
'id' => $prefix2.'btn_url',
'type' => 'text'
)
);// end lower left array
$prefix3 = 'home_lower_center_';
$home_meta_fields_lower_center = array(
array(
'label' => 'Column Title',
'desc' => 'H2 title for column.',
'id' => $prefix3.'title',
'type' => 'text'
),
array(
'label' => 'Column Image',
'desc' => 'Upload a pre-cropped 360px wide x 300px tall web-optimized image.',
'id' => $prefix3.'image',
'type' => 'image'
),
array(
'label' => 'Column Sub-Title',
'desc' => 'H3 sub-title above text block.',
'id' => $prefix3.'sub_title',
'type' => 'text'
),
array(
'label'=> 'Text block',
'desc' => 'Caption text block.',
'id' => $prefix3.'caption',
'type' => 'textarea'
),
array(
'label' => 'Button Label',
'desc' => 'Button link label (what user reads on button).',
'id' => $prefix3.'btn_label',
'type' => 'text'
),
array(
'label' => 'Button Title Tag',
'desc' => 'On hover and SEO text.',
'id' => $prefix3.'btn_title_tag',
'type' => 'text'
),
array(
'label' => 'Button URL',
'desc' => 'URL where button links to. Enter http:// to work.',
'id' => $prefix3.'btn_url',
'type' => 'text'
)
);// end lower center array
$prefix4 = 'home_lower_right_';
$home_meta_fields_lower_right = array(
array(
'label' => 'Column Title',
'desc' => 'H2 title for column.',
'id' => $prefix4.'title',
'type' => 'text'
),
array(
'label' => 'Column Image',
'desc' => 'Upload a pre-cropped 360px wide x 300px tall web-optimized image.',
'id' => $prefix4.'image',
'type' => 'image'
),
array(
'label' => 'Column Sub-Title',
'desc' => 'H3 sub-title above text block.',
'id' => $prefix4.'sub_title',
'type' => 'text'
),
array(
'label'=> 'Text block',
'desc' => 'Caption text block.',
'id' => $prefix4.'caption',
'type' => 'textarea'
),
array(
'label' => 'Button Label',
'desc' => 'Button link label (what user reads on button).',
'id' => $prefix4.'btn_label',
'type' => 'text'
),
array(
'label' => 'Button Title Tag',
'desc' => 'On hover and SEO text.',
'id' => $prefix4.'btn_title_tag',
'type' => 'text'
),
array(
'label' => 'Button URL',
'desc' => 'URL where button links to. Enter http:// to work.',
'id' => $prefix4.'btn_url',
'type' => 'text'
)
);// end lower right array
//The Callback
function show_home_meta_box() {
global $home_meta_fields, $post;
// Using nonce for verification
echo '<input type="hidden" name="home_meta_box_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />';
//Begin field table and loop
echo '<table class="form-table">';
foreach ($home_meta_fields as $field) {
// get value of this field if it exists for this page
$meta = get_post_meta($post->ID, $field['id'], true);
// begin a table row with
echo '<tr>
<th><label for="'.$field['id'].'">'.$field['label'].'</label></th>
<td>';
switch($field['type']) {
// case items will go here
// text
case 'text':
echo '<input type="text" name="'.$field['id'].'" id="'.$field['id'].'" value="'.$meta.'" size="30" />
<br /><span class="description">'.$field['desc'].'</span>';
break;
// textarea
case 'textarea':
echo '<textarea name="'.$field['id'].'" id="'.$field['id'].'" cols="60" rows="4">'.$meta.'</textarea>
<br /><span class="description">'.$field['desc'].'</span>';
break;
case 'image':
$image = get_template_directory_uri().'/library/images/img-preview-blank.png';
echo '<span class="custom_default_image" style="display:none">'.$image.'</span>';
if ($meta) { $image = wp_get_attachment_image_src($meta, 'medium'); $image = $image[0]; }
echo '<input name="'.$field['id'].'" type="hidden" class="custom_upload_image" value="'.$meta.'" />
<img src="'.$image.'" class="custom_preview_image" style="max-width:300px" alt="" /><br />
<input class="custom_upload_image_button button" type="button" value="Choose Image" />
<small> <a href="#" class="custom_clear_image_button">Remove Image</a></small>
<br clear="all" /><span class="description">'.$field['desc'].'';
break;
} // end switch
echo '</td></tr>';
} // end foreach
echo '</table>'; // end table
}
// Save the Data
function save_home_meta($post_id) {
global $home_meta_fields;
// verify nonce
if (!wp_verify_nonce($_POST['home_meta_box_nonce'], basename(__FILE__)))
return $post_id;
// check autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return $post_id;
// check permissions
if ('page' == $_POST['post_type']) {
if (!current_user_can('edit_page', $post_id))
return $post_id;
} elseif (!current_user_can('edit_post', $post_id)) {
return $post_id;
}
// loop through fields and save the data
foreach ($home_meta_fields as $field) {
$old = get_post_meta($post_id, $field['id'], true);
$new = $_POST[$field['id']];
if ($new && $new != $old) {
update_post_meta($post_id, $field['id'], $new);
} elseif ('' == $new && $old) {
delete_post_meta($post_id, $field['id'], $old);
}
} // end foreach
}
add_action('save_post', 'save_home_meta');
// end Upper Home Content
// Lower Left
//The Callback
function show_home_meta_box_lower_left() {
global $home_meta_fields_lower_left, $post;
// Using nonce for verification
echo '<input type="hidden" name="home_meta_box_left_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />';
//Begin field table and loop
echo '<table class="form-table">';
foreach ($home_meta_fields_lower_left as $field) {
// get value of this field if it exists for this page
$meta = get_post_meta($post->ID, $field['id'], true);
// begin a table row with
echo '<tr>
<th><label for="'.$field['id'].'">'.$field['label'].'</label></th>
<td>';
switch($field['type']) {
// case items will go here
// text
case 'text':
echo '<input type="text" name="'.$field['id'].'" id="'.$field['id'].'" value="'.$meta.'" size="30" />
<br /><span class="description">'.$field['desc'].'</span>';
break;
// textarea
case 'textarea':
echo '<textarea name="'.$field['id'].'" id="'.$field['id'].'" cols="60" rows="4">'.$meta.'</textarea>
<br /><span class="description">'.$field['desc'].'</span>';
break;
case 'image':
$image = get_template_directory_uri().'/library/images/img-preview-blank.png';
echo '<span class="custom_default_image" style="display:none">'.$image.'</span>';
if ($meta) { $image = wp_get_attachment_image_src($meta, 'medium'); $image = $image[0]; }
echo '<input name="'.$field['id'].'" type="hidden" class="custom_upload_image" value="'.$meta.'" />
<img src="'.$image.'" class="custom_preview_image" style="max-width:300px" alt="" /><br />
<input class="custom_upload_image_button button" type="button" value="Choose Image" />
<small> <a href="#" class="custom_clear_image_button">Remove Image</a></small>
<br clear="all" /><span class="description">'.$field['desc'].'';
break;
} // end switch
echo '</td></tr>';
} // end foreach
echo '</table>'; // end table
}
// Save the Data
function save_home_meta_lower_left($post_id) {
global $home_meta_fields_lower_left;
// verify nonce
if (!wp_verify_nonce($_POST['home_meta_box_left_nonce'], basename(__FILE__)))
return $post_id;
// check autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return $post_id;
// check permissions
if ('page' == $_POST['post_type']) {
if (!current_user_can('edit_page', $post_id))
return $post_id;
} elseif (!current_user_can('edit_post', $post_id)) {
return $post_id;
}
// loop through fields and save the data
foreach ($home_meta_fields_lower_left as $field) {
$old = get_post_meta($post_id, $field['id'], true);
$new = $_POST[$field['id']];
if ($new && $new != $old) {
update_post_meta($post_id, $field['id'], $new);
} elseif ('' == $new && $old) {
delete_post_meta($post_id, $field['id'], $old);
}
} // end foreach
}
add_action('save_post', 'save_home_meta_lower_left');
// end lower left
// Lower Center Column
//The Callback
function show_home_meta_box_lower_center() {
global $home_meta_fields_lower_center, $post;
// Using nonce for verification
echo '<input type="hidden" name="home_meta_box_center_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />';
//Begin field table and loop
echo '<table class="form-table">';
foreach ($home_meta_fields_lower_center as $field) {
// get value of this field if it exists for this page
$meta = get_post_meta($post->ID, $field['id'], true);
// begin a table row with
echo '<tr>
<th><label for="'.$field['id'].'">'.$field['label'].'</label></th>
<td>';
switch($field['type']) {
// case items will go here
// text
case 'text':
echo '<input type="text" name="'.$field['id'].'" id="'.$field['id'].'" value="'.$meta.'" size="30" />
<br /><span class="description">'.$field['desc'].'</span>';
break;
// textarea
case 'textarea':
echo '<textarea name="'.$field['id'].'" id="'.$field['id'].'" cols="60" rows="4">'.$meta.'</textarea>
<br /><span class="description">'.$field['desc'].'</span>';
break;
case 'image':
$image = get_template_directory_uri().'/library/images/img-preview-blank.png';
echo '<span class="custom_default_image" style="display:none">'.$image.'</span>';
if ($meta) { $image = wp_get_attachment_image_src($meta, 'medium'); $image = $image[0]; }
echo '<input name="'.$field['id'].'" type="hidden" class="custom_upload_image" value="'.$meta.'" />
<img src="'.$image.'" class="custom_preview_image" style="max-width:300px" alt="" /><br />
<input class="custom_upload_image_button button" type="button" value="Choose Image" />
<small> <a href="#" class="custom_clear_image_button">Remove Image</a></small>
<br clear="all" /><span class="description">'.$field['desc'].'';
break;
} // end switch
echo '</td></tr>';
} // end foreach
echo '</table>'; // end table
}
// Save the Data
function save_home_meta_lower_center($post_id) {
global $home_meta_fields_lower_center;
// verify nonce
if (!wp_verify_nonce($_POST['home_meta_box_center_nonce'], basename(__FILE__)))
return $post_id;
// check autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return $post_id;
// check permissions
if ('page' == $_POST['post_type']) {
if (!current_user_can('edit_page', $post_id))
return $post_id;
} elseif (!current_user_can('edit_post', $post_id)) {
return $post_id;
}
// loop through fields and save the data
foreach ($home_meta_fields_lower_center as $field) {
$old = get_post_meta($post_id, $field['id'], true);
$new = $_POST[$field['id']];
if ($new && $new != $old) {
update_post_meta($post_id, $field['id'], $new);
} elseif ('' == $new && $old) {
delete_post_meta($post_id, $field['id'], $old);
}
} // end foreach
}
add_action('save_post', 'save_home_meta_lower_center');
// end lower center
// Lower Right Column
//The Callback
function show_home_meta_box_lower_right() {
global $home_meta_fields_lower_right, $post;
// Using nonce for verification
echo '<input type="hidden" name="home_meta_box_right_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />';
//Begin field table and loop
echo '<table class="form-table">';
foreach ($home_meta_fields_lower_right as $field) {
// get value of this field if it exists for this page
$meta = get_post_meta($post->ID, $field['id'], true);
// begin a table row with
echo '<tr>
<th><label for="'.$field['id'].'">'.$field['label'].'</label></th>
<td>';
switch($field['type']) {
// case items will go here
// text
case 'text':
echo '<input type="text" name="'.$field['id'].'" id="'.$field['id'].'" value="'.$meta.'" size="30" />
<br /><span class="description">'.$field['desc'].'</span>';
break;
// textarea
case 'textarea':
echo '<textarea name="'.$field['id'].'" id="'.$field['id'].'" cols="60" rows="4">'.$meta.'</textarea>
<br /><span class="description">'.$field['desc'].'</span>';
break;
case 'image':
$image = get_template_directory_uri().'/library/images/img-preview-blank.png';
echo '<span class="custom_default_image" style="display:none">'.$image.'</span>';
if ($meta) { $image = wp_get_attachment_image_src($meta, 'medium'); $image = $image[0]; }
echo '<input name="'.$field['id'].'" type="hidden" class="custom_upload_image" value="'.$meta.'" />
<img src="'.$image.'" class="custom_preview_image" style="max-width:300px" alt="" /><br />
<input class="custom_upload_image_button button" type="button" value="Choose Image" />
<small> <a href="#" class="custom_clear_image_button">Remove Image</a></small>
<br clear="all" /><span class="description">'.$field['desc'].'';
break;
} // end switch
echo '</td></tr>';
} // end foreach
echo '</table>'; // end table
}
// Save the Data
function save_home_meta_lower_right($post_id) {
global $home_meta_fields_lower_right;
// verify nonce
if (!wp_verify_nonce($_POST['home_meta_box_right_nonce'], basename(__FILE__)))
return $post_id;
// check autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return $post_id;
// check permissions
if ('page' == $_POST['post_type']) {
if (!current_user_can('edit_page', $post_id))
return $post_id;
} elseif (!current_user_can('edit_post', $post_id)) {
return $post_id;
}
// loop through fields and save the data
foreach ($home_meta_fields_lower_right as $field) {
$old = get_post_meta($post_id, $field['id'], true);
$new = $_POST[$field['id']];
if ($new && $new != $old) {
update_post_meta($post_id, $field['id'], $new);
} elseif ('' == $new && $old) {
delete_post_meta($post_id, $field['id'], $old);
}
} // end foreach
}
add_action('save_post', 'save_home_meta_lower_right');
// end lower right
?>
When I save all that, I get the following errors in the admin backend:
Warning: include(): http:// wrapper is disabled in the server configuration by allow_url_include=0 in /Users/[my_mac_user]/Sites/[dev_directory]/wp-content/themes/[site_theme]/functions.php on line 354
Warning: include(http://[site_url]/wp-content/themes/[site_theme]/metaboxes/home-main-home.php): failed to open stream: no suitable wrapper could be found in /Users/[my_mac_user]/Sites/[dev_directory]/wp-content/themes/[site_theme]/functions.php on line 354
Warning: include(): Failed opening 'http://[site_url]/wp-content/themes/[site_theme]/metaboxes/home-main-home.php' for inclusion (include_path='.:/Applications/MAMP/bin/php/php5.4.4/lib/php') in /Users/[my_mac_user]/Sites/[dev_directory]/wp-content/themes/[site_theme]/functions.php on line 354
Also, I'm now no longer hiding the content box with the template name change to front-page.php
. Tried using the global object of $post to get page_on_front
too, but that doesn't work, either.
Error:
Notice: Trying to get property of non-object in /Users/amores/Sites/[dev_directory]/wp-content/themes/[site_theme/functions.php on line 328
Additional code failed attempt:
add_action( 'admin_init', 'hide_editor' );
function hide_editor() {
// Get the Post ID
global $post;
if ( $post->ID == get_option( 'page_on_front' ) ) {
remove_post_type_support('page', 'editor');
// add custom .js to footer for uploading images
function custom_admin_js() {
$url = get_option('siteurl');
$url = get_bloginfo('template_directory') . '/library/js/libs/meta-content.js';
echo '"<script type="text/javascript" src="'. $url . '"></script>"';
}
add_action('admin_footer', 'custom_admin_js');
// include 'metaboxes/home-meta.php';
}
}
Thanks!
page-home.php
. Is this template for your site front page, or for your blog posts index?