0

I am trying to filter my post title for a specific page and when a form is submitted on that page , for this I am trying to filter my title . Here is the code

function filter_title_after() {

   $current_page_id = get_the_ID();
   if($current_page_id == 58 && !empty($_POST)){
       $title = "Congratulation";
   } else {
       $title = the_title();
   }

    return $title;
}
add_filter( 'the_title', 'filter_title_after'); 

After adding this filter , I am getting an error ""Maximum function nesting level of '100' reached, aborting!

I have already looked several stackexchange question , but none of the came to any help

wp_insert_posts Fatal error: Maximum function nesting level of '100' reached, aborting! Problem:Save Several Duplicate posts in The Database and then Error nesting level of '100' reached

3

the_title() calls your function again. If you want to avoid that, remove the callback inside of your function:

function filter_title_after() {
    remove_filter( current_filter(), __FUNCTION__ );
    // the rest of your code.

But you shouldn’t call the_title() in your function at all: it prints the title – this is not what you want – and you get the original title and ID already as an argument from WordPress. Use it.

Also, testing for POST requests should be done by checking $_SERVER[ 'REQUEST_METHOD' ].

function filter_title_after( $title, $post_id ) {

    if ( ! is_singular() )
        return $title;

    if ( 58 !== (int) $post_id )
        return $title;

    if ( 'POST' !== $_SERVER[ 'REQUEST_METHOD' ] )
        return $title;

    return "Congratulation";
}

add_filter( 'the_title', 'filter_title_after', 10, 2 ); 
1
  • Thanks @toscho , that solves my problem and learned something new . Have a great day . – shuvroMithun Feb 20 '15 at 5:43

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.