Relevanssi Shortcodes

Below, you’ll find a detailed guide on how to add the Relevanssi – A Better Search Shortcodes to your WordPress website, including their parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the Relevanssi – A Better Search Plugin shortcodes not to show or not to work correctly.

Before starting, here is an overview of the Relevanssi – A Better Search Plugin and the shortcodes it provides:

Plugin Icon
Relevanssi – A Better Search

"Relevanssi – A Better Search is a powerful WordPress plugin that significantly enhances the default search functionality, providing more relevant search results for your website visitors."

★★★★☆ (377) Active Installs: 100000+ Tested with: 6.3.2 PHP Version: 7.0
Included Shortcodes:
  • [search]
  • [noindex]
  • [relevanssi_search_form]

Relevanssi [search] Shortcode

The Relevanssi shortcode is a powerful tool for enhancing search functionality. This shortcode generates search links based on given terms. It accepts two parameters – ‘term’ and ‘phrase’. If ‘term’ is provided, it’s used as the search term; else, the content within the shortcode is used. If ‘phrase’ is not ‘not’, the term is treated as a phrase. The output is a link to the search results.

Shortcode: [search]

Parameters

Here is a list of all possible search shortcode parameters and attributes:

  • term – Specifies the search term to be used.
  • phrase – Defines whether the term should be treated as an exact phrase or not.

Examples and Usage

Basic example – A simple implementation of the shortcode to search for a specific term.

[search term="wordpress" /]

Advanced examples

Using the shortcode to search for a term and specify the phrase attribute. The search will be performed for the exact phrase as it is.

[search term="wordpress" phrase="exact" /]

Using the shortcode without specifying a term. The search will be performed for the content of the shortcode.

[search]wordpress[/search]

Using the shortcode without specifying a term and with the phrase attribute. The search will be performed for the exact phrase of the content of the shortcode.

[search phrase="exact"]wordpress[/search]

PHP Function Code

In case you have difficulties debugging what causing issues with [search] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode( 'search', 'relevanssi_shortcode' );

Shortcode PHP function:

function relevanssi_shortcode( $atts, $content ) {
	$attributes = shortcode_atts(
		array(
			'term'   => false,
			'phrase' => 'not',
		),
		$atts
	);

	$term   = $attributes['term'];
	$phrase = $attributes['phrase'];

	if ( false !== $term ) {
		$term = rawurlencode( relevanssi_strtolower( $term ) );
	} else {
		$term = rawurlencode( wp_strip_all_tags( relevanssi_strtolower( $content ) ) );
	}

	if ( 'not' !== $phrase ) {
		$term = '%22' . $term . '%22';
	}

	$link = get_bloginfo( 'url' ) . "/?s=$term";
	$pre  = "<a rel='nofollow' href='$link'>"; // rel='nofollow' for Google.
	$post = '</a>';

	return $pre . do_shortcode( $content ) . $post;
}

Code file location:

relevanssi/relevanssi/lib/shortcodes.php

Relevanssi [noindex] Shortcode

The Relevanssi Noindex shortcode is a tool that allows the execution of nested shortcodes within content. It ensures seamless content processing.

Shortcode: [noindex]

Examples and Usage

Basic example – A simple usage of the ‘noindex’ shortcode from the Relevanssi plugin. It simply wraps the content that you don’t want to be indexed by search engines.

[noindex] Content to be excluded from indexing [/noindex]

Advanced examples

Using the noindex shortcode to exclude a specific section of the content from being indexed. This is useful when you have a large piece of content and only want to exclude a specific part from being indexed.

[noindex] Specific section of the content to be excluded from indexing [/noindex]

Using the noindex shortcode in conjunction with other shortcodes. In this example, we are using the ‘contact-form’ shortcode within the ‘noindex’ shortcode. This will prevent the contact form from being indexed by search engines.

[noindex] [contact-form id="1"] [/noindex]

PHP Function Code

In case you have difficulties debugging what causing issues with [noindex] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode( 'noindex', 'relevanssi_noindex_shortcode' );

Shortcode PHP function:

function relevanssi_noindex_shortcode( $atts, $content ) {
	return do_shortcode( $content );
}

Code file location:

relevanssi/relevanssi/lib/shortcodes.php

Relevanssi [relevanssi_search_form] Shortcode

The Relevanssi plugin shortcode enables advanced search functionality in WordPress. It allows for customization of search forms, including additional fields like checkboxes, dropdowns, and post types. This shortcode generates a search form, which can be further customized using attributes. For example, you can add dropdown menus for post types or categories, or checkboxes for tags. The shortcode also supports hidden fields for additional customization.

Shortcode: [relevanssi_search_form]

Parameters

Here is a list of all possible relevanssi_search_form shortcode parameters and attributes:

  • dropdown – defines a dropdown field in the search form
  • checklist – creates a checklist field in the form
  • post_type_boxes – generates checkboxes for selecting post types
  • post_type – used to create a dropdown for selecting a post type
  • category – used to create a dropdown for selecting a category
  • post_tag – used to create a dropdown for selecting a tag

Examples and Usage

