Visitors Online Shortcode

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

Before starting, here is an overview of the Visitors Online by BestWebSoft Plugin and the shortcodes it provides:

Plugin Icon
Visitors Online by BestWebSoft

"Visitors Online by BestWebSoft is a dynamic WordPress plugin that allows you to monitor and track your website visitors in real-time, giving you valuable insights into user behavior."

★★★★✩ (9) Active Installs: 2000+ Tested with: 6.3.2 PHP Version: false
Included Shortcodes:
  • [vstrsnln_info]

Visitors Online [vstrsnln_info] Shortcode

The Visitors Online plugin shortcode is used to display real-time visitor information on your WordPress site. It fetches data like the number of users, guests, and bots currently active. This shortcode collects and displays data about the maximum number of visits, users, guests, and bots. It also provides geographical and browser information of the visitors. Shortcode: [vstrsnln_info_display]

Shortcode: [vstrsnln_info]

Examples and Usage

Basic example – Displaying visitors’ details using the shortcode.

[vstrsnln_info /]

Under the title ‘Advanced examples’:

Displaying visitors’ details with the option to specify whether it’s a widget or not. If ‘is_widget’ is set to true, the details will be displayed in a widget area. If it’s set to false or not specified, the details will be displayed in the content area.

[vstrsnln_info is_widget=true /]

Here’s another advanced example where ‘is_widget’ is set to false, meaning the visitors’ details will be displayed in the content area.

[vstrsnln_info is_widget=false /]

Please note that the shortcode ‘vstrsnln_info’ doesn’t support any other parameters/attributes. The ‘is_widget’ attribute is a parameter in the PHP function and not an attribute in the shortcode itself. However, you can modify the function to accept more parameters and use them in the shortcode.

PHP Function Code

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

Shortcode line:

add_shortcode( 'vstrsnln_info', 'vstrsnln_info_display' );

Shortcode PHP function:

