Wordpress操作在它完成之前开始

时间:2013-05-18 15:58:10

标签: php wordpress-plugin wordpress

我正在尝试在保存新帖子时创建一个新的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);

}

?>

1 个答案:

答案 0 :(得分:0)

您通过行动创建了无限循环。

完成后你必须解开你的行动并重新挂钩:

// 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