Tag: WordPress

Lately I’ve been working on a WordPress Gutenberg plugin. The official WordPress documentation has always been a bit hit and miss due to them changing things so frequently but it’s been good for getting the initial plugin development environment setup. Unfortunately, within an hour of development I stumbled into my first big problem; several of the controls do not seem to exist!

ComboboxControl and the Flex Controls are the two that I wanted to use and despite being covered in the docs they are not actually included with the built in Gutenberg. If you try to use them the block will fail with the dreaded generic error:

"This block has encountered an error and cannot be previewed"

In the browser console you will see the following error:

wordpress react Warning: React.createElement: type is invalid -- expected a string

The solution is quite simple; install the Gutenberg plugin! The one that is bundled with WordPress is an older version. The main Gutenberg plugin project is very active and frequently updated:

That should solve most of your missing component problems. I actually got double unlucky as this latest Gutenberg version still does not include the ComboboxControl but I suspect that is due to some big changes they have been making to it which are covered in this pull request.

I’ve been using the Members plugin on a client’s WordPress site to define custom roles. It’s been working great except for one thing; the authors dropdown on the Gutenberg document editor does not show anyone who has a custom role.

I had a look around and found several posts that suggested using the the wp_dropdown_users_args filter which worked for the Quick Edit author dropdown but not for the main document editor. In fact I couldn’t find anybody with a solution for that since Gutenberg was introduced. I managed to suss it out for myself and thought I’d share the code for anyone stuck in a similar situation. The following goes inside functions.php. Make sure to replace “custom_role_one” and “custom_role_two” with your actual role names.

// Handles the Quick Edit authors dropdown
function display_custom_roles_in_author_dropdown($query_args, $r)
{
	if (isset($r['name']) && ($r['name'] === 'post_author_override' || $r['name'] === 'post_author')) {
		if (isset($query_args['who'])) {
			unset($query_args['who']);
        }
        $query_args['role__in'] = ['administrator', 'author', 'editor', 'custom_role_one', 'custom_role_two'];
    }
    return $query_args;
}
add_filter('wp_dropdown_users_args', 'display_custom_roles_in_author_dropdown', 10, 2);

// Handles the Gutenberg document editor authors dropdown
function display_custom_roles_in_gutenberg_author_dropdown($prepared_args, $request = null)
{
	if (isset($prepared_args['who']) && $prepared_args['who'] === 'authors') {
		unset($prepared_args['who']);
		$prepared_args['role__in'] = ['administrator', 'author', 'editor', 'custom_role_one', 'custom_role_two'];
	}
    return $prepared_args;
}
add_filter('rest_user_query', 'display_custom_roles_in_gutenberg_author_dropdown', 10, 2);

Recently I’ve been working on a new project as part of a development team. After a recent Git merge I found that I was unable to save any changes on WordPress posts and pages. I logged out of the site and cleared cache but then found I was unable to login. When I entered my details and submitted the form all that would happen is that I would be redirected back to the login page.

I read about several possible reasons for this and tried out several of the fixes but nothing worked. I put my Sherlock cap on and started checking the commits. It turns out the culprit had been hidden away in an old branch that only recently got committed.

It was inside the .htaccess file:

php_value upload_max_filesize 20MB
php_value post_max_size 25MB
php_value memory_limit 30MB
php_value max_execution_time 90

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

Did you spot it? Kudos if you did! The culprit was the three PHP values that specify MB. PHP expects those to just say M as specified in the shorthand byte value note. After removing those naughty B’s I was able to login and save updates again. Here’s the working version:

php_value upload_max_filesize 20M
php_value post_max_size 25M
php_value memory_limit 30M
php_value max_execution_time 90

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress