I am trying to create a meta box with select options for each page with home-page-template.php assigned to it. I have got that functionality working correctly.
I also want it above the editor, below the page title, which was working, but NOW IS NOT. I have constantly modified the code trying to get this to work. Not sure what caused it to not be available above the editor. It used to be 'advanced' instead of 'up', neither of which work now.
My biggest problem is that on page update the page is not saving/storing the selection made. What I am wanting to be able to do is get the page ID of the option selected and use it in a variable on page.php. The page ID is being assigned to the "value" for each option. That's how I plan on or assume I will be getting the page id. The page id then needs to pass to a variable which I will use to get advanced custom fields values on the page.php.
Here is the code for the functions.php:
/* ** START ** ADDS A NEW META BOX TO PAGES */
// register the meta box
add_action( 'add_meta_boxes', 'my_custom_field_cityselect' );
add_action( 'load-page.php', 'my_custom_field_cityselect' );
add_action( 'load-page-new.php', 'my_custom_field_cityselect' );
function my_custom_field_cityselect() {
add_meta_box(
'my_select_meta_box_id', // this is HTML id of the box on edit screen
'Select The City For This Page', // title of the box
'my_customfield_box_content', // function to be called to display the checkboxes, see the function below
'page', // on which edit screen the box should appear
'up', // part of page where the box should appear
'high' // priority of the box
);
}
// Move all "up" metaboxes above the default editor
function my_upper_meta_box() { global $post, $wp_meta_boxes;
// Get the globals
do_meta_boxes( get_current_screen(), 'up', $post );
// Output the "up" meta boxes
} add_action( 'edit_form_after_title', 'my_upper_meta_box' );
// display the metabox
function my_customfield_box_content($post) {
// nonce field for security check, you can have the same
// nonce field for all your meta boxes of same plugin
wp_nonce_field( basename( __FILE__ ), 'my_custom_field_select_nonce' );
$meta_element_class = get_post_meta($post->ID, 'my_customfield_box_content', true);
$args = array(
'post_type' => 'page',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => '_wp_page_template',
'value' => 'page-home-template.php'
)
)
);
$the_pages = new WP_Query( $args );
echo '<select style=\'width:300px;\' name=\'my_metabox_location\'>';
echo '<option style=\'color:red\' value=\'0\'>Choose Appropriate Location</option>';
if( $the_pages->have_posts() ){
while( $the_pages->have_posts() ){
$the_pages->the_post();
$mymbpostid = get_the_ID();
$mymbposttitle = str_replace("Weight Loss ","",get_the_title());
$mymbposttitle = str_replace("Home","(HOME PAGE) - DO NOT USE THIS OPTION",get_the_title());
echo '<option style=\'color:green\' value=\''. $mymbpostid .'\''. selected( $meta_element_class, '$mymbpostid' ) .'>'. $mymbposttitle .'</option>';
}
}
wp_reset_postdata();
echo '</select>';
}
// save data from checkboxes
add_action( 'save_post', 'my_custom_field_data' );
function my_custom_field_data($post_id) {
// check if this isn't an auto save
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
// security check
if ( !wp_verify_nonce( $_POST['mypluing_nonce'], plugin_basename( __FILE__ ) ) )
return;
// further checks if you like,
// for example particular user, role or maybe post type in case of custom post types
// now store data in custom fields based on checkboxes selected
if ( isset( $_POST['my_metabox_location'] ) )
{
$meta_box_dropdown_value = $_POST["my_metabox_location"];
}
update_post_meta( $post->ID, 'my_customfield_box_content', $_POST['meta_box_dropdown_value'] );
}
// Removes the Meta Box from the Home Page Template
add_action('admin_head','my_meta_init');
function my_meta_init(){
$template_file = get_post_meta(get_the_ID(), '_wp_page_template', TRUE);
if (($template_file == 'page-home-template.php')) {
remove_meta_box('my_select_meta_box_id','page','advanced');
}
}
// Add styles for meta box
add_action('admin_head', 'custom_admin_meta_css');
function custom_admin_meta_css() {
echo '<style>#my_select_meta_box_id h2 {color:red}</style>';
}
/* ** END ** ADDS A NEW META BOX TO PAGES */
And here is the code for the page.php (not sure if the second line is doing anything or needs to be there at all):
<?php
$loc_phone = get_field('location_phone_number', $mymbpostid);
$loc_info = get_post_meta( $post->ID,'my_custom_field_checkboxes',true );
echo '<h2>Page ID - '. $loc_info .' | Page Phone - '. $loc_phone .'</h2>';
?>
I apologize if this is a hot jumbled mess as I have grabbed pieces from many posts and slapped this together. I feel like I'm really close, but just can't get it to save. Also not sure if this is set to save uniquely for each page individually, as it does need to.
ANY HELP IS GREATLY APPRECIATED. Correct code examples are the best answers for me as my PHP and WordPress core knowledge is dangerously limited. Thank you in advance!