Basic Example: – Utilizes the relevanssi_search_form shortcode to generate a simple search form.

[searchform]

Advanced Examples:

1. Utilizes the relevanssi_search_form shortcode to generate a search form with a dropdown menu for post types.

[searchform dropdown_post_type="post_type"]

2. Uses the relevanssi_search_form shortcode to generate a search form with a checklist of categories.

[searchform checklist_category="category"]

3. Implements the relevanssi_search_form shortcode to generate a search form that includes a dropdown for post types and a checklist for categories.

[searchform dropdown_post_type="post_type" checklist_category="category"]

4. Utilizes the relevanssi_search_form shortcode to generate a search form that includes a dropdown for post types, a checklist for categories, and a hidden field for custom attributes.

[searchform dropdown_post_type="post_type" checklist_category="category" hidden_custom="custom_value"]

PHP Function Code

In case you have difficulties debugging what causing issues with [relevanssi_search_form] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode( 'searchform', 'relevanssi_search_form' );

Shortcode PHP function:

function relevanssi_search_form( $atts ) {
	$form = get_search_form( false );
	if ( is_array( $atts ) ) {
		$additional_fields = array();
		foreach ( $atts as $key => $value ) {
			if ( 'dropdown' === substr( $key, 0, 8 ) ) {
				$key = 'dropdown';
			}
			if ( 'checklist' === substr( $key, 0, 9 ) ) {
				$key = 'checklist';
			}
			if ( 'post_type_boxes' === $key ) {
				$post_types = explode( ',', $value );
				if ( is_array( $post_types ) ) {
					$post_type_objects   = get_post_types( array(), 'objects' );
					$additional_fields[] = '<div class="post_types"><strong>Post types</strong>: ';
					foreach ( $post_types as $post_type ) {
						$checked = '';
						if ( '*' === substr( $post_type, 0, 1 ) ) {
							$post_type = substr( $post_type, 1 );
							$checked   = ' checked="checked" ';
						}
						if ( isset( $post_type_objects[ $post_type ] ) ) {
							$additional_fields[] = '<span class="post_type post_type_' . $post_type . '">'
							. '<input type="checkbox" name="post_types[]" value="' . $post_type . '"' . $checked . '/> '
							. $post_type_objects[ $post_type ]->name . '</span>';
						}
					}
					$additional_fields[] = '</div>';
				}
			} elseif ( 'dropdown' === $key && 'post_type' === $value ) {
				$field = '<select name="post_type">';
				$types = get_option( 'relevanssi_index_post_types', array() );
				if ( ! is_array( $types ) ) {
					$types = array();
				}
				foreach ( $types as $type ) {
					if ( post_type_exists( $type ) ) {
						$object = get_post_type_object( $type );
						$field .= '<option value="' . $type . '">' . $object->labels->singular_name . '</option>';
					}
				}
				$field              .= '</select>';
				$additional_fields[] = $field;

			} elseif ( 'dropdown' === $key && 'post_type' !== $value ) {
				$name = $value;
				if ( 'category' === $value ) {
					$name = 'cat';
				}
				if ( 'post_tag' === $value ) {
					$name = 'tag';
				}
				$args                = array(
					'taxonomy'          => $value,
					'echo'              => 0,
					'hide_if_empty'     => true,
					'show_option_none'  => __( 'None' ),
					'name'              => $name,
					'option_none_value' => 0,
				);
				$additional_fields[] = wp_dropdown_categories( $args );
			} elseif ( 'checklist' === $key && 'post_type' !== $value ) {
				$name = $value;
				if ( 'category' === $value ) {
					$name = 'cat';
				}
				if ( 'post_tag' === $value ) {
					$name = 'tag';
				}
				$args = array(
					'taxonomy' => $value,
					'echo'     => 0,
				);
				if ( ! function_exists( 'wp_terms_checklist' ) ) {
					include ABSPATH . 'wp-admin/includes/template.php';
				}
				$checklist           = wp_terms_checklist( 0, $args );
				$checklist           = str_replace( 'post_category', 'cats', $checklist );
				$checklist           = str_replace( 'tax_input[post_tag]', 'tags', $checklist );
				$checklist           = str_replace( "disabled='disabled'", '', $checklist );
				$checklist           = preg_replace( '/tax_input\[(.*?)\]/', '\1', $checklist );
				$additional_fields[] = $checklist;
			} else {
				$key   = esc_attr( $key );
				$value = esc_attr( $value );

				$additional_fields[] = "<input type='hidden' name='$key' value='$value' />";
			}
		}
		$form = str_replace( '</form>', implode( "\n", $additional_fields ) . '</form>', $form );
	}
	/**
	 * Filters the Relevanssi shortcode search form before it's used.
	 *
	 * @param string $form The form HTML code.
	 * @param array  $atts The shortcode attributes.
	 */
	return apply_filters( 'relevanssi_search_form', $form, $atts );
}

Code file location:

relevanssi/relevanssi/lib/shortcodes.php

Conclusion

Now that you’ve learned how to embed the Relevanssi – A Better Search Plugin shortcodes, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.

Comments

Leave a Reply

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