如何更新WordPress主题:安全而深入的指南插图

WordPress theme is one of the most important parts of WordPress website. It determines the appearance of the website, the way the content is displayed, and makes the website look more modern, more professional and more in line with the brand image.

Therefore, it is important to update the theme in a timely manner and ensure that it always works properly.

But sometimes it’s not entirely simple. If you have customized the WordPress theme, updating it means you may lose your work. If the plug-ins in your site are not compatible with the new version of the theme, you need to decide which code needs to be updated and which does not. If you are running the WooCommerce Store with your own customized theme, you need to make sure it works properly after the update.

In this article, I will teach you how to update WordPress topics.

Why keep updating WordPress themes?

It is important to keep the WordPress website code up to date. New versions of plug-ins, themes, and WordPress itself are all released for a reason. Failure to update in time may bring risks to your website. Let’s look at three reasons why you should always keep your site up-to-date: security, compatibility, and functionality.

# 1 Security

The most important reason to ensure that your Web site code is always updated is security.

A large part of the updates to themes, plug-ins, and WordPress cores are security patches: small versions designed to overcome any security vulnerabilities.

Because WordPress is an open source system and has an active community to monitor software security and other issues, any security vulnerabilities will be publicly discussed and fixed as soon as possible.

This is also one of the advantages of WordPress: an active community ensures that vulnerabilities are quickly discovered and fixed.

But there is a downside: the openness of all this means that anyone who wants to attack your site will know that there is something wrong with a particular version of the theme. They may target sites that use the theme of this version.

Therefore, as soon as an update is released, you need to update the theme, plug-in, and WordPress kernel immediately to avoid becoming a target.

If you install a good security plug-in, you will be alerted when a new version of the theme is released, so you have no excuse not to run the update. Next, I will show you how to update safely so that the site can still run smoothly during and after the update.

Now, you should believe that keeping the site up to date will enhance its security.

# 2 compatibility

The theme is also updated to ensure compatibility with the latest version of WordPress. This can be done either immediately after the new version of WordPress is released or in a short period of time. Good theme developers test their themes against the new version of WordPress (and the development version at the time of creation) and make any necessary changes to ensure compatibility.

However, over time, new versions of WordPress will continue to be released, and compatibility issues may arise with your theme. In my experience, there are far fewer incompatibility issues with themes than plug-ins, but this is still something you need to pay attention to.

When updating a theme, you should first ensure that you are running the latest version of WordPress.

# 3 Features

The third reason to keep a theme up-to-date is to take full advantage of the new features that developers may add to the theme.

Themes update new features from time to time, either because users need additional functionality, or because themes can take advantage of the new features of WordPress itself (I’m talking about Gutenberg) and add additional functionality to the theme.

If your theme has new features to enhance your site, why not take advantage of it?

How to update topics in WordPress (two ways)

Updating theme is an important task in the daily maintenance of the website. Be sure to back up the site before running any updates.

Here is how to update a topic in WordPress:

  1. If your theme is not customized, go to appearance & gt; theme & gt; and click “Update now” on the active theme.
  2. If your theme contains custom code, updating the theme with the latest version will overwrite all your custom settings. To safely update a custom theme, use subtopics.

How to securely update WordPress themes

Updating a theme in WordPress is very simple. Just go to the dashboard & gt; to update and select the theme, and then click the Update theme button.

如何更新WordPress主题:安全而深入的指南插图1

Update theme

But wait! You might think you just need to click the update button, right? Of course, this updates the theme. But it is not safe to do so.

Each time you update any element of a WordPress site (theme, plug-in, or WordPress core), you should back up the site and test the updated software to check that everything is all right. To do this without touching your WordPress real-time site, you should first run updates on the WordPress staging version of your site.

Let’s take a closer look at how to do this.

Make a backup first

Be sure to back up your website before you update it. In this way, if the update causes the site to crash, you have something to restore the site.

If you follow the following advice and always test on the staging server first, you are less likely to encounter any problems: after all, you have already tested it. But safety is better than regret.

