• Hello, im very new in building WP Plugins.

    I want to build a plugin that changes the title and the content before posting, but i have a problem with wp_insert_post. When i run this function, the script keeps running until Apache crashes (i think is an infinite loop).

    This is the code:

    function one($post_id, $post) {
    
        $post->post_title = "new title";
        $post->post_content = "new comment";
        wp_insert_post($post);
    
    }
    
    add_action("publish_post", "one", 1, 2);

    Any ideas?

    Kind regards.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter elusuario

    (@elusuario)

    The code is adding the post, but…

    If i turn off the time limit (set_time_limit(0);), it keeps running infinitely; if not, it stops after 30 seconds and i get a fatal error.

    the code gets stuck in an infinite loop. wp_insert_post calls publish_post which calls wp_insert_post so on and so on. I am looking for a clean way to resolve this issue as well. hopefully i won’t have to resort to wpdb calls.

    wlk

    (@wlk)

    Hi,
    I suggest you to do it other way. Because what you did does this:

    1. on publish_post do:
    2. add new post and call publish_post, which goes to step 1

    Function one is ok, but the trigger that activates it is wrong (unless you want to have infinite loop of posts being created).

    Find more appropriate hook for calling function that will create new post.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Problem with wp_insert_post’ is closed to new replies.