function vstrsnln_info_display( $is_widget = false ) {
		global $wpdb, $vstrsnln_options;
		$vstrsnln_blog_id		= get_current_blog_id();
		$vstrsnln_content		= $vstrsnln_options['structure_pattern'];
		$vstrsnln_date_today	= date( 'Y-m-d', time() );

		/* get realtime info */
		$vstrsnln_type_user	= $wpdb->get_row( "
			SELECT COUNT( * ) AS 'guest',
				( SELECT COUNT( * ) FROM `" . $wpdb->base_prefix . "vstrsnln_detailing`
				LEFT JOIN `" . $wpdb->base_prefix . "bws_list_countries` ON `" . $wpdb->base_prefix . "bws_list_countries` . `id`=`" . $wpdb->base_prefix . "vstrsnln_detailing` . `country_id`
					WHERE `date_connection` = '" . $vstrsnln_date_today . "'
						AND `user_type` = 'bot'
						AND `time_off` IS NULL
						AND `blog_id` = '" . $vstrsnln_blog_id . "'
				) AS 'bot',
				( SELECT COUNT( * ) FROM `" . $wpdb->base_prefix . "vstrsnln_detailing`
				LEFT JOIN `" . $wpdb->base_prefix . "bws_list_countries` ON `" . $wpdb->base_prefix . "bws_list_countries` . `id`=`" . $wpdb->base_prefix . "vstrsnln_detailing` . `country_id`
					WHERE `date_connection` = '" . $vstrsnln_date_today . "'
						AND `user_type` = 'user'
						AND `time_off` IS NULL
						AND `blog_id` = '" . $vstrsnln_blog_id . "'
				) AS 'user'
			FROM `" . $wpdb->base_prefix . "vstrsnln_detailing`
			LEFT JOIN `" . $wpdb->base_prefix . "bws_list_countries` ON `" . $wpdb->base_prefix . "bws_list_countries` . `id`=`" . $wpdb->base_prefix . "vstrsnln_detailing` . `country_id`
			WHERE `date_connection` = '" . $vstrsnln_date_today . "'
			AND `user_type` = 'guest'
			AND `time_off` IS NULL
			AND `blog_id` = '" . $vstrsnln_blog_id . "'
			LIMIT 1"
		);

		$table_general = vstrsnln_get_table_general( $vstrsnln_blog_id );

		/* preparing data for placeholder replacing */
		$replacements = array (
			'total'		=> 0, /* total count will be calculated later */
			'users'		=> $vstrsnln_type_user->user,
			'guests'	=> $vstrsnln_type_user->guest,
			'bots'		=> $vstrsnln_type_user->bot,
		);

		/* get total "realtime visitors" count */
		if ( strpos( $vstrsnln_content, '{USERS}' ) ) {
			$replacements['total'] += $vstrsnln_type_user->user;
		}

		if ( strpos( $vstrsnln_content, '{GUESTS}' ) ) {
			$replacements['total'] += $vstrsnln_type_user->guest;
		}

		if ( strpos( $vstrsnln_content, '{BOTS}' ) ) {
			$replacements['total'] += $vstrsnln_type_user->bot;
		}

		/* add new replacements */
		if ( ! empty( $table_general ) ) {
			$max_replacements = array (
				'max-date'		=> $table_general->date_connection,
				'max-total'		=> $table_general->number_visits,
				'max-users'		=> $table_general->number_users,
				'max-guests'	=> $table_general->number_guests,
				'max-bots'		=> $table_general->number_bots
			);

			/* get total "max visitors" count */
			if ( ! strpos( $vstrsnln_content, '{MAX-USERS}' ) ) {
				$max_replacements['max-total'] -= $table_general->number_users;
			}

			if ( ! strpos( $vstrsnln_content, '{MAX-GUESTS}' ) ) {
				$max_replacements['max-total'] -= $table_general->number_guests;
			}

			if ( ! strpos( $vstrsnln_content, '{MAX-BOTS}' ) ) {
				$max_replacements['max-total'] -= $table_general->number_bots;
			}

			/* add geo replacements */
			$geo_replacements = array();

			if ( ! empty( $table_general->country_id ) ) {
                $geo_replacements['country'] = $wpdb->get_var( $wpdb->prepare( "
                    SELECT `country_name`
                    FROM `" . $wpdb->base_prefix . "bws_list_countries`
                    WHERE `id` = %d",
                    $table_general->country_id
                ) );
            } else {
             $geo_replacements['country'] = __( 'No data', 'visitors-online' );
            }

			$geo_replacements['browser'] =
				! empty( $table_general->browser ) ?
				$table_general->browser :
				__( 'No data', 'visitors-online' );

			$replacements = array_merge( $replacements, $geo_replacements );
		} else {
			/* max replacements = realtime replacements if $table_general is empty (at first day, you install the plugin) */
			$max_replacements = array(
				'max-date'		=> date( 'Y-m-d' ),
				'max-total'		=> $replacements['total'],
				'max-users'		=> $replacements['users'],
				'max-guests'	=> $replacements['guests'],
				'max-bots'		=> $replacements['bots'],
				'country'		=> __( 'No data', 'visitors-online' ),
				'browser'		=> __( 'No data', 'visitors-online' )
			);
		}

		$replacements = array_merge( $replacements, $max_replacements );

		/*
		* process patterns with foreach
		* it helps us to change brackets (when it is needed) and add PRRE modifiers
		*/
		$patterns = array();
		foreach ( $replacements as $key => $value )
			$patterns[] = '~{' . $key . '}~i'; /* "i" should be used to make the expression case-insensitive */

		$vstrsnln_content = preg_replace( $patterns, array_values( $replacements ), $vstrsnln_content );

		$vstrsnln_content = '<div class="vstrsnln-block">' . wpautop( $vstrsnln_content ) . '</div>';
		$vstrsnln_content = apply_filters( 'vstrsnln_content', $vstrsnln_content );

		if ( is_admin() || true == $is_widget ) {
			echo $vstrsnln_content;
		} else {
			return $vstrsnln_content;
		}
	}

Code file location:

visitors-online/visitors-online/visitors-online.php

Conclusion

Now that you’ve learned how to embed the Visitors Online by BestWebSoft 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 *