插件生成意外输出-没有PHP错误

时间:2014-08-04 作者:Harry12345

我正在创建一个插件,创建一个我正在处理的自定义帖子类型。我跟着this tutorial. 我刚刚将其从movie\\u reviews更改为员工。我在PHP中没有发现任何语法错误,看起来一切正常,但当我在WordPress中激活插件时,它说它生成了3个字符的意外输出。我认为问题在于display\\u staff\\u member\\u meta\\u box函数,但它似乎没有任何错误。下面是我正在使用的代码。

<?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 个回复
最合适的回答,由SO网友:gmazzap 整理而成

意外输出为以下空白行?><?php.

避免无缘无故地关闭php标记并再次打开它们。在代码中,删除所有结束的php标记(?>) 只在第一行留下开头标记。

结束

相关推荐

How can I find plugins' slug?

我想知道如何才能找到插件的slug(slug=WordPress用来更新插件和确定哪些插件当前处于活动状态的内部名称)?它通常是插件的文件夹名,但如果插件没有文件夹,则是其文件名(如hello.php)。是否有其他例外情况?大小写字符重要吗</插件的slug是否可以与其文件夹名不同?如果有一个叫做hello的插件呢。php和另一个/您好。php/您好。php