Claude-Assisted WordPress Plugin: Correction 7

Meta was barfing, so I turned to Claude, to help with something I’ve never been good at: Gutenberg Blocks. I asked it to make a block that writes the shortcode. I guess it did exactly that, but that’s not really what I wanted!

The code it produced almost worked. It was intended for a flat PHP file, but since I have everything in classes, I needed to make some edits.

Using plugins_url in a Plugin Organized into Classes

The key one is this line in plugin.php:

define( 'CORRECTION_PLUGIN_FILE', __FILE__ );

Within the code you have boilerplate like this:

        // Register the block's script
        wp_register_script(
            'correction-block-editor',
            plugins_url('build/index.js', __FILE__),
            array('wp-blocks', 'wp-element', 'wp-editor', 'wp-components')
        );

The __FILE__ is used to help build the URL to the plugin directory, and then to the JavaScript. This fails, because the file is not “plugin.php” but “includes/Correction/CorrectionBlock.php”.

The fix is:

        // Register the block's script
        wp_register_script(
            'correction-block-editor',
            plugins_url('build/index.js', CORRECTION_PLUGIN_FILE),
            array('wp-blocks', 'wp-element', 'wp-editor', 'wp-components')
        );

Overall, it was good. I spent around 2 hours working on it, and also writing this blog post. So it’s probably closer to 1 hour of work. LLMs save a lot of time, but, so far, they haven’t produced completely correct code. You still need to be able to intuit what’s wrong, and look up docs, and get things working.

The code is here: tagged claude1