如何更新WordPress主题:安全而深入的指南插图2
The pagoda panel provides automatic backup for scheduled tasks.

If you do not use the pagoda and your hosting provider does not provide a simple backup of the website, then you need to use the backup plug-in. The free Updraft Plus plug-in allows you to run automatic and manual backups and is relatively easy to recover. Of course, many websites also provide snapshot service support, and it is also possible to generate a server snapshot before performing an update.

When to run the update

First of all, it is worth determining the best time to run the update. You want to minimize downtime or disruption to site visitors, which means that it is reasonable to run updates at the time of the day when the number of visitors is lowest (or on a certain day of the week).

Don’t think that this is the least likely time of day for you to use the site: if you have a large number of visitors from overseas, the number of visitors may surge while you are sleeping.

Use tools such as Google Analytics to determine the time when the site is least visited and schedule updates accordingly.

Once you have this information, you can schedule daily or weekly website updates. At this point, you don’t need to test on a staging site: you can test it at your convenience. However, updating to a real-time website will cause a certain amount of downtime, so schedule the update at the least impact time.

The advanced version of the Easy Updates Manager plug-in allows you to update automatically or manually.

如何更新WordPress主题:安全而深入的指南插图3

Easy Updates Manager plug-in

Use staging sites

Before updating a theme, you should always test whether the update works properly, conflicts with other code in the site, and whether the site still works properly after the update.

To do this, you need a copy of the site that is not visible to the public, and you can safely destroy it without any unnecessary consequences.

The best place to test is a temporary site. This is because the staging site is on the same server as the real site and is a copy of the real site. This means that any program running on a staging site will also run on your real-time site.

Test update

You have created a staging site and are ready to install theme updates and test them. What should we do next?

Please follow these steps.

  1. Check that the theme is compatible with your version of WordPress. If it is not compatible, you may need to update WordPress (which is always a top priority). Follow this test procedure to update, test the update using a staging site, and then push it to the live site.
  2. Update the theme in the “themes” interface of the WordPress management page by clicking the “Update” link under the theme name, and if you have purchased the theme from a third-party vendor, upload the new version.
  3. Test whether the staging version of the website is working properly. Test different content types (home page, single post and page, archive page, any custom post type or taxonomy). Check if there is a problem with the gadget or menu. Test processes such as form submission or purchase. The content you need to test depends on your site, but it must be comprehensive; if you browse the site quickly without testing all content types and processes, you may find that users find problems earlier than you do, probably when they buy or fill out the form.
  4. If the theme update causes the site to crash, check the topic’s support forum to see if there are any known problems. You can open the “themes” page in the administration interface and click on the topic to get the relevant information. Click the link to view the details of the version, and then find the View support Forum button in the right sidebar. See if anyone else has had a problem.
  5. If you have a problem that no one else has reported, please report it to the theme developer. This will help them solve the problem. Then, when they release a later version of the theme, you need to repeat the process and hope they can fix it.
  6. If all goes well, you can bring the staging site online because you have thoroughly tested it.

How to update a custom WordPress theme

But what if you have edited the theme and want to update it without losing the changes?

This is troublesome, but not impossible, as long as you remember what you modified, or, more specifically, which files you edited.

Use subtopics to avoid problems when updating custom WordPress topics

The safest way to update a custom WordPress theme is to create a subtheme and add your custom settings to it. If you are not sure whether your site uses subthemes, you can use our WordPress theme detection tool to check the site.

