Wordpress: code added to functions.php disappears after update

浪子不回头ぞ 提交于 2021-01-29 07:20:29

问题


Our intranet website has a link which opens a Windows Explorer window from within the page. After a Wordpress update, this functionality was lost. After some Googling I was able to find a solution by adding the following code to the functions.php file:

function allowed_link_protocols_filter($protocols)
{
     $protocols[] = 'file';
     return $protocols;
}
add_filter('kses_allowed_protocols', 'allowed_link_protocols_filter');

A few days ago, our Wordpress website got another update after which I noticed that the added functionality was removed again (probably overwritten by the new functions.php file of the new version).

How can I add something to functions.php so that I don't have to add it again with every new update that follows?

Please note that, though I know my way around PHP a little bit, I have no Wordpress experience.


回答1:


One way would be to create a child theme of your theme, which will not be overwritten when you update the theme.

But if you just want to add a single function, I would suggest creating a plugin.

  1. With FTP go to the folder wp-content >> plugins

  2. Inside the plugins folder create a new folder called my_protocol_filter

  3. Inside of this new created folder, create a php file with the same name my_protocol_filter.php

  4. Inside of this php file you have to paste the following code

       <?php /*
       Plugin Name: My custom protocol filter
       Description: Allowed link protocol filter
       Author: Joe
       Version: 1.0
       */
    

The comment defines the name of your plugin. Below that you paste your code

function allowed_link_protocols_filter($protocols)
{
     $protocols[] = 'file';
     return $protocols;
}
add_filter('kses_allowed_protocols', 'allowed_link_protocols_filter'); ?>
  1. As the folder and the file is in the plugin folder of your wordpress installation (using FTP) you will now find the plugin in your wordpress backend in the plugin section.
  2. Active the plugin. Your function will now work, no matter what theme you are using or updating.


来源:https://stackoverflow.com/questions/64754090/wordpress-code-added-to-functions-php-disappears-after-update

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!