0

I'm creating a plugin that creates a custom post type I'm working on. I followed this tutorial. I have just changed it from movie_reviews to staff members. I don't get any syntax errors in PHP and it all looks alright, but when I activate the plugin in WordPress it says it generated 3 characters of unexpected output. I think the problem is the display_staff_member_meta_box function, but there doesn't appear to be any errors in it. Below is the code I'm using.

<?php
/*
Plugin Name: Staff Members
Plugin URI: http://intranet.yourgroupuk.com
Description: Declares a plugin that will create a custom post type displaying staff members
Version: 1.0
Author: Harry Glozier
Author URI: http://intranet.yourgroupuk.com
*/

function create_staff_member() {
    register_post_type( 'staff_members',
        array(
            'labels' => array(
                'name' => 'Staff Members',
                'singular_name' => 'Staff Member',
                'add_new' => 'Add New',
                'add_new_item' => 'Add New Staff Member',
                'edit' => 'Edit',
                'edit_item' => 'Edit Staff Member',
                'new_item' => 'New Staff Member',
                'view' => 'View',
                'view_item' => 'View Staff Member',
                'search_items' => 'Search Staff Members',
                'not_found' => 'Staff Member not found',
                'not_found_in_trash' => 'No Staff Members found in Trash',
                'parent' => 'Parent Staff Member'
            ),

            'public' => true,
            'show_in_menu' => true,
            'menu_position' => 80,
            'supports' => array( 'title', 'editor', 'thumbnail', 'revisions' ),
            'menu_icon' => plugins_url( 'staff-icon-2.png', __FILE__ ),
            'exclude_from_search' => false,
            'has_archive' => true
        )
    );
}

function my_admin() {
    add_meta_box( 'staff_member_meta_box',
        'Staff Member Details',
        'display_staff_member_meta_box',
        'staff_members', 'normal', 'high'
    );
} 
?>

<?php
function display_staff_member_meta_box( $staff_member ) { //Causes error
    // Retrieve current Job Title and Company based on staff ID
    $job_title = esc_html( get_post_meta( $staff_member->ID, 'job_title', true ) );
    $company = esc_html( get_post_meta( $staff_member->ID, 'company', true ) );

    $table = <<<TEXT
    <table>
        <tr>
            <td style="width: 100%">Job Title</td>
            <td><input type="text" size="80" name="staff_member_job_title" value="$job_title" /></td>
        </tr>
        <tr>
            <td style="width: 150px">Company</td>
            <td>
                <select style="width: 200px" name="staff_member_company">
                    <option value="Your Group">Your Group</option>
                    <option value="Your Power">Your Power</option>
                    <option value="Your Electrical">Your Electrical</option>
                    <option value="Newmills Engineering">Newmills Engineering</option>
                </select>
            </td>
        </tr>
    </table>
TEXT;
    echo $table;
} //End of function 
?>

<?php
function add_staff_member_fields( $staff_member_id, $staff_member ) {
    // Check post type for staff members
    if ( $staff_member->post_type == 'staff_members' ) {
        // Store data in post meta table if present in post data
        if ( isset( $_POST['staff_member_job_title'] ) && $_POST['staff_member_job_title'] != '' ) {
            update_post_meta( $staff_member_id, 'job_title', $_POST['staff_member_job_title'] );
        }
        if ( isset( $_POST['staff_member_company'] ) && $_POST['staff_member_company'] != '' ) {
            update_post_meta( $staff_member_id, 'company', $_POST['staff_member_company'] );
        }
    }
} 

/* function include_template_function( $template_path ) {
    if ( get_post_type() == 'staff_members' ) {
        if ( is_single() ) {
            // checks if the file exists in the theme first,
            // otherwise serve the file from the plugin
            if ( $theme_file = locate_template( array ( 'single-staff_members.php' ) ) ) {
                $template_path = $theme_file;
            } else {
                $template_path = plugin_dir_path( __FILE__ ) . '/single-staff_members.php';
            }
        }
    }
    return $template_path;
} */
?>

<?php
add_action( 'init', 'create_staff_member' );
add_action( 'admin_init', 'my_admin' );
add_action( 'save_post', 'add_staff_member_fields', 10, 2);
//add_filter( 'template_include', 'include_template_function', 1 );
1

The unexpected output is the blank lines betwen ?> and <?php.

Avoid to close php tags and open them again with no reason. In your code, remove all the closing php tags (?>) and leave only the opening tag on first line.

  • Thanks a lot! I was not aware that would cause a problem, I only did it like that as I did it one function at a time. – Harry12345 Aug 4 '14 at 11:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.