Posted on

Adding custom functions to a site

The Relevanssi knowledge base is full of code snippets and custom functions that you can use to enhance your site and to change how Relevanssi works. But where to place those snippets and functions?

There are many different methods of adding custom functions to your site, and I will go through them in this article.

Functions.php

The traditional method is to use the theme functions.php file. As the name suggests, this file exists for this purpose. WordPress will run everything you add here on every page load, both in the admin backend and front end.

With this approach, you need to be conscious of theme updates. If you have a theme that gets updates, it’s not good to modify the functions.php file. Updates to the theme will overwrite your modifications, making you lose all functionality added to the file.

In those cases, you need to use a child theme: the child theme inherits everything from the parent theme, and you can safely make changes to the theme files in the child theme. Updates to the parent theme won’t overwrite the changes in the child theme.

Creating child themes isn’t tricky, and some plugins help with the task.

Custom plugin

Another approach is to create a custom plugin to store all the modifications. Creating a plugin requires slightly more setup work, but adding new functions is just as easy as with the functions.php method once you have the custom plugin.

Using a plugin also has the added benefit of being independent of your theme. If you switch your theme, you don’t lose all the custom modifications you’ve done. Relevanssi custom functions are things you need, no matter which theme you use, so putting them in a plugin outside your theme makes sense.

You can find instructions for writing plugins on WordPress.org. The minimum plugin is straightforward: you can create a file called custom-functions.php in the /wp-content/plugins/ directory and put this in the file:

<?php
/**
 * Plugin Name: My custom functions
 */
  

Once you do that, “My custom functions” should show up in the WordPress Plugins list for you to activate, and then you can add custom functions to this file.

PHP snippets

Finally, the third approach uses a PHP snippet plugin that lets you add and edit PHP code snippets from the WP admin. My recommendation would be Code Snippets, a well-done plugin that does the job well. The plugin is easy to use, and it’s easy to manage the snippets and control how WordPress runs them.

4 comments Adding custom functions to a site

    1. Stefan, I’m not sure if the Woodmart live search uses Relevanssi. Better check that first; otherwise, nothing I say here will help you.

      With Relevanssi, one guaranteed method is using the relevanssi_hits_filter hook. With that hook, you get a list of all the results found. You can then sort the results into two buckets: sold items and available items. Then just put the available items first. There are plenty of examples in the documentation, and your web designer should be able to figure that out, it’s pretty simple array manipulation.

      1. I think he has added it to my site in addition to whatever standard one. I see it on the dashboard. I am not able to do this myself, do you know someone who can help me get it sorted? thanks

        1. Stefan, the templating may be custom, but underneath it’s the Woodmart ajax search. Any WordPress developer should be able to help you out with this. It’s fairly basic stuff, and even if someone doesn’t have any Relevanssi experience, the filter is simple and there are plenty of examples on this site.

Leave a Reply

Are you a Relevanssi Premium customer looking for support? Please use the Premium support form.

Your email address will not be published. Required fields are marked *