Fixing Gutenberg error “The editor has encountered an unexpected error”

After update to WP 5, I’ve faced with next issue, I’ve couldn’t add new post or edit existed. Looks like error happens because of misconfigured nginx and when new ‘Gutenberg’ editor is active (which is true by default for wordpress 5.0 and above).

Earlier I had nginx location / configured in next manner:

location / {
    try_files $uri $uri/ /index.php?$args;
}

Same configuration can be found on wordpress codex page:

And on nginx recipe page:

The issue caused by question sign in try_files directive, when $args is empty, index.php is called like this: “/index.php?”. Solution is simple:

$is_args
    “?” if a request line has arguments, or an empty string otherwise

After I changed location / block like this:

location / {
    try_files $uri $uri/ /index.php$is_args$args;
}

The problem is gone.

How to use angled brackets in WP-Syntax

I often use WP-Syntax plugin for syntax highlighting. When i used angled brackets or ‘&’ symbol or something like that i recieved next garbage:

$ echo fail 2>&1 >/dev/null

It happened because wp-syntax tried to escape special symbols, but <pre> tag says wordpress to show symbols as it passed into that tag. Early, before publishing i switched from visual into text mode and changed all that garbage into what i want. But solution is to add  escaped option into <pre> tag, <pre lang=”bash” escaped=”true”>, now same block will look like i expected:

$ echo congrat 2>&1 >/dev/null

Enjoy!

Password protected access to “/wp-admin” on nginx

Here i found advice how to make wordpress more securely, idea is to protect access to “/wp-admin” by http auth, but “/wp-admin/admin-ajax.php” must stay available for everyone, same is true for some .css files.
I spend some time to research solution, it was a bit complicated:

location /wp-admin {
	location ~ /wp-admin/admin-ajax.php$ {
		# Php handler
	}
	location ~* /wp-admin/.*\.php$ {
		auth_basic            "You shall not pass!";
		auth_basic_user_file  $document_root/.htpasswd;
		# Php handler
	}
}

It is possible to add additional location to serve static content, but i am too lazy to do it.

PS
Also here i found receipts for apache and lighttpd if anybody interesting. Continue reading

Forbid access to php into “wp-content”.

I expected in few articles an advice to disable direct access to php scripts into “/wp-content/uploads” i done small research and observed, than .php placed into “/wp-content” and into subdirs was newer directly accessed thru web. So i just completely disabled direct access to php scripts placed into that dir and subdirs. Looks more securely.

PS
I made mistake. At least tiny-mce php scripts must be accessible thru web. So i just convert  this rules that touch ‘/wp-include’ for nginx:

location ~* /wp-includes {
	location ~* /wp-includes/[^/]+\.php$ {
		deny all;
	}
	location ~* /wp-includes/js/tinymce/langs/.+\.php$ {
		deny all;
	}
	location ~* /wp-includes/theme-compat {
		deny all;
	}
	location ~ /wp-includes/.+\.php$ {
		include php_wordpress_handler;
	}
}

How to change page order of top menu in wordpress.

I try to reorder menu items by setting page order into Dashboard/Pages/Quick Edit, also i try plugin called ‘my page orders’, but anyway menu items was sorted alphabetically by page title, so i started to dig deeper.
In my template ‘top menu’ generated by function ‘wp_nav_menu‘ placed into header.php. When i looked at template, i saw that in parameters specified  ‘fallback_cb‘ function defined in function.php. When i found this function, i saw that this function uses ‘get_pages‘ with default arguments to get pages, so i just define 'sort_column' now function looks like get_pages(array( ‘sort_column’ => ‘menu_order’))
After that modification pages in menu sorted by as i expected.