To do this, you need to create a subtheme with the edited theme file. Please follow these steps on your staging site (because if you do not operate 100% correctly, it may damage your real-time website).

  1. Back up the parent theme and copy it to the local computer. This means that you have a copy of the revised theme in case anything is left out and needs to be edited after updating the theme.
  2. Create a subtheme for an existing theme. Add a style sheet that contains the details of the parent theme (see above) and a function file that gets the style from the parent theme.
  3. Determine any changes you have made to the function file of the parent theme. Copy the new feature to the function file of the subtopic.
  4. Determine any changes you make to the parent theme stylesheet. Copy this code into the stylesheet of the subtheme. This overrides the style of the same element, class, or ID in the parent theme.
  5. Find the template file or include file that you have edited. Copy these files to a subtopic, and if the files are in a subdirectory, be sure to use the same file structure.
  6. Activate the child theme, not the parent theme.
  7. Test the website. Check any customizations you have made to the theme, especially the pages displayed using the template files you edited and copied.
  8. Check the documents carefully to make sure that no edited files are missing.
  9. To update the parent theme in the staging site, you can update it through the themes screen, or by uploading the theme if you do not get the theme from the theme directory.
  10. Test the site again. Make sure that any template files you edit work properly. If anything is missing, review the backup of the edited version of the parent theme and copy what you need to the child theme.
  11. Test again. When you are satisfied with the operation of the staging site, you can push the staging changes to the real-time site (make sure you back up the real-time site first). Test your real-time website, and then you can start.

After these operations are completed, there is no need to repeat them the next time you update the parent theme. But remember: if you need to make more changes in the future, make changes to the child theme, not the parent theme. Before making any changes, please prepare the subtheme and test all the content on the staging site. For complete completion, use a version control system such as Github.

如何更新WordPress主题:安全而深入的指南插图4

GitHub website

Update parent theme: make any changes to the child theme

Sometimes you may find that after updating the parent theme, the site crashes due to changes made to the child theme.

Maybe you attached a function to the hook of the parent theme, and the hook has been modified or even deleted. Maybe the template file of your subtheme is no longer consistent with the template file of the parent theme, which means that the pages generated by your subtheme are inconsistent with the rest of the site.

If this happens, you need to do one of two things:

  • Remove related files from the subtopic and stop using related files in the subtopic. Sometimes, the update of the parent theme means that it works better for you than the child theme, so you no longer need the file. Once you delete the files in the child theme, WordPress uses the files in the parent theme.
  • Edit the files in the subtheme so that they are consistent with the parent theme.

If you choose the first method above, be sure to test it on your staging site and keep a backup of the subtheme before editing. Similarly, using Github can help you keep abreast of changes.

If you need to edit the subtheme file using the second method, you need to be clear about what changes you have made, what changes have been made to the parent theme, and how they interact. It is necessary to compare the previous and new versions of the code for the parent theme and the child theme. In this way you can find out where the problem is and solve it.

Read the documentation for the parent topic of this release: the release notes in the theme library, or more detailed documentation on the developer’s website. This will help you identify any changes in the theme that affect subtopics.

Again, be sure to keep the backup and conduct a comprehensive test.

How to customize the WordPress theme to safely update it in the future

If you haven’t customized the theme yet, but are planning to do so, you can add your customization to the subtheme from the start, which will make things easier.

If you edit a theme directly from the theme catalog or theme vendor, any changes you make to the theme will be lost when you update the theme. The new theme file will overwrite the file you edited, and all your work will be wasted.

That’s why you shouldn’t edit third-party themes directly. The correct way to edit a third-party theme is to create a subtheme for that theme and add your edits to it.

Let’s take a closer look at the subtopics: how they work and how to use them to safely update (and edit) your custom WordPress themes.

Use subtopics to avoid problems when updating topics

A subtheme is a theme that runs the site in conjunction with the parent theme. It can add additional features, such as design elements and functions, to the parent theme, but it does not override the parent theme. Instead, specific files that you create in child themes will overwrite specific files with the same name in the parent theme. This, in conjunction with the WordPress template hierarchy, tells WordPress which theme template file to use to display a page on the site.

If you don’t know anything about the theme template file and are confused, let me give you an example.

Your existing theme has three files for displaying the archive page:

  • The category.php file, which is used to display the classified archive page.
  • Archive.php file, which will show all archive types unless there is a more specific file (such as category.php). This means that the file will be used for label archiving (for example), but not for category archiving.
  • Index.php file, which is a backup file for any content type that does not have a more specific file. In this case, because of the existence of the archive.php file, no index.php file will be used on any archive page.

When displaying archived files on a Web site, WordPress runs this hierarchy and selects the files that are displayed first. If it is classified archiving, it is category.php. If it is a tag archive, it is archive.php because the topic does not have an tag.php file.

