0

I'm having a weird problem I have written the code as below:

add_action('save_post', 'save_post_func');

function save_post_func(){
    include_once(ABSPATH . WPINC . '/feed.php');

    $rss = fetch_feed($url);

    if (!is_wp_error($rss)) {
        //ini_set('xdebug.max_nesting_level', 1000); <-- Doesn't work

        $maxitems = $rss->get_item_quantity(5);

        $rss_items = $rss->get_items(0, $maxitems);

        foreach ($rss_items as $item) { // count($rss_items) = 5 ???
            wp_insert_post(array('post_title' => 'a')); // inserts > 100 records ???
        }

    }

}

Displays this error:

Fatal error: Maximum function nesting level of '100' reached, aborting!

Any help? Where is the problem all seems ok...

  • 2
    Currently you insert post, if you save a post and this run in a endless loop. I think you must change the hook to init, not save_post. – bueltge Jun 4 '14 at 9:07
  • No no it is in the save post hook.The error only occurs when i save the post and i try to insert another post – Antonio Papa Jun 4 '14 at 9:26
  • 1
    @AntonioPapa I see you fixed it, nice one! @bueltge is right - if you use wp_insert_post on your save_post hook, you trigger an infinite loop! Hence why removing the hook on first-run solves the issue :) – TheDeadMedic Jun 4 '14 at 9:47
  • Yep you are right i thought i made a error in the loop :D – Antonio Papa Jun 4 '14 at 15:25
1

Fixed the problem I've added this line before the insertion of the posts.

remove_action('save_post', __FUNCTION__);

final code:

add_action('save_post', 'save_post_func');

function save_post_func(){
    remove_action('save_post', __FUNCTION__);

    include_once(ABSPATH . WPINC . '/feed.php');

    $rss = fetch_feed($url);

    if (!is_wp_error($rss)) {

        $maxitems = $rss->get_item_quantity(5);

        $rss_items = $rss->get_items(0, $maxitems);

        foreach ($rss_items as $item) { 
            wp_insert_post(array('post_title' => 'a')); 
        }

    }

}
1

That's because the code you were using was creating an infinite loop. You should unhook the function before calling wp_insert_post and add it back again when done. Here is the sample code you should use:

function save_post_func( $post_id ){
    if ( ! wp_is_post_revision( $post_id ) ){

        // unhook this function so it doesn't loop infinitely
        remove_action('save_post', 'save_post_func');

        include_once(ABSPATH . WPINC . '/feed.php');

        $rss = fetch_feed($url);

        if (!is_wp_error($rss)) {

            $maxitems = $rss->get_item_quantity(5);

            $rss_items = $rss->get_items(0, $maxitems);

            foreach ($rss_items as $item) { 
                wp_insert_post(array('post_title' => 'a')); 
            }

        }

        // re-hook this function
        add_action('save_post', 'save_post_func');
    }
}
add_action('save_post', 'save_post_func');  

More info from Codex

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.