如何调试“SAVE_POST”操作?

时间:2012-11-30 作者:user668499

是否可以验证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 ) );
        }
    ?>

3 个回复
最合适的回答,由SO网友:brasofilo 整理而成

你的代码在我这边运行得很好。所以,我想说你的问题在别处。

Troubleshooting guide.

您可以使用标准debuggingFirePHP. 对于FireHP不显示信息的情况,我也会使用以下选项,即:。,save_post.

function my_log( $msg, $title = \'\' )
{
    $error_dir = \'/Applications/MAMP/logs/php_error.log\';
    $date = date( \'d.m.Y h:i:s\' );
    $msg = print_r( $msg, true );
    $log = $title . "  |  " . $msg . "\\n";
    error_log( $log, 3, $error_dir );
}
在动作回调中:my_log( $_POST, \'Contents of $_POST\' );.

据我所知,没有“验证PHP”这样的东西
有些编辑器会在您编写时显示错误:http://www.jetbrains.com/phpstorm/

SO网友:kaiser

如果你需要的话var_dump() 您的save_post 回调,简单exit; 其中:

add_action( \'save_post\', \'cd_meta_box_save\' );
function cd_meta_box_save( $post_id )
{
    $post = get_post( $post_id );
    // do abort checks
    if (
        wp_is_post_revision( $post );
        OR wp_is_post_autosave( $post );
        OR ! wp_verify_nonce( ... )
        OR ... etc.
    )
        return;

    exit( var_dump( $post, $_POST ) );
}

SO网友:T.Todua

方法1:

add_action(\'save_post\', \'something_process1\');  function something_process1() {
   if(defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE) return;
   print_r($_POST);
}
方法2:创建日志文件(my_logs.txt) 在使用此代码的文件夹中:

add_action(\'save_post\', \'something_process2\',11);   function something_process2() {
    print_r($_POST);
    file_put_contents(dirname(__file__).\'/my_logs.txt\', "\\r\\n\\r\\n".ob_get_contents(), FILE_APPEND);
}

结束