Polylang language in REST API
Polylang is a popular multilinguality plugin for WordPress. The pro version of it also provides some additional REST API endpoints. So in the regular version a request like the following will return all posts and just ignore the lang
parameter:
Still, you can filter by language by adding a ustom REST field through your themes functions.php
:
This code was originally written by them-es.
ACF fields in REST API
ACF is another popular WordPress plugin for adding custom fields to WP posts and pages (you can add a short summary, social links etc. etc. for example). These fields are also not included in the standard WP REST API, even if in the ACF settings you activate Show In REST API
in the respective field group (even though according to the ACF documentation it should).
Again, you can add a simple snippet to the functions.php
file in order to retrieve the acf fields, too:
(This code was taken from here.)
Try GET /wp/v2/posts
and every post should now have your custom fields from AC included.
Redirect frontend to elsewhere
When using a headless WordPress setup, I ran into a situation where I wanted to have the WordPress admin section be served on www.example.com/wp-admin
, but visitors should not be allowed to go see any WordPress frontend under www.example.com
. Instead they should be redirected to the new frontend I host under www.elpmaxe.com
. In order to do that I built a super minimal theme which only redirects frontend traffic and does nothing else (for which I used this solution):
- under
themes/
create a folder calledredirect
- add a
style.css
and anindex.php
to it style.css
should look like this:style.css
should look like this:- activate the theme in the admin UI.
Add CORS header to MyCalender plugin
MyCalendar is a WordPress plugin for creating calendars. Under ‘Advanced Settings’ you can activate exposing its API, which will let you fetch events from the calendar like that: GET https://example.com?to=2024-06-25&from=2024-05-26&mc-api=json
. However, when working in a local development environment you might run into CORS issues, so you should
But you can add to following lines to the top of some file in your theme or plugins in order to allow fetching from everywhere:
I added it to /httpdocs/wp-content/plugins/my-calendar/my-calender.php
hoping that the CORS header will be scoped only to the MyCalendar routes, but I don’t think that’s the case. It’s also not the best idea to add custom code in plugins
files instead of themes
files, because the code might get overwritten when updating the respective plugin.
Moving wordpress from one server to another without plugins
- zip the public folder (e.g. `/var/www/wordpress) and download
- export the database via phpmyadmin
-
create a database on your new server and import the backup
-
change the database credentials in
wp-config.php
-
use
wp-cli
in order to seacrch-and-replace all links in your database withwp search-replace '://olddomain.com' '://new domain.com' --all-tables