Site logo

[Support request] Quizzes Break on PHP 8

Home Forums Support [Support request] Quizzes Break on PHP 8

Home Forums Support Quizzes Break on PHP 8

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #2493782
    Gevorg

    Hello,

    We have LearnDash installed and its quizzes break after upgrading to PHP 8. I have contacted their support and after checking the error logs, they suggested contacting the GP support.

    I would really appreciate it if you could take a look at this and see if there is a fix.
    You can find more information in the form below.

    Thank you.

    #2494053
    David
    Staff
    Customer Support

    Hi there,

    the Stack Trace points to line 32 of the content-single.php template in your child theme.

    This line: $mainCategorySlug = str_replace(' ', '-', strtolower($mainCategory));

    For reference in this part of the code:

    if ( $mainCategory == '' ) {
        $mainCategory = get_the_category_by_ID($subcategory->category_parent);
        $mainCategorySlug = str_replace(' ', '-', strtolower($mainCategory));
    }

    The strtolower function is expecting $mainCategory to be a string and for whatever reason it is not.

    To debug comment out that line and var_dump the variable eg.

    if ( $mainCategory == '' ) {
        $mainCategory = get_the_category_by_ID($subcategory->category_parent);
        //$mainCategorySlug = str_replace(' ', '-', strtolower($mainCategory));
        var_dump($mainCategory);
    }

    It may give you some idea of whats going wrong.

    #2494091
    Gevorg

    Hi David,

    As always your codes are spot on and solve the problem. Thank you so much!

    The only issue now is that there is an error message on the quiz pages:

    object(WP_Error)#5500 (3) { [“errors”]=> array(1) { [“invalid_term”]=> array(1) { [0]=> string(11) “Empty Term.” } } [“error_data”]=> array(0) { } [“additional_data”:protected]=> array(0) { } }

    Is there a way to get rid of that?

    #2494217
    David
    Staff
    Customer Support

    So thats what var_dump is doing which we can use to debug the issue.

    Ill try and explain, so in your code:

    if ( $mainCategory == '' ) {
        $mainCategory = get_the_category_by_ID($subcategory->category_parent);
        $mainCategorySlug = str_replace(' ', '-', strtolower($mainCategory));
    }

    This line: $mainCategory = get_the_category_by_ID($subcategory->category_parent); is loading the $mainCategory with a category term that is returned by the get_the_category_by_ID function.
    The get_the_category_by_ID will either return the correct term name as a string or a wp_error as an array

    This line uses:

    $mainCategorySlug = str_replace(' ', '-', strtolower($mainCategory));

    then uses the strtolower function to convert the $mainCategory string to lower case.

    But as var_dump shows us the $mainCategory is returning an wp_error array so the strtolower function fails as it wants a string.

    Try changing that code to:

    if ( $mainCategory == '' ) {
    
        $mainCategory = get_the_category_by_ID($subcategory->category_parent);
        if ( ! is_wp_error( $mainCategory ) ) {
            $mainCategorySlug = str_replace(' ', '-', strtolower($mainCategory));
        }
        
    }

    This will check to make sure there is ! (not) a wp_error before doing the string replace.

    #2494233
    Gevorg

    David,

    Thank you so much for your help! It works now. I appreciate it.

    Have a great day.

    #2494369
    David
    Staff
    Customer Support

    You’re welcome.

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