Skip to main contentSkip to footer

I need to add some custom shortcodes to the list of the “removed” ones (so they don’t show in plain text in the results).

However, some of them are built like [shortcode_name text="Need to keep this in results"] and I would like the content in the text parameter to stay in the results.

How do I go about this?

– Duke at Remove shortcode but keep content in X parameter

You can’t do this using the Relevanssi shortcode removal because that’s a binary operation: the whole shortcode is kept or removed, and there’s no way to keep only part of the shortcode.

However, you can do this with the relevanssi_post_content filter hook by removing the shortcode while keeping the attribute text. To get the same effect in excerpts, you can add the same filter function to the relevanssi_pre_excerpt_content filter hook.

add_filter( 'relevanssi_pre_excerpt_content', 'rlv_shortcode_attribute', 8 );
add_filter( 'relevanssi_post_content', 'rlv_shortcode_attribute', 8 );

/**
 * Indexes only the "text" attribute from the 

This is custom heading element

shortcode * * @param string $content Post content * * @return string Post content with the shortcode replaced with just the "text" attribute content. */ function rlv_shortcode_attribute( $content ) { return preg_replace( '/\

This is custom heading element

/im', '\1', $content ); }

Since this particular case was about a Visual Composer shortcode, it’s worth noticing the new filter must happen on priority 8 or earlier, because on priority 9, Relevanssi runs the relevanssi_remove_page_builder_shortcodes() function that removes all Visual Composer shortcodes, including this vc_custom_heading. As long as the new filter runs before the default function, there’s no problem.

Your account

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

Search

Popular Resources

Adding custom functions to a site

…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……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

WooCommerce: Popularity and price sorting

Many WooCommerce users use search sorting that allows users to sort by popularity or price. Unfortunately, while Relevanssi works fine with WooCommerce, those sorts do not work. Relevanssi doesn’t know about price or popularity, and the sorting assumes there’s a default WP search underneath. Relevanssi doesn’t do meta field sorting…as easily as the default WP search does. This function handles price, popularity and rating sorting: add_filter( ‘relevanssi_orderby’, ‘woocommerce_relevanssi_orderby’ ); function woocommerce_relevanssi_orderby( $orderby ) { if ( in_array( $orderby, array( ‘price’, ‘price-desc’ ), true ) ) { global $wp_query; $orderby = ‘meta_value_num’; $wp_query->query_vars[‘meta_key’] = ‘_price’; } if ( ‘price’ ===…

Showing the number of search results

…$wp_query->found_posts . ‘ hits.</p>’; ?> in your search results template. In case that produces no results, try introducing the global $wp_query variable like this: <?php global $wp_query; echo ‘<p>Found ‘ . $wp_query->found_posts . ‘ hits.</p>’; ?> The usual method which involves a new WP_Query object does not work with Relevanssi….

Related Posts:

Comment Section:

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