skip to Main Content

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.

2

Answers


  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 plugins folder of your wordpress installation (using FTP) you will now find the new plugin in your wordpress backend in the plugins section.
    2. Activate the plugin. Your function will now work, no matter what theme you are using or updating.
    Login or Signup to reply.
  2. this is just an update, I found that the best way to put custom javascript is directly through the page it self, in every page there is an editor choose text from it and put your javascript there like below, it work like a charm without causing any conflict.

    <script> your code here </script>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search