metabox is not displaying correctly in wordpress plugin

i’am try to write own plugin but every time i got error or metabox is not show correctly.

this is my plugin code
see the code it displays on top of header. i want to display it below the ‘editor’ box
i tired but not show correctly.

<?php
/* 
    Plugin Name: party event
    Plugin URI: http://careertracker.net
    Version: 1.0
    Author: Savan Paun 
    Description: event sample plugin
*/
// Registers the new post type and taxonomy
function wpt_event_posttype() {
    register_post_type( 'events',
        array(
            'labels' => array(
                'name' => __( 'Events' ),
                'singular_name' => __( 'Event' ),
                'add_new' => __( 'Add New Event' ),
                'add_new_item' => __( 'Add New Event' ),
                'edit_item' => __( 'Edit Event' ),
                'new_item' => __( 'Add New Event' ),
                'view_item' => __( 'View Event' ),
                'search_items' => __( 'Search Event' ),
                'not_found' => __( 'No events found' ),
                'not_found_in_trash' => __( 'No events found in trash' )
            ),
            'public' => true,
            'supports' => array( 'title', 'editor', 'thumbnail', 'comments' ),
            'capability_type' => 'post',
            'rewrite' => array("slug" => "events"), // Permalinks format
            'menu_position' => 5,
            'register_meta_box_cb' => 'add_events_metaboxes'
        )
    );
}
add_action( 'init', 'wpt_event_posttype' );

function add_events_metaboxes()
{
    ?>
    <table width=450 border=1>
    <tr>
    <td width=139>Foods</td>
    <td width=295>
    <label for=select></label>
    <select name=food id=food>
      <option value="Pizza">Pizza</option>
      <option value="Hotdog">Hotdog</option>
    </select>
    </td>
    </tr>
    <tr>
    <td>Tea</td>
    <td><select name=tez id=tea>
      <option value="Yes">Yes</option>
      <option value="No">No</option>
    </select></td>
    </tr>
    <tr>
    <td>Person</td>
    <td><label for=textfield></label>
    <input type=text name=textfield id=textfield /></td>
    </tr>
    </table>
    <?php 
}
?>

Related posts

Leave a Reply

