The numbers
- Aimed at the talk: 47 people.
- Attendants: 23 people.
- It was held on February 15, 2020 in the space provided by ULab, the co-working center of Alicante.
Important: We remind you that in the tool Slack have a channel registered to organize and to share the management of the meetup, specifically the channel we use is #wpalicante of Slack WordPress Spain. If you want to lend a hand, contribute ideas and become more active part of this community follow the instructions on the following page-> http://wp-es.es/slack/
Automate Your WordPress Installation: The Chat
As we are already getting warm in this 2020, the next proposal that we make from WPAlicante is that you are able to speed up the installations that you have to make of WordPress. To this end, this month he is with us in the space of ULab the great Borja López, former organizer of the WordPress Valencia meetup and organizer of the first WordCamp Valencia, who talks about automation of WordPress installations, with PHP, WP-CLI and others.
Of course, you can't miss the classic round of questions at the end of the talk, typical of all meetingups, as well as the traditional networking accompanied by refreshments in one of the surrounding pubs, which the weather doesn't allow to be outdoors, always thanks to our sponsors: WeglotThe WordPress Plugin to translate your website into the language of your choice. You can try it out without any obligation as it is free to use for translating up to 2000 words into one language, and it takes up very little space on the server. We also have as a sponsor WP HerculesThe company offers both hosting and maintenance services for WordPress. And finally, on this occasion we also count on the professionalism of Marco ProductionsThe director, who has offered to record and edit the talk so that you don't miss any details of what happened.
We leave you the link to the shared presentation during the talk and here are some points that we found interesting. although we include at the end of this entry The full video of the presentation.
Fundamentals: On which machine we have our WordPress hosted
Installing WordPress always involves the same steps and the same 3 or 4 hours to set up the site to our liking by installing the chosen theme and the basic plugins we need. Those same steps could be programmed to be done by the machine alone and for that the system to be chosen will depend on both our technical level and the technical level of our accommodations.
It is not the same to use cheap accommodation where it is virtually impossible to access by SSHThe only thing that is more important is to have our website on our own servers or on a machine at Amazon, where it will be quite common to be able to access it by SSH.
There are three ways to do a custom WordPress installation:
- Template siteA person who installs his WordPress locally with everything he needs and then when a project arrives, copies what he has locally by modifying a couple of things without any major complications. It's the way simpler that exists and is accessible to any installer, whether or not they know how to program.
- WP_install_defaults()The default is WordPress. This one already requires a certain PHP level to be able to perform it. It's more complex than the next one we'll see but more affordable to anyone because they don't need to be able to access the server via SSH.
- WP-CLIConsole commands: to use console commands directly. This method is much more swiftly and efficiently as we'll see now, but it requires permissions to access the server console.
WordPress installation automation through PHP:
The basic method used is the WP_install_defaults()which is the default method that WordPress uses to perform an installation. There you can see how to install the default entry, the default comment, the default theme... It's located at
wp-admin/includes/upgrade.php
And we're going to create our version of WP_install_defaults() with the same parameters (we copy the header as it is), fill the function with what we want WordPress to actually install and save it in:
wp-content/install.php
This means that from now on, to perform our custom installation we always have to download WordPress, save our install.php file in wp-content and then launch the installation. Apparently nothing out of the ordinary is going to happen, nothing different from a regular installation. What happens is that After the process is finished, when we enter WordPress, we'll see ourIn the presentation you have a link to an example. You can also find for each type of element, the commands/functions that are used and particular examples of use. All the features are inside the WordPress codex, some may be a little harder to find, but everything is there and the documentation is usually pretty good.
Configure options
The largest and heaviest table in WordPress is the options table. There was a moment where each plugin and theme had its own board but at one point it was decided that everything should be on the same board. The main advantage is that we have all options togetherso there's only one place to play. The disadvantage is that there are many settings that WordPress saves as a serialized in JSON The first one is the serialization, which is not the usual serialization used, but WordPress adds fields that save the character count of the values in such a way that if you change a value by hand and it doesn't have the same count the configuration fails.
The most common tasks regarding the options are their update and recovery of the value they contain. It is very common to want to modify in the installation the language, the default category, the title of the blog, the time zone or the permalinks for the posts we create. The options are in:
wp-admin/options.php
Other tasks within the installation file
- Install pluginsNote that with the function that currently exists for PHP, only the plugin is installed, that is, it is not activated or configured. Also, only plugins from the WordPress repository can be installed. It is very complex to try to install a plugin downloaded from another site.
- Installing themes (themes): Ideally we should have a function as simple as installing plugins but unfortunately this is not the case, there are a few steps that are detailed in the presentation.
- Configure plugins and themesThe following options are available.
- Create userWhen you create it, the ID is collected that allows you to contribute the characteristics that we want it to have, such as whether it has to be superuser, whether it has to see the welcome panel in WordPress, whether a password has to be generated, whether a role has to be assigned to it... What you can do is create a special user role for our client, with which he can see the progress but cannot touch anything and we have the superuser. When we finish the website and give it to the client, we can modify his role so that from that moment on he can be the superuser.
- Create categoryThe underscore functions of WordPress. The issue of translations is vital in WordPress because if we don't have translation files, texts may appear in a language that is not the one we are dealing with. So much so that WordPress not only has WordCamps but also special events once or twice a year (WordPress Translation Day) to review the translation files.
- Create an entry (post): To do this we have to insert an entry in the Posts table with its main data, which will return an ID, which is the one we will use for other tasks such as including it in a certain category. This is not an easy task because the categories are actually terms that are stored in the relationship tables with which we will have to include the category ID that we will have to have recovered... That is, the process of doing these tasks with PHP works for any hosting at the cost that we have to be touching many more things than through the console. Within the data that is part of the post you can include the content of the post itself, so you can include in plain text even the Gutenberg blocks that we want to appear.
- Create a pageThe same as creating an entry but with the only exception that we add the parameter that indicates that it is a page and not an entry. Remember that all content is actually being saved in the same WordPress table and only the type of content changes.
- Set up the privacy policyThe only thing it does is give you a template (very crappy) but it allows you to use it by default if you don't include a specific one. Since PHP is a PHP, you can use the ID of the first user you are creating to obtain the client's data with which to fill out the privacy policy.
- Create a commentDefault comments can be included in the installation itself.
This is basically all you can do with PHP. There are some other things that can also be done but because of their complexity it is not advisable to address them, such as widget creation. We discourage this because the widgets are stored in serialized arrays in the Options table. That means that every time you have to configure a widget you have to retrieve the serialized that it touches, pass it to array as it touches, search inside the array and configure it, and the documentation with all the parameters needed to configure a widget through PHP is practically nonexistent.
WordPress installation automation via WP-CLI
Initially, it was born as a project that was not a WordPress project, but of a person who knew the command console theme well and set up the whole initial WP-CLI. Automatic liked it so much that they included it in the WordPress core. The main responsible of the WP-CLI community is Alain SchlesserA German who is often active in European WordCamps. You can also ask any questions in the group. The WP-CLI documentation is very detailed although it is true that it requires some technical knowledge, in addition to having permissions to access the console of our hosting.
Apart from greatly simplifying all the tasks that we have already seen for PHP, in WP-CLI we have a series of additional options that in PHP would be unthinkable, such as
- Change the SALTS: is something we haven't seen in PHP pq would be very complex and here it's a simple command. In addition, you can make password generation as complicated as you want with another simple console command.
- Install and activate plugins from any path. That is, not only can we install any type of plugin, whether it is in the WordPress repository or not, but we can also activate it. And if that's not enough, you can even make a list of plugins in a zip and indicate that file in the console.
- Installing a ThemeYou can also create a child theme and activate it.
- Modify serialized dataThere is a specific command to modify a variable within a serializable within a configuration. In PHP it should be done by hand and in WP-CLI it is as simple as indicating the key of the serialisable and asking to change the specific value.
- Create usersUser management: Not only does it have several commands to create users with more or less information according to our interests, but you can also create as many random users as you want, for example, to perform load or stress tests.
- Create menusIn PHP it is quite complex. Here we resolve into one command for creation and another for adding the pages we want to each menu.
- Create Custom Post Types (CPTs): What is created is the skeleton that must then be shaped.
Final networking questions and issues
QUESTIONCan you create a file with all those WP-CLI command lines to make it automatic (instead of having to type each command in the console itself)?
RESPONSEWP-CLI: WP-CLI are console commands. You can create a console file with everything you want, complicate it as much as you can and pass it to the console to run. In fact, WP-CLI can not only be used for installation. Another example of use can be to have it in a CRON so that every so often it synchronizes the data locally with what is in the Production Database. This way my copy in the place where I go around and try improvements always I have it updated with the last thing in Production.
QUESTIONWP-CLI: To use WP-CLI, all you need to do is access your WordPress console via SSH and you can do everything we've been seeing in the chat.
RESPONSEThere are two ways to do it:
1- I make a SSH connection to the server, inside I have WP-CLI, which in the documentation also appears the installation options in case it is not installed although the hosting usually has it, and I execute the WP-CLI commands.
2.- The same command puts the data in SSH and is executed through SSH. This "subcommand" is common to all WP-CLI commands and is also in the documentation.
Glossary
- SSHSecure SHell: This is a protocol that makes it easier to secure communications between two systems using a client/server architecture and that allows users to connect to a host remotely, through an authentication process (user and password) that ensures that you have permission to access the remote system.
- JSONData Exchange Format: This is a lightweight format for exchanging data. It comes from the acronym JavaScript Object Notation. It is a text format independent of the programming language that is very easy to read, generate and interpret by a human. You can use both sorted lists of values and collections of "name", "value" pairs. When you serializes a data in JSON what you are doing is converting a custom .NET object, for example, into a JSON string. The opposite process is called deionization.
- WordPress underscore featuresLocalization and Translation: These are functions used for localization and translation. There are three types:
- __( ): For normal translations.
- _x( ): For translations in a certain context.
- _e(): For translations to be displayed on the screen (the e is from "echo", which is how you paint your screen in PHP).
- SALTSWordPress sites: They are a way of keeping a WordPress site safe. To do this, it uses a cryptographic tool that helps protect each user's login by encrypting the user and password entry with its corresponding security key.
Leave a reply