But if you create a subtheme, WordPress always selects the file with the highest hierarchy in the subtheme, rather than the file in the parent theme.

That is, if you create a subtheme that has an archive.php file, the following occurs:

  • For classification, WordPress uses the category.php in the parent topic because it is higher in the hierarchy.
    For tags, WordPress uses the archive.php in the child topic because the file with the same name in the child topic overwrites the file with the same name in the parent theme.

This means that if you want to edit a file in a theme, just create a subtheme, copy the file from the parent theme to the subtheme, and then edit it. The original file in the parent theme will not be touched, so when you update the theme, you won’t lose any work. This also means that even after the update, WordPress uses the archive.php file in the child theme instead of the updated archive.php file in the parent theme.

This has two effects on the update theme:

  • Creating a subtheme means that you can update the parent theme without losing your edits.
  • When you update the parent theme, you need to check your code for changes that conflict with the child theme.

We will come to the second point soon. But how do you use subthemes to save any edits you might want to make to the theme? Let’s have a look together.

  1. Create your child theme, which requires a stylesheet and a function file from which you can call the style of the parent theme.
  2. Determine which files in the parent theme you want to edit.
  3. If the file you are editing is a feature file or stylesheet, do not copy them, but add new code to the stylesheet and feature file that you have created in the subtheme.
  4. If the theme you want to edit is a template file or an include file, copy these files in the child theme and copy them completely from the parent theme. Use the same file structure when copying, for example, if the circular file is in the parent theme folder / include file / loop.php, copy it to the child theme folder / include file / loop.php.
  5. Edit the file in the subtopic.
  6. Activate child topics instead of parent topics in the WordPress administration interface.

okay! The only extra work is to create subthemes, which are very simple and quick. Now, whenever you need to change the theme, you just need to edit the subtheme. In this way, when you update the parent theme, you won’t lose your job.

How to update the WordPress theme on the WooCommerce website

If you are using WooCommerce, you may have edited the theme to make it compatible with WooCommerce.

This is only necessary if you are using a version of WooCommerce prior to version 3. 3. The WooCommerce team’s advice is to upgrade the plug-in, and then you’ll find that you don’t need to add additional code to the theme to ensure compatibility.

But if for some reason you can’t upgrade to the current version of WooCommerce (which I don’t recommend), you need to copy your changes to a subtheme before updating the theme.

You need to take one of two ways:

  • If you add the woocommerce_content () function to the woocommerce.php file created in the theme, simply copy the file to the child theme and make the current theme the parent theme.
  • If you used the WooCommerce hook, copy the code from the theme’s function file to the sub-topic’s function file. Don’t copy the whole file, just copy the code.

More often, you add additional template files to the theme to overwrite WooCommerce’s template files. This applies to any version of WooCommerce.

If you do this, you need to create a subtheme and then copy these template files into your subtheme. Be sure to use the same file structure as the original topic. Then activate the child theme as described above, test and update the parent theme.

After completing the above steps, you need to conduct a more comprehensive test than a non-store website. In addition to more comprehensive testing of the site (see above), you should also test the operation of the store. Test the store main page, product category page, individual product page, and checkout process. After ensuring that everything is working properly, push the changes to the official website.

Summary

Now that you know why it’s important to update WordPress themes, the most important thing is to know how to update them according to different situations. A new theme will be more secure, compatible with the WordPress core, and benefit from any new features.

But it’s also important to update correctly. Before running the update, be sure to make a backup and test it on your staging site before you can push the changes to the live site.

If you make any changes to the theme, there will be extra work. First create a subtheme and add any changes you have made or need to make to it. This way, when you update the parent theme, your changes will not be lost.

Disclaimer: All articles on this website, unless otherwise specified or marked, are original and published on this website. Any individual or organization is prohibited from copying, stealing, collecting, or publishing the content of this site to any website, book, or other media platform without the consent of this site. If the content on this website infringes on the legitimate rights and interests of the original author, you can contact us for assistance.