Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

Higher PHP version causes site to break

  • Hello lovely people of the GeneratePress support team.
    A few days ago had a message on my WP site to say I had an outdated version of PHP and to upgrade.
    I asked my hosting service to do this and thought everything was fine.
    However today a very kind person emailed me to say my website wasn’t working!
    So I got back in touch with my hosting service who indeed said that anything higher than the 7.4 version of PHP breaks my site.
    I’ve given you the ‘stack trace’ info that WPX gave me in order to show you the critical error.
    As always I really appreciate your help.
    Cheers Jodie

  • Hi there,

    It looks like you have a custom function in your child theme that’s triggering this error. Could you please share the full snippet you’ve added to your child theme’s functions.php file so we can take a closer look?

  • Hi Alvind
    Here you go.
    Cheers Jodie

    <?php
    /**
     * GeneratePress child theme functions and definitions.
     *
     * Add your custom PHP in this file. 
     * Only edit this file if you have direct access to it on your server (to fix errors if they happen).
     */
    
    function generatepress_child_enqueue_scripts() {
    	if ( is_rtl() ) {
    		wp_enqueue_style( 'generatepress-rtl', trailingslashit( get_template_directory_uri() ) . 'rtl.css' );
    	}
    }
    add_action( 'wp_enqueue_scripts', 'generatepress_child_enqueue_scripts', 100 );
    get_avatar( get_the_author_meta( $author_id ));
    
    function ta_add_noindex_headers() {
        header( 'X-Robots-Tag: noindex, nofollow' );
    }
     
    add_action( 'ta_before_link_redirect' , 'ta_add_noindex_headers' );
    
    add_action( 'after_setup_theme', function() {
        add_theme_support( 'wp-block-styles' );
    } );
    
    add_filter( 'generate_post_date_output', function( $output, $time_string ) {
    	printf( '<span class="posted-on">%s</span>',
    		$time_string
    	);
    }, 10, 2 );
    
    add_shortcode('portable_hook', function($atts){
    	ob_start();
            $atts = shortcode_atts( array(
                'hook2_name' => 'no foo'
            ), $atts, 'portable_hook' );
    		do_action($atts['hook2_name']);
    	return ob_get_clean();
    });
    
    add_filter( 'the_content', 'insert_featured_image_with_meta_check', 20 );
    
    function insert_featured_image_with_meta_check( $content ) {
    	global $post;
        
    	$inserted_hook = do_shortcode('[portable_hook hook2_name="after_first_image"]');
    	return prefix_insert_after_first_image( $inserted_hook, 1, $content );
    }
    
    function prefix_insert_after_first_image( $insertion, $image_id, $content ) {
    	$images = preg_split( '/(<img[^>]+\>)/i', $content, -1, PREG_SPLIT_DELIM_CAPTURE );
     
    	foreach ( $images as $index => $image ) {
        	if ( $image_id == $index ) {
            	$images[$index] .= $insertion;
        	}
    	}
     
    	return implode( '', $images );
    }
    
    function gravatar_alt($text) {
        $alt = get_the_author_meta( 'display_name' );
        $text = str_replace('alt=\'\'', 'alt=\'Avatar for '.$alt.'\' title=\'Gravatar for '.$alt.'\'',$text);
        return $text;
    }
    add_filter('get_avatar','gravatar_alt');
    
    add_filter( 'generate_hook_element_display', function( $display, $element_id ) {
    	if(400138 === $element_id && !is_admin()) {
    		$category = get_category( get_query_var( 'cat' ) );
    		var_dump($category->category_parent);
    		if($category->category_parent > 0) {
    			return false;
    		}
    	} 
    	return $display;
        
    }, 10, 2 );
    
    add_filter( 'generate_comments_title_output', function( $output, $comments_title ) {
        return sprintf(
            '<p class="comments-title">%s</p>',
            esc_html( $comments_title )
        );
    }, 10, 2 );
    
    add_filter( 'comment_form_defaults', 'custom_reply_title' );
    function custom_reply_title( $defaults ){
      $defaults['title_reply_before'] = '<p id="reply-title" class="comment-reply-title">';
      $defaults['title_reply_after'] = '</p>';
      return $defaults;
    }
    
    add_filter( 'generate_comments_title_output', function( $output, $comments_title ) {
        $comments_title = 'Comments';
        return sprintf(
            '<p class="comments-title">%s</p>',
            esc_html( $comments_title )
        );
    }, 10, 2 );
    
    add_filter( 'generate_element_display', function( $display, $element_id ) {
        if ( 1533194 === $element_id && is_author( 'jodie-morgan' ) ) {
            $display = true;
        }
    
        return $display;
    }, 10, 2 );
    
    add_filter('render_block', function ($block_content, $block) {
    	if (!is_admin() && ! empty($block['attrs']['className']) && strpos($block['attrs']['className'], 'add-nofollow') !== false) {
    		$robots_meta = get_post_meta(get_the_ID(), 'rank_math_robots', true);
    
    		if ( strpos($robots_meta, 'noindex') !== false ) {
    			$myreplace = 'href';
    			$myinsert = 'rel="nofollow" href';
    			$block_content = str_replace($myreplace, $myinsert, $block_content);
    		}
    	}
    
    	return $block_content;
    }, 10, 2);
    
    // links with class important-link
    // open in new window and rel no follow
    add_filter( 'render_block', function( $block_content, $block ) {
        // check the block for class names
        if ( 
            ! empty( $block['attrs']['className'] ) 
            && 'important-link' === $block['attrs']['className']
        ) {
            $block_content = str_replace( '<a ', '<a target="_blank" rel="nofollow" ', $block_content );
        }
    
        return $block_content;
    }, 10, 2 );
    
    add_filter( 'generate_404_text','generate_custom_404_text' );
    function generate_custom_404_text()
    {
        return '';
    }
    add_filter( 'get_search_form','my_remove_search_form' );
    function my_remove_search_form()
    {
        $template = $GLOBALS['template'];
        $template_file = substr($template, strrpos($template, '/') + 1);
        if ($template_file === '404.php') {
            return '';
        }
    }
    
    add_filter( 'generate_404_title','remove_404_title' );
    function remove_404_title()
    {
          return '';
    }
    
    function disable_comments_rss_feed( $for_comments ) {
    	if ( is_comment_feed() ) {
        	return '';
    	}
    	return $for_comments;
    }
    add_filter( 'commentsrss2_comments_feed', 'disable_comments_rss_feed' );
    
    add_action( 'after_setup_theme', 'tu_add_comment_url_filter' );
    function tu_add_comment_url_filter() {
    	add_filter( 'comment_form_default_fields', 'tu_disable_comment_url', 20 );
    }
    
    function tu_disable_comment_url($fields) {
    	unset($fields['url']);
    	return $fields;
    }
    
    add_action( 'template_redirect', 'noindex_feeds' );
    function noindex_feeds() {
        if ( ! is_feed() ) {
            return;
        }
    header( 'X-Robots-Tag: noindex', true );
    	}
    
  • Can you try replacing this block of code:

    add_filter('render_block', function ($block_content, $block) {
    	if (!is_admin() && !empty($block['attrs']['className']) && strpos($block['attrs']['className'], 'add-nofollow') !== false) {
    		$robots_meta = get_post_meta(get_the_ID(), 'rank_math_robots', true);
    
    		if (strpos($robots_meta, 'noindex') !== false) {
    			$myreplace = 'href';
    			$myinsert = 'rel="nofollow" href';
    			$block_content = str_replace($myreplace, $myinsert, $block_content);
    		}
    	}
    
    	return $block_content;
    }, 10, 2);

    With this updated version:

    add_filter('render_block', function ($block_content, $block) {
    	if (!is_admin() && !empty($block['attrs']['className']) && strpos($block['attrs']['className'], 'add-nofollow') !== false) {
    		$robots_meta = get_post_meta(get_the_ID(), 'rank_math_robots', true);
    
    		// Fix: Ensure $robots_meta is a string before using strpos()
    		if (is_string($robots_meta) && strpos($robots_meta, 'noindex') !== false) {
    			$myreplace = 'href';
    			$myinsert = 'rel="nofollow" href';
    			$block_content = str_replace($myreplace, $myinsert, $block_content);
    		}
    	}
    
    	return $block_content;
    }, 10, 2);

    The issue in the error log was likely caused by the strpos() function being passed a non-string value. This updated code ensures that $robots_meta is a string before attempting to use strpos(), which should resolve the error.

  • Thanks very much Alvind. So do I replace that first and then try to update the PHP version to 8 to see if the site functions as normal?

  • That’s correct, let us know how it goes!

  • Hi Alvind. I’ve done as you’ve instructed and the site has been upgraded to PHP Version 8.3 and it’s working now.
    Thank you very much for your help.
    Cheers Jodie

  • You’re welcome, glad to hear that! 🙂

Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.