Skip to main contentSkip to footer

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

PSA: New ransom scam in town
We were threatened with a new ransom extortion message. I haven’t seen this one before, looks like it’s new but…
Indexing HTML comments

By default, Relevanssi does not index HTML comments inside your posts. Relevanssi removes all HTML tags before indexing, and HTML comments (<!– like this –>) are counted as HTML tags. If you have content inside comments that you want to be indexed, you need to modify the punctuation control –…

WooCommerce: Indexing product variation SKUs for main product

…) { foreach ( $variations as $variation ) { $sku = get_post_meta( $variation->ID, ‘_sku’, true ); $content .= ” $sku “; } } } return $content; } This will index the product variation SKUs for the main product, and you will be able to find the main product when searching…Relevanssi can index product and product variation SKUs for WooCommerce products: just add _sku to list of custom fields to index. However, if you want to find the main product when searching for the product variation SKU, you need some extra code using the relevanssi_content_to_index filter hook. Add this function…

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