Wrong add_action on the plugin.php file

Hey there,

long time - no see(write). First of all, I wish you all a happy new year - may all your wishes come true in 2024!!

I’m currently creating a new woo project with pinegrow (but, for now, without pg woo extension) and a lot of custom functionality. My goal is to create (aside of the normal blocks) a few blocks which do nothing more then registering a few react scripts to deliver a custom user interface on the my account site and some special Sites for admins.

However, I’m not sure if the bug mentioned below, maybe occurs because of the absence of any CSS file in the project yet. It’s a new project with, at the time of writing, only one block (if that’s somehow neccessary). That block just contains a div with a php action on it executing the following code:

register_react_app("/beratungen/list");
function register_react_app($sub_path) {
    $HUFSCHUH_PLUGIN_PATH = hufschuh_plugin_base_path();
    $HUFSCHUH_PLUGIN_URL = hufschuh_plugin_base_url();

    // Basispfad hinzufügen
    $base_path = "/blocks/assets/react";
    $full_path = $HUFSCHUH_PLUGIN_PATH . $base_path . $sub_path;

    // CSS-Dateien einbinden
    foreach (glob($full_path . "/*.css") as $css_file) {
        $css_url = $HUFSCHUH_PLUGIN_URL . str_replace($HUFSCHUH_PLUGIN_PATH, '', $css_file);
        wp_enqueue_style(basename($css_file), $css_url);
    }

    // JS-Dateien einbinden
    foreach (glob($full_path . "/*.js") as $js_file) {
        $js_url = $HUFSCHUH_PLUGIN_URL . str_replace($HUFSCHUH_PLUGIN_PATH, '', $js_file);
        wp_enqueue_script(basename($js_file), $js_url, array(), false, true);
    }
}

I’m not sure if the mentioned above matters in any way, but in the generated plugin.php file, on the very end of the file there is an action looking for a function called “hufschuh_add_blocks_editor_styles” which was not generated inside that plugin.php file. That add_action call causes the site to crash. Based on the name of the function I was wondering if that function get’s maybe added if i have added any CSS or JS file via the block controls?

I can send you the whole project if you’d like to and if it helps you!? :slight_smile:

Cheers
Wolfgang

In Pinegrow, in the WordPress settings (WordPress > Theme Settings) did you initially have the “Load only styles of blocks on frontend” option enabled and then disabled it?

By unchecking the box and exporting the theme, that section of code is not elminated. It is also not deleted if you regenerate the functions.php file or clean up the exported theme folder.

Tenically, when you uncheck the box “Load only styles of blocks on frontend”, in the pinegrow.json file the boolean value to the property blocks_load_if_rendered": "false" is changed, however Pinegrow continues to export the project without making any changes.

Try it this way:

  • Duplicate your current project folder and open it in Pinegrow;
  • Define the WordPress settings to suit your needs, use the “Clear the theme folder” option, and close Pinegrow;
  • Open the project in vscode, look for that code snippet and delete it anywhere;
/* Control how block assets are loaded on the front-end */
...
...
...
/* End of controlling how block assets are loaded on the front-end */
  • Reopen the project in Pinegrow, export the theme;
  • Open the exported theme in vscode and check if that feature is still present.

Hi there!

I’ve resolved the issue by matching action call with function name in plugin itself, then it did not trigger this error anymore.

If once checked the Plugin-Setting “Load only styles and scripts of blocks that are displayed on the front-end” then the exportet plugin breaks:

It does not help to uncheck the Box in settings. You need to remove the following code from your plugin PHP file manually:

/* Control how block assets are loaded on the front-end */
function framesblocks_should_load_separate_core_block_assets() {
    /* Pinegrow generated Load Block Assets Separately Begin */
    //Load only assets of blocks that are used on the page
    add_filter( 'should_load_separate_core_block_assets', '__return_true' );

    /* Pinegrow generated Load Block Assets Separately End */
}
add_action('init', 'framesblocks_add_blocks_editor_styles');
/* End of controlling how block assets are loaded on the front-end */

When enabling the “Load only styles and scripts of blocks that are displayed on the front-end” again, this code snippet is added again.

As an alternative you can add this code to the inc/custom.php while replacing MYPREFIX with your plugin slug.

if(!function_exists('MYPREFIX_add_blocks_editor_styles')) {
    function MYPREFIX_add_blocks_editor_styles() {
        return;
    }
}

Interestingly the error stays present if I add css files to my custom blocks.
I think this is a bug @matjaz ?

This is a bug triggered by checking the “Load only styles…” setting. It will be fixed in the next release, coming out this week.

Please note that unchecking the setting does not remove the issue. The solution is to regenerate functions.php / plugin file or to manually remove the code.

1 Like