Wordpress:添加到functions.php的代码在更新后消失 [英] Wordpress: code added to functions.php disappears after update
问题描述
我们的 Intranet 网站有一个链接,可从页面内打开 Windows 资源管理器窗口.Wordpress 更新后,此功能丢失.经过一些谷歌搜索后,我能够通过将以下代码添加到 functions.php 文件中来找到解决方案:
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');
几天前,我们的 Wordpress 网站进行了另一次更新,之后我注意到添加的功能再次被删除(可能被新版本的新 functions.php 文件覆盖).
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).
如何在functions.php 中添加一些东西,这样我就不必在每次更新时再次添加它?
How can I add something to functions.php so that I don't have to add it again with every new update that follows?
请注意,虽然我对 PHP 有所了解,但我没有 Wordpress 经验.
Please note that, though I know my way around PHP a little bit, I have no Wordpress experience.
推荐答案
一种方法是为您的主题创建一个子主题,当您更新主题时它不会被覆盖.
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.
使用 FTP 转到文件夹
wp-content
>>插件
在 plugins
文件夹内创建一个名为 my_protocol_filter
Inside the plugins
folder create a new folder called my_protocol_filter
在这个新创建的文件夹中,创建一个同名的php文件my_protocol_filter.php
Inside of this new created folder, create a php file with the same name my_protocol_filter.php
在这个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'); ?>
- 由于文件夹和文件位于 wordpress 安装(使用 FTP)的插件文件夹中,您现在可以在插件部分的 wordpress 后端找到该插件.
- 激活插件.无论您使用或更新什么主题,您的功能现在都可以使用.
这篇关于Wordpress:添加到functions.php的代码在更新后消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!