Site icon Hip-Hop Website Design and Development

Customized Meta Field (SELECT) Not Saving

I’m making an attempt to create a meta field with choose choices for every web page with home-page-template.php assigned to it. I’ve acquired that performance working accurately.

I additionally need it above the editor, under the web page title, which was working, however NOW IS NOT. I’ve continually modified the code making an attempt to get this to work. Undecided what triggered it to not be accessible above the editor. It was once ‘superior’ as a substitute of ‘up’, neither of which work now.

My greatest drawback is that on web page replace the web page will not be saving/storing the choice made. What I’m wanting to have the ability to do is get the web page ID of the choice chosen and use it in a variable on web page.php. The web page ID is being assigned to the “worth” for every choice. That is how I plan on or assume I will likely be getting the web page id. The web page id then must cross to a variable which I’ll use to get superior customized fields values on the web page.php.

Right here is the code for the capabilities.php:

/*   ** START ** ADDS A NEW META BOX TO PAGES   */
// register the meta field
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' );
perform my_custom_field_cityselect() {
    add_meta_box(
        'my_select_meta_box_id',          // that is HTML id of the field on edit display screen
        'Choose The Metropolis For This Web page',    // title of the field
        'my_customfield_box_content',   // perform to be referred to as to show the checkboxes, see the perform under
        'web page',        // on which edit display screen the field ought to seem
        'up',      // a part of web page the place the field ought to seem
        'excessive'      // precedence of the field
    );
}

// Transfer all "up" metaboxes above the default editor
perform my_upper_meta_box() {     international $put up, $wp_meta_boxes;
// Get the globals
do_meta_boxes( get_current_screen(), 'up', $put up );
// Output the "up" meta containers 

} add_action( 'edit_form_after_title', 'my_upper_meta_box' );

// show the metabox
perform my_customfield_box_content($put up) {
    // nonce discipline for safety examine, you'll be able to have the identical
    // nonce discipline for all of your meta containers of identical 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' => 'web page',
        'posts_per_page' => -1,
        'meta_query' => array(
            array(
                'key' => '_wp_page_template',
                'worth' => 'page-home-template.php'
            )
        )
    );
    $the_pages = new WP_Query( $args );
    echo '<choose model='width:300px;' identify='my_metabox_location'>';
    echo '<choice model='coloration:pink' worth='0'>Select Applicable Location</choice>';
    if( $the_pages->have_posts() ){
        whereas( $the_pages->have_posts() ){
            $the_pages->the_post();
            $mymbpostid = get_the_ID();
            $mymbposttitle = str_replace("Weight Loss ","",get_the_title());
            $mymbposttitle = str_replace("Dwelling","(HOME PAGE) - DO NOT USE THIS OPTION",get_the_title());
            echo '<choice model='coloration:inexperienced' worth=''. $mymbpostid .'''. chosen( $meta_element_class, '$mymbpostid' ) .'>'. $mymbposttitle .'</choice>';
        }
    }
    wp_reset_postdata();

    echo '</choose>';
}

// save knowledge from checkboxes
add_action( 'save_post', 'my_custom_field_data' );
perform my_custom_field_data($post_id) {

    // examine if this is not an auto save
    if ( outlined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
        return;

    // safety examine
    if ( !wp_verify_nonce( $_POST['mypluing_nonce'], plugin_basename( __FILE__ ) ) )
        return;

    // additional checks in case you like, 
    // for instance explicit consumer, function or perhaps put up sort in case of customized put up varieties

    // now retailer knowledge in customized fields primarily based on checkboxes chosen
    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 Field from the Dwelling Web page Template
add_action('admin_head','my_meta_init');
perform 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','web page','superior');    
    }
}

// Add types for meta field
add_action('admin_head', 'custom_admin_meta_css');          
perform custom_admin_meta_css() {
  echo '<model>#my_select_meta_box_id h2 {coloration:pink}</model>';
}
/*   ** END ** ADDS A NEW META BOX TO PAGES   */

And right here is the code for the web page.php (unsure if the second line is doing something or must be there in any respect):

<?php 
$loc_phone = get_field('location_phone_number', $mymbpostid);
$loc_info = get_post_meta( $post->ID,'my_custom_field_checkboxes',true );
echo '<h2>Web page ID - '. $loc_info .' | Web page Telephone - '. $loc_phone .'</h2>';
?>

I apologize if it is a scorching jumbled mess as I’ve grabbed items from many posts and slapped this collectively. I really feel like I am actually shut, however simply cannot get it to save lots of. Additionally unsure if that is set to save lots of uniquely for every web page individually, because it does have to.

ANY HELP IS GREATLY APPRECIATED. Right code examples are the perfect solutions for me as my PHP and WordPress core information is dangerously restricted. Thanks prematurely!