• Resolved jimmyt1988

    (@jimmyt1988)


    3.1 add_meta_box() doesnt seem to be working?

    Am i doing something wrong?

    <?php
    add_action('admin_menu', 'my_post_options_box');
    
    function my_post_options_box() {
    add_meta_box('post_info', 'Post Information', 'custom_post_info', 'post', 'side', 'high');
    }
    
    //Adds the actual option box
    function custom_post_info() {
    global $post;
    ?>
    <fieldset id="mycustom-div">
    <div>
    <p>
    <label for="cpi_dropdown_options" >Dropdown Options:</label><br />
    <select name="cpi_dropdown_options" id="cpi_dropdown_options">
    <option<?php selected( get_post_meta($post->ID, 'cpi_dropdown_options', true), 'Option 1' ); ?>>Option 1</option>
    <option<?php selected( get_post_meta($post->ID, 'cpi_dropdown_options', true), 'Option 2' ); ?>>Option 2</option>
    <option<?php selected( get_post_meta($post->ID, 'cpi_dropdown_options', true), 'Option 3' ); ?>>Option 3</option>
    </select>
    <br />
    <br />
    <label for="cpi_text_option">Text Option:</label><br />
    <input type="text" name="cpi_text_option" id="cpi_text_option" value="<?php echo get_post_meta($post->ID, 'cpi_text_option', true); ?>">
    </p>
    </div>
    </fieldset>
    <?php
    }
    
    add_action('save_post', 'custom_add_save');
    function custom_add_save($postID){
    // called after a post or page is saved
    if($parent_id = wp_is_post_revision($postID))
    {
    $postID = $parent_id;
    }
    
    if ($_POST['cpi_dropdown_options']) {
    update_custom_meta($postID, $_POST['cpi_dropdown_options'], 'cpi_dropdown_options');
    }
    if ($_POST['cpi_text_option']) {
    update_custom_meta($postID, $_POST['cpi_text_option'], 'cpi_text_option');
    }
    }
    
    function update_custom_meta($postID, $newvalue, $field_name) {
    // To create new meta
    if(!get_post_meta($postID, $field_name)){
    add_post_meta($postID, $field_name, $newvalue);
    }else{
    // or to update existing meta
    update_post_meta($postID, $field_name, $newvalue);
    }
    }
    ?>
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘add_meta_box() not working?’ is closed to new replies.