Wordpress操作在完成之前开始


Wordpress action starting before it's done

我试图创建一个新的bbPress论坛时,新的帖子保存…我写了这段代码,但我得到的只是一个无限循环。奇怪的是,循环开始不是当我保存一个帖子,但首先,当我去"所有帖子"或"新帖子"。请问有什么问题?

代码

<?php
add_action('save_post', 'register_ForumCustom');
function register_ForumCustom($post_id){
    $post = get_post($post_id);
    // Create post object
    $my_new_post = array(
      'post_title'    => 'Forum di'.$post->post_title,
      'post_content'  => '',
      'post_status'   => 'publish',
      'post_author'   => 1,
      'post_type' => 'forum'
    );
    // Insert the post into the database
    $new_forum_id = wp_insert_post( $my_new_post );
    //OK, maybe is here that the loop starts, but i know that the problem is because there is another "save_post". I can solve this, but i don't understand the other problem!
    update_post_meta($post_id, "forum_id", $new_forum_id);
}
?>

您通过操作创建了无限循环。

你必须解开你的动作,当你完成后重新挂钩:

// unhook
remove_action('save_post', 'register_ForumCustom');
// do updates/inserts
// re-hook
add_action('save_post', 'register_ForumCustom');

见:http://codex.wordpress.org/Plugin_API/Action_Reference/save_post Avoiding_infinite_loops