是否可以验证PHP代码?
我有一个自定义的贴子类型(电影),带有自定义的元框。
一切都很好,我可以添加新的电影和细节的自定义元框。
我的问题是更新按钮。如果我创建了一部新电影,请添加信息,然后单击“更新”以获得白色屏幕。
我知道问题出在函数上。php和自定义帖子类型,但我如何找到错误?
如果我检查白页的源代码,它会显示2行空行,但代码没有前导或尾随空行。
<?php
add_action( \'admin_print_styles-post-new.php\', \'film_style\', 11 );
add_action( \'admin_print_styles-post.php\', \'film_style\', 11 );
function film_style() {
global $post_type;
if( \'films\' == $post_type )
wp_enqueue_style( \'film-style\', get_stylesheet_directory_uri() . \'/css/filmMetaBox.css\');
}
?>
<?php
add_action(\'init\', \'film_init\');
function film_init(){
$film_labels = array(
\'name\' => _x(\'Film\', \'post type general name\'),
\'singular_name\' => _x(\'Films\', \'post type singular name\'),
\'all_items\' => __(\'All Films\'),
\'add_new\' => _x(\'Add new Film\', \'film\'),
\'add_new_item\' => __(\'Add new Film\'),
\'edit_item\' => __(\'Edit Film\'),
\'new_item\' => __(\'New film\'),
\'view_item\' => __(\'View film\'),
\'search_items\' => __(\'Search in films\'),
\'not_found\' => __(\'No films found\'),
\'not_found_in_trash\' => __(\'No films found in trash\'),
\'parent_item_colon\' => \'\'
);
$args = array(
\'labels\' => $film_labels,
\'public\' => true,
\'publicly_queryable\' => true,
\'show_ui\' => true,
\'query_var\' => true,
\'rewrite\' => true,
\'capability_type\' => \'post\',
\'hierarchical\' => false,
\'menu_position\' => null,
\'supports\' => array(\'title\',\'editor\'),
\'has_archive\' => \'films\',
\'register_meta_box_cb\' => \'cd_meta_box_add\'
);
register_post_type(\'films\',$args);
}
?>
<?php
add_action( \'add_meta_boxes\', \'cd_meta_box_add\' );
function cd_meta_box_add(){
add_meta_box( \'my-meta-box-id\', \'Film Credits\', \'cd_meta_box_cb\', \'films\', \'normal\', \'high\' );
}
function cd_meta_box_cb( $post ){
$values = get_post_custom( $post->ID );
$title = isset( $values[\'meta_box_title\'] ) ? esc_attr( $values[\'meta_box_title\'][0] ) : \'\';
$director = isset( $values[\'meta_box_director\'] ) ? esc_attr( $values[\'meta_box_director\'][0] ) : \'\';
$desc = isset( $values[\'meta_box_desc\'] ) ? esc_attr( $values[\'meta_box_desc\'][0] ) : \'\';
wp_nonce_field( \'my_meta_box_nonce\', \'meta_box_nonce\' );
?>
<div id="filmMeta">
<p>
<label for="meta_box_title" class="label">Film Title</label>
<input type="text" name="meta_box_title" id="meta_box_title" class="textInput" value="<?php echo $title; ?>" />
</p>
<p>
<label for="meta_box_title" class="label">Director</label>
<input type="text" name="meta_box_director" id="meta_box_title" class="textInput" value="<?php echo $director; ?>" />
</p>
<p>
<label for="meta_box_desc" class="label">Description</label>
<textarea name="meta_box_desc" id="meta_box_desc" class="textInput" col="100" row="5"><?php echo $desc; ?></textarea>
</p>
</div>
<?php
}
add_action( \'save_post\', \'cd_meta_box_save\' );
function cd_meta_box_save( $post_id ){
if( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE ) return;// Bail if we\'re doing an auto save
// 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( !current_user_can( \'edit_post\' ) ) return;// if our current user can\'t edit this post, bail
$allowed = array( // now we can actually save the data
\'a\' => array( // on allow a tags
\'href\' => array() // and those anchords can only have href attribute
)
);
if( isset( $_POST[\'meta_box_title\'] ) )//if data set save it.
update_post_meta( $post_id, \'meta_box_title\', wp_kses( $_POST[\'meta_box_title\'], $allowed ) );
if( isset( $_POST[\'meta_box_director\'] ) )
update_post_meta( $post_id, \'meta_box_director\', wp_kses( $_POST[\'meta_box_director\'], $allowed ) );
if( isset( $_POST[\'meta_box_desc\'] ) )
update_post_meta( $post_id, \'meta_box_desc\', wp_kses( $_POST[\'meta_box_desc\'], $allowed ) );
}
?>