phpwordpress

Error in WordPress with plugin reactpress


I'm using WordPress version 5.7.2 and when I upgrade it to php version 7.4.19 I get these errors:

Failed opening 'default' for inclusion (include_path='.:/usr/lib/php7.4') wp-includes/template-loader.php on line 106

Warning: include(default): failed to open stream: No such file or directory in /homepages/1/d229455270/htdocs/clickandbuilds/WordPress/DaseCMS/wp-includes/template-loader.php on line 106

This happens when I activate the plugin reactpress. This is the piece of code where the error occurs:

/**
* Filters the path of the current template before including it.
*
* @since 3.0.0
*
* @param string $template The path of the template to include.
*/
    $template = apply_filters( 'template_include', $template );
    if ( $template ) {
        include $template;  //Error in this line
    } elseif ( current_user_can( 'switch_themes' ) ) {
        $theme = wp_get_theme();
    if ( $theme->errors() ) {
        wp_die( $theme->errors() );
    }

Why is this happening? How can I fix it? I see that is compatible with my WordPress version...

enter image description here

And with my php version...

enter image description here

Thank you in advance


Solution

  • Why is this happening?

    Because there's a mistake in the Reactpress_Public::repr_change_page_template() method (see line 99 in wp-content/plugins/reactpress/public/class-reactpress-public.php) which is hooked onto template_include.

    The author should check if the value of the _wp_page_template metadata (which stores the path of a custom page template) is not default (which is the default value) and only if so, then should the $template value be set to the metadata value.

    And if one doesn't do that check, then we'd end up with include 'default' which then emits the error/warning in question ("Failed opening 'default' for inclusion").

    How can I fix it?

    Please contact the plugin support and ask them to fix the issue ASAP, but for the time being, you may just change the conditional here to: (* change the entire "if")

    if (!empty($meta['_wp_page_template'][0]) && $meta['_wp_page_template'][0] != $template && // wrapped
        'default' !== $meta['_wp_page_template'][0] // check if the value is NOT "default"
    ) {
        $template = $meta['_wp_page_template'][0];
    }
    

    Yes, you shouldn't modify core plugin files; but this is a special case, because the plugin needs a fix, which hopefully will come in the plugin's next release.

    Alternate solution without modifying the plugin files

    .. is by overriding the template using the same hook:

    // Add to the theme functions.php file:
    add_filter( 'template_include', 'my_fix_template_include', 100 );
    function my_fix_template_include( $template ) {
        if ( 'default' === $template && is_page() ) { // * the plugin uses is_page()
            $template = get_page_template();
        }
    
        return $template;
    }