Skip to main contentSkip to footer

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.

Your account

Not logged in. Log in to see your license details.

Search

Popular Resources

Moving the admin search page

Jules Colle came up with a method of moving the Relevanssi admin search page from under the Dashboard heading to the top level of the admin menu. add_action( ‘admin_menu’, function () { remove_submenu_page( ‘index.php’, ‘relevanssi_admin_search’ ); add_menu_page( ‘Search all content’, ‘Search all content’, ‘manage_options’, ‘relevanssi_admin_search’, ‘relevanssi_admin_search_page’, ‘dashicons-search’, 2 ); },……99 ); This function adds an admin_menu action that first uses remove_submenu_page() to remove the existing admin search function and then add_menu_page() to add it again on the top menu level. This works well except for one thing. The Relevanssi admin javascript is added only to specific pages, and the…new location of the admin search page is not one of those. This problem was solved in version 2.13 (Premium) and 4.11 (free version), when the filter hook relevanssi_acceptable_hooks was introduced. This filter hook lets you add new admin page hooks where Relevanssi javascript is used, so we can introduce…

Search shortcode

Relevanssi adds a shortcode to help making links to search results. That way users can easily find more information about a given subject from your blog. The syntax is simple: John Doe This will make the text John Doe a link to search results for John Doe. In case you……want to link to some other search term than the anchor text (necessary in languages like Finnish), you can use: Mr. John Doe Now the search will be for John Doe, but the anchor says Mr. John Doe. One more parameter: setting will wrap the search term in quotation marks,…

Related Posts:

Currently there are no related posts available.

Comment Section:

4 Comments. Leave new

  • hi I am trying to work out how to get customers searches for items displaying the live (for sale) items on my site before the sold items, but I cannot figure out how to do it , and my web designer has washed his hands of it. can you help?

    http://www.ashokaarts.com

    regards Stefan

    Reply
    • 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.

      Reply
      • STEFAN DOMONEY
        December 13, 2022 3:45 pm

        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

        Reply
        • 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.

          Reply

Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed