Insert Html Snippet Shortcode

Below, you’ll find a detailed guide on how to add the Insert Html Snippet 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 Insert Html Snippet Plugin shortcode not to show or not to work correctly.

Before starting, here is an overview of the Insert Html Snippet Plugin and the shortcodes it provides:

Plugin Icon
Insert Html Snippet

"Insert Html Snippet is a WordPress plugin that offers a simple way to embed HTML code snippets into your posts, pages, or widgets. It's efficient and user-friendly for both beginners and advanced users."

★★★★☆ (219) Active Installs: 20000+ Tested with: 5.9.8 PHP Version: false
Included Shortcodes:
  • [xyz-ihs]

Insert Html Snippet [xyz-ihs] Shortcode

The ‘xyz-ihs’ shortcode from the Insert HTML Snippet plugin is designed to display specific content. The function ‘xyz_ihs_display_content’ retrieves and displays the content of a named snippet from the database, only if its status is active.

Shortcode: [xyz-ihs]

Parameters

Here is a list of all possible xyz-ihs shortcode parameters and attributes:

  • snippet – the name of the HTML snippet to be displayed

Examples and Usage

Basic example – In the following example, the shortcode is used to display a snippet named ‘example1’. The shortcode will fetch and display the HTML snippet stored under the name ‘example1’ in the database.

[xyz-ihs snippet="example1"]

Advanced examples

The shortcode can be used to display different HTML snippets based on certain conditions. In the following example, the shortcode is used to display different HTML snippets based on the user’s role. If the user is an administrator, the snippet ‘adminSnippet’ is displayed. If the user is a subscriber, the snippet ‘subscriberSnippet’ is displayed. If the user does not have any of these roles, no snippet is displayed.


if (current_user_can('administrator')) {
    echo do_shortcode('[xyz-ihs snippet="adminSnippet"]');
} elseif (current_user_can('subscriber')) {
    echo do_shortcode('[xyz-ihs snippet="subscriberSnippet"]');
}

Another advanced usage of the shortcode is to display a snippet only if it is active. In the following example, the snippet ‘example2’ is displayed only if it is active. If the snippet is not active, no content is displayed.


$result = $wpdb->get_var($wpdb->prepare( "SELECT status FROM ".$wpdb->prefix."xyz_ihs_short_code WHERE title=%s", 'example2'));
if($result == 1) {
    echo do_shortcode('[xyz-ihs snippet="example2"]');
}

PHP Function Code

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

Shortcode line:

add_shortcode('xyz-ihs','xyz_ihs_display_content');

Shortcode PHP function:

function xyz_ihs_display_content($xyz_snippet_name){
	global $wpdb;
	
	if(is_array($xyz_snippet_name)&& isset($xyz_snippet_name['snippet'])){
	   
		$snippet_name = $xyz_snippet_name['snippet'];
		
		$query = $wpdb->get_results($wpdb->prepare( "SELECT * FROM ".$wpdb->prefix."xyz_ihs_short_code WHERE title=%s" ,$snippet_name));
		
		if(count($query)>0){
			
			foreach ($query as $sippetdetails){
			if($sippetdetails->status==1)
				return do_shortcode($sippetdetails->content) ;
			else 
				return '';
				break;
			}
			
		}else{

			return '';		
		}
		
	}
}

Code file location:

insert-html-snippet/insert-html-snippet/shortcode-handler.php

Conclusion

Now that you’ve learned how to embed the Insert Html Snippet 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 *