metabox在wordpress插件中无法正确显示

时间:2014-04-23 06:17:34

标签: php plugins wordpress-plugin wordpress

我尝试编写自己的插件,但每次我收到错误或元数据都没有正确显示。

这是我的插件代码 看到它显示在标题顶部的代码。我想在“编辑器”框下面显示它 我很累,但没有正确显示。

<?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 
}
?>

2 个答案:

答案 0 :(得分:0)

请参阅下面的完整工作代码:

<?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) );      
    }


}

答案 1 :(得分:0)

问题是您的register_meta_box_cb回调函数应该是:

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

然后使用HTML输出函数print_meta_box()

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

但也许你正在寻找一些like this(没有元框和摆脱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 
}

相关问题