Search Regex Shortcode

Below, you’ll find a detailed guide on how to add the Search Regex Shortcode to your WordPress website, including its parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the Search Regex Plugin shortcode not to show or not to work correctly.

Before starting, here is an overview of the Search Regex Plugin and the shortcodes it provides:

Plugin Icon
Search Regex

"Search Regex is a robust WordPress plugin. It allows you to search and replace data on your website with ease. It's perfect for large-scale edits and data management."

★★★★☆ (87) Active Installs: 200000+ Tested with: 6.3.2 PHP Version: 5.6
Included Shortcodes:
  • [search_regex]

Search Regex [search_regex] Shortcode

The Search-Regex plugin shortcode is used to perform various operations on content. It employs different cases to manipulate text, get date, and retrieve column data. The ‘md5’ case returns an md5 hash of the content. The ‘upper’ and ‘lower’ cases transform the text to uppercase and lowercase respectively. The ‘dashes’ and ‘underscores’ cases replace spaces or underscores/dashes with dashes or underscores. The ‘date’ case returns the current date in a specified format. The ‘value’ case returns the row value while the ‘column’ case retrieves column data based on the attribute ‘name’. The shortcode also applies filters to the ‘searchregex_do_shortcode’ hook, allowing further customization.

Shortcode: [search_regex]

Parameters

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

  • md5 – returns the MD5 hash of the shortcode content
  • upper – converts the shortcode content to uppercase
  • lower – converts the shortcode content to lowercase
  • dashes – replaces underscores and spaces with dashes in the content
  • underscores – replaces dashes and spaces with underscores in the content
  • date – displays the current date, format can be specified
  • value – returns the value of the current row
  • column – returns the value of a specified column in the current row

Examples and Usage

Basic example – The following shortcode will convert the enclosed content to uppercase letters:

[upper]Hello World![/upper]

Advanced examples

Convert the enclosed content to MD5 hash:

[md5]Hello World![/md5]

Replace spaces and underscores in the enclosed content with dashes:

[dashes]Hello_World![/dashes]

Display the current date in a specified format:

[date format="Y-m-d"]

Display the value of a specific column in the database:

[column name="column_name"]

Display the value of a joined column in the database:

[column name="joined_column_name"]

PHP Function Code

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

Shortcode line:

add_shortcode( $code, [ $this, 'do_shortcode' ] );

Shortcode PHP function:

function do_shortcode( $attrs, $content, $tag ) {
		if ( $this->schema === null ) {
			return '';
		}

		$this->level++;
		if ( $this->level > self::LOOP_MAX ) {
			return '';
		}

		switch ( $tag ) {
			case 'md5':
				return md5( do_shortcode( $content ) );

			case 'upper':
				return strtoupper( do_shortcode( $content ) );

			case 'lower':
				return strtolower( do_shortcode( $content ) );

			case 'dashes':
				return str_replace( [ '_', ' ' ], '-', do_shortcode( $content ) );

			case 'underscores':
				return str_replace( [ '-', ' ' ], '_', do_shortcode( $content ) );

			case 'date':
				return date( isset( $attrs['format'] ) ? $attrs['format'] : 'r' );

			case 'value':
				return $this->row_value;

			case 'column':
				$name = '';

				if ( isset( $attrs['name'] ) ) {
					$name = $attrs['name'];
				} elseif ( isset( $attrs[0] ) ) {
					$name = $attrs[0];
				}

				if ( $name && isset( $this->raw[ $name ] ) ) {
					$schema = $this->schema->get_column( $name );

					if ( $schema ) {
						return $this->get_schema_value( $schema, $attrs, $this->raw[ $name ] );
					}

					return $this->raw[ $name ];
				}

				$schema = $this->schema->get_column( $name );
				if ( $schema && $schema->get_join_column() ) {
					return $this->get_schema_join( $schema, $this->row_id, $attrs );
				}

				return '';
		}

		/**
		 * @psalm-suppress TooManyArguments
		 */
		return apply_filters( 'searchregex_do_shortcode', '', $tag, $attrs, $content );
	}

Code file location:

search-regex/search-regex/includes/action/class-dynamic-column.php

Conclusion

Now that you’ve learned how to embed the Search Regex Plugin shortcode, 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 *