问题
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.
With FTP go to the folder
wp-content
>>plugins
Inside the
plugins
folder create a new folder calledmy_protocol_filter
Inside of this new created folder, create a php file with the same name
my_protocol_filter.php
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'); ?>
- 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.
- 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