2 comments

  1. See full working code below:

    <?php 
    /* 
        Plugin Name: party event
        Plugin URI: http://careertracker.net
        Version: 1.0
        Author: Savan Paun 
        Description: event sample plugin
    */
    // Registers the new post type and taxonomy
    function wpt_event_posttype() {
        register_post_type( 'events',
            array(
                'labels' => array(
                    'name' => __( 'Events' ),
                    'singular_name' => __( 'Event' ),
                    'add_new' => __( 'Add New Event' ),
                    'add_new_item' => __( 'Add New Event' ),
                    'edit_item' => __( 'Edit Event' ),
                    'new_item' => __( 'Add New Event' ),
                    'view_item' => __( 'View Event' ),
                    'search_items' => __( 'Search Event' ),
                    'not_found' => __( 'No events found' ),
                    'not_found_in_trash' => __( 'No events found in trash' )
                ),
                'public' => true,
                'supports' => array( 'title', 'editor', 'thumbnail', 'comments' ),
                'capability_type' => 'post',
                'rewrite' => array("slug" => "events"), // Permalinks format
                'menu_position' => 5,
    
            )
        );
    }
    add_action( 'init', 'wpt_event_posttype' );
    
    add_action( 'admin_init', 'events_init' );
    add_action( 'save_post', 'event_meta_box_save' );
    
    function events_init() {
    
        add_meta_box("events-information", "Events Information", "events_meta_options", "events", "normal", "high");      
    }
    
    function events_meta_options( $post ) {
    
        $values = get_post_custom( $post->ID );
    
        $person     =   isset( $values['person'] ) ? esc_attr( $values['person'][0] ) : ''; 
    
        $foods      =   isset ( $values['foods'] ) ? esc_attr( $values['foods'][0] ) : '';
    
        $tea        =   isset ( $values['tea'] ) ? esc_attr( $values['tea'][0] ) : '';
    
        wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );
    
        ?>
        <table width="100%" border="0" class="options" cellspacing="5" cellpadding="5">
    
            <tr>
                <td width="1%">
                    <label for="foods"><?php _e('Foods', 'Events'); ?></label>
                </td>
                <td width="10%">
                   <select name='foods' id='foods'>
                    <option value="Pizza" <?php if ($foods) selected( $foods, 'Pizza' ); ?>><?php _e( 'Pizza', 'Events' )?></option>
                    <option value="Hotdog" <?php if ($foods) selected( $foods, 'Hotdog' ); ?>><?php _e( 'Hotdog', 'Events' )?></option>
                   </select>
                </td>          
            </tr> 
    
            <tr>
                <td width="1%">
                    <label for="tea"><?php _e('Tea', 'Events'); ?></label>
                </td>
                <td width="10%">
                    <select name='tea' id='tea'>
                        <option value="Yes" <?php if ($tea) selected( $tea, 'Yes' ); ?>><?php _e( 'Yes', 'Events' )?></option>
                        <option value="No" <?php if ($tea) selected( $tea, 'No' ); ?>><?php _e( 'No', 'Events' )?></option>
                    </select>              
                </td>          
            </tr>
    
            <tr>
                <td width="1%">
                    <label for="person"><?php _e('Person', 'Events'); ?></label>
                </td>
                <td width="10%">
                    <input type="text" id="person" name="person" value="<?php echo $person; ?>" placeholder="<?php _e('Enter person name', 'Events'); ?>" style="width:90%; padding: 5px 10px; line-height: 20px;"/>
                </td>          
            </tr>  
    
    
        </table>   
        <?php   
    }
    
    
    function event_meta_box_save( $post_id )
    {
        global $post;  
    
        $custom_meta_fields = array( 'person', 'foods', 'tea');
    
        // Bail if we're doing an auto save
        if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
    
        // if our nonce isn't there, or we can't verify it, bail
        if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;
    
        // if our current user can't edit this post, bail
        if( !current_user_can( 'edit_post' ) ) return;
    
        // now we can actually save the data
        $allowed = array( 
                    'a' => array( 
                            'href' => array(),
                            'title' => array()
                        ),
                    'br' => array(),
                   'em' => array(),
                   'strong' => array(),
                   'p' => array(),
                   'span' => array(),
                   'div' => array(),
                );    
    
        foreach( $custom_meta_fields as $custom_meta_field ){
    
            if( isset( $_POST[$custom_meta_field] ) )           
    
                update_post_meta($post->ID, $custom_meta_field, wp_kses( $_POST[$custom_meta_field], $allowed) );      
        }
    
    
    }
    
  2. The problem is that your register_meta_box_cb callback function should be like:

    function add_events_metaboxes()
    {
        add_meta_box( 
                    'box_id', 
                    'box_title', 
                    'print_mb_so_23236266', 
                    'events', 
                    'normal', 
                    'high'
            );
    }
    

    And then the function print_meta_box() with the HTML output:

    function print_mb_so_23236266() {
        ?>
        <table width="450" border="1">
        <!-- ETC -->
        <?php
    }
    

    But maybe you’re looking for something like this (without a meta box and getting rid of register_meta_box_cb):

    add_action( 'edit_form_after_editor', 'print_mb_so_23236266' );
    
    function print_mb_so_23236266( $post ) 
    {
        if( 'events' !== $post->post_type )
            return;
    
        ?>
        <table width="450" border="1">
        <tr>
        <td width="139">Foods</td>
        <td width="295">
        <label for="select"></label>
        <select name="food" id="food">
          <option value="Pizza">Pizza</option>
          <option value="Hotdog">Hotdog</option>
        </select>
        </td>
        </tr>
        <tr>
        <td>Tea</td>
        <td><select name="tez" id="tea">
          <option value="Yes">Yes</option>
          <option value="No">No</option>
        </select></td>
        </tr>
        <tr>
        <td>Person</td>
        <td><label for="textfield"></label>
        <input type="text" name="textfield" id="textfield" /></td>
        </tr>
        </table>
        <?php 
    }