Вход на сайт

Просмотр новости

Найдите то, что Вас интересует

How to enable pretty permalinks in WordPress under wp-env

Дата публикации: 23-02-2023 15:38:22

Recently I was working on a WordPress block with custom REST endpoints and I had an interesting challenge — enable pretty permalinks in a development environment running on top of @wordpress/env (wp-env). If you didn’t know, WordPress won’t automatically redirect endpoint calls to the registered routes, unless you have pretty permalinks enabled. This also applies […]
How to enable pretty permalinks in WordPress under wp-env originates from Goce Mitevski


Основное содержимое страницы с новостью.

The content of the .htaccess file in a stylized form

Recently I was working on a WordPress block with custom REST endpoints and I had an interesting challenge — enable pretty permalinks in a development environment running on top of @wordpress/env (wp-env).

If you didn’t know, WordPress won’t automatically redirect endpoint calls to the registered routes, unless you have pretty permalinks enabled. This also applies to posts, pages and basically all other permalinks that use URL parameters to pass data and have an associated redirection rule attached to the URL parameter.

If you don’t have pretty permalinks enabled, the custom REST endpoint URL is a bit different. Instead of http://localhost:8888/wp-json/custom-route/ it accepts the REST route as a URL parameter – http://localhost:8888/?rest_route=/custom-route/. And that’s all fine. You just need to be aware of this difference, especially when working in containerized environments based on Docker – such as wp-env.

wp-env lets you easily set up a local WordPress environment for building and testing plugins and themes. Since WordPress blocks are in fact plugins, we can use wp-env for development and testing.

So, how do you enable pretty permalinks in a development environment running on top of wp-env?

Turns out, it’s as easy as creating a .htaccess file with the correct ownership. In short, connect to the Docker container running the WordPress environment, create the .htaccess file and transfer its ownership to the www-data user. Anyway, let’s do it, step by step.

First and foremost, navigate to the folder from which wp-env is started. This is usually the folder of the WordPress plugin, when working with blocks.

Find the name of the Docker container for the running the WordPress environment by typing: docker ps.

Connect to the Docker container and start bash by typing: docker exec -i -t <container id goes here> /bin/bash.

Check if the current folder is the one of the actual WordPress instance by typing: ls -la. You should get a list of the familiar WordPress filesystem.

Create the .htaccess file by typing: touch .htaccess. The file should be created by the root user.

Transfer the ownership of .htaccess to the www-data user by typing: chown www-data:www-data .htaccess.

That’s all! Now you can navigate to the Permalink Settings page (Settings > Permalinks) and change the Permalink structure to anything you like. Pretty permalink redirection should be applied accordingly to all WordPress routes, including custom REST endpoints of plugins.

In the background WordPress will check if it has privileges to write in .htaccess. Since it has (from the www-data user), it will automatically add the required rewrite rules to the .htaccess file. If you cat .htaccess you’ll see that the content of the file changes, based on your selection in the Permalink Settings page.

# BEGIN WordPress

# The directives (lines) between “BEGIN WordPress” and “END WordPress” are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* – [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ – [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

It goes without saying that you’ll need to do everything from scratch, if at one time, you run wp-env destroy. wp-env destroy deletes all Docker containers, volumes and local files created in the containers.

And that’s about it. I hope you find this article useful.

Схожие новости

#Наименование новостиТональностьИнформативностьДата публикации
1Five WordPress Features Wanted Before Version 5.009.3113-07-2015
2Revisiting WordPress smilies in a pure CSS way010.4220-12-2014
3Introducing the Advanced Professional WordPress Developer Certification07.6519-02-2026
4 Comment on How to Embed an MP4 Video in WordPress (Block Editor, Classic, HTML, Shortcode, & Plugin) by Konstantin Obenland 07.6503-03-2026
5Хук-Фильтр WordPress: post_type_link021.1121-09-2026
6X-post: Introducing Ipsum, the new default theme010.216-09-2026
7Access more visitor data with the Kinsta API07.3514-08-2026
8Хук-Фильтр WordPress: paginate_links017.6920-09-2026
9Solicitação para PTE011.8109-09-2026
10Solicitação para PTE013.4219-08-2026

Классификация: . Схожих патентов: 0. Схожих новостей: 10. Тональность: 0. Информативность: 11.6. Источник: www.nicer2.com.