wordpress: Fatal error: Maximum function nesting level of ‘100’ reached

If you are running wordpress and have xdebug installed, this is a warning that you have something wrong in functions.php or plugin file. Don’t turn xdebug off. fix the code. One place to look at the ‘save_post’ hook. Take this code for example,


function my_post_update() {
  global $post;
  $post_title = esc_html($_POST['post_title']);
  $post_status = esc_html($_POST['post_status']);
  $post_type_args = array( 'ID' => $post->ID, 'post_status' => $post_status, 'post_title' => $post_title));
  // need to unhook to avoid infinite loop
  remove_action('save_post', __FUNCTION__, 9);
  wp_update_post($post_type_args);
}
add_action('save_post', 'my_post_update', 9);

I intend to save my post before most of my plugins kicked in, so I put a priority of 9. However, because I run wp_update_post again within the function, my_post_update gets triggered again, resulting in infinite loop. To make sure we trigger the function once only, we have to remove the hook before wp_update_post.

Author: bpeh

Bernard Peh is a great passioner of web technologies and one of the co-founder of Sitecritic.net Website Design and Reviews. He works with experienced web designers and developers everyday, developing and designing commercial websites. He specialises mainly in SEO and PHP programming.