Import Social Events Shortcode

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

Before starting, here is an overview of the Import Social Events Plugin and the shortcodes it provides:

Plugin Icon
Import Social Events

"Import Social Events is a WordPress plugin that seamlessly integrates your social events from Facebook into your website, ensuring your audiences never miss an update."

★★★★☆ (103) Active Installs: 5000+ Tested with: 6.3.2 PHP Version: 5.3
Included Shortcodes:
  • [facebook_events]

Import Social Events [facebook_events] Shortcode

The Import Facebook Events plugin shortcode enables users to display Facebook events on their website. It includes attributes for layout, post per page, category, past events, order, and date range.

Shortcode: [facebook_events]

Parameters

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

  • layout – defines the layout style for the events
  • col – sets the number of columns for the events layout
  • posts_per_page – determines the number of events displayed per page
  • category – allows filtering of events by specific categories
  • past_events – enables display of past events when set to “yes”
  • order – sets the ordering of events, either descending or ascending
  • orderby – determines the parameter to order events by
  • start_date – sets the start date for events to be displayed
  • end_date – sets the end date for events to be displayed

Examples and Usage

Basic example – Display Facebook events using the default parameters set in the plugin settings.

[facebook_events]

Advanced examples

Display Facebook events in a specific layout, with a specified number of posts per page, and from specific categories.

[facebook_events layout="style2" col='2' posts_per_page='12' category="cat1,cat2"]

Show past Facebook events in descending order.

[facebook_events past_events="yes" order="desc"]

Display Facebook events that fall within a specific date range.

[facebook_events start_date="2022-01-01" end_date="2022-12-31"]

Show Facebook events from a specific category, ordered by the event start date in ascending order.

[facebook_events category="cat1" orderby="event_start_date" order="asc"]

PHP Function Code

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

Shortcode line:

add_shortcode( 'facebook_events', array( $this, 'facebook_events_archive' ) );

Shortcode PHP function:

function facebook_events_archive( $atts = array() ) {
		// phpcs:ignore Squiz.PHP.CommentedOutCode.Found
		// [facebook_events layout="style2" col='2' posts_per_page='12' category="cat1,cat2" past_events="yes" order="desc" orderby="" start_date="" end_date="" ]
		$current_date = current_time( 'timestamp' );
		$paged        = ( get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1 );
		if ( is_front_page() ) {
			$paged = ( get_query_var( 'page' ) ? get_query_var( 'page' ) : 1 );
		}
		$eve_args = array(
			'post_type'   => 'facebook_events',
			'post_status' => 'publish',
			'meta_key'    => 'start_ts', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key --Ignore.
			'paged'       => $paged,
		);

		// post per page.
		if ( isset( $atts['posts_per_page'] ) && ! empty( $atts['posts_per_page'] ) && is_numeric( $atts['posts_per_page'] ) ) {
			$eve_args['posts_per_page'] = $atts['posts_per_page'];
		}

		// Past Events.
		if ( ( isset( $atts['start_date'] ) && ! empty( $atts['start_date'] ) ) || ( isset( $atts['end_date'] ) && ! empty( $atts['end_date'] ) ) ) {
			$start_date_str = '';
			$end_date_str   = '';
			if ( isset( $atts['start_date'] ) && ! empty( $atts['start_date'] ) ) {
				try {
					$start_date_str = strtotime( sanitize_text_field( $atts['start_date'] ) );
				} catch ( Exception $e ) {
					$start_date_str = '';
				}
			}
			if ( isset( $atts['end_date'] ) && ! empty( $atts['end_date'] ) ) {
				try {
					$end_date_str = strtotime( sanitize_text_field( $atts['end_date'] ) );
				} catch ( Exception $e ) {
					$end_date_str = '';
				}
			}

			if ( ! empty( $start_date_str ) && ! empty( $end_date_str ) ) {
				$eve_args['meta_query'] = array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
					'relation' => 'AND',
					array(
						'key'     => 'end_ts',
						'compare' => '>=',
						'value'   => $start_date_str,
					),
					array(
						'key'     => 'end_ts',
						'compare' => '<=',
						'value'   => $end_date_str,
					),
				);
			} elseif ( ! empty( $start_date_str ) ) {
				$eve_args['meta_query'] = array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
					array(
						'key'     => 'end_ts',
						'compare' => '>=',
						'value'   => $start_date_str,
					),
				);
			} elseif ( ! empty( $end_date_str ) ) {
				$eve_args['meta_query'] = array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
					'relation' => 'AND',
					array(
						'key'     => 'end_ts',
						'compare' => '>=',
						'value'   => strtotime( date( 'Y-m-d' ) ),
					),
					array(
						'key'     => 'end_ts',
						'compare' => '<=',
						'value'   => $end_date_str,
					),
				);
			}
		} else {
			if( isset( $atts['past_events'] ) && $atts['past_events'] === true ){
				$atts['past_events'] = "yes";
			}
			if ( isset( $atts['past_events'] ) && 'yes' === $atts['past_events'] ) {
				$eve_args['meta_query'] = array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
					array(
						'key'     => 'end_ts',
						'compare' => '<=',
						'value'   => $current_date,
					),
				);
			} else {
				$eve_args['meta_query'] = array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
					array(
						'key'     => 'end_ts',
						'compare' => '>=',
						'value'   => $current_date,
					),
				);
			}
		}

		// Category.
		if ( isset( $atts['category'] ) && ! empty( $atts['category'] ) ) {
			$categories = explode( ',', $atts['category'] );
			$tax_field  = 'slug';
			if ( is_numeric( implode( '', $categories ) ) ) {
				$tax_field = 'term_id';
			}
			if ( ! empty( $categories ) ) {
				$eve_args['tax_query'] = array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query
					array(
						'taxonomy' => $this->event_category,
						'field'    => $tax_field,
						'terms'    => $categories,
					),
				);
			}
		}

		// Order by.
		if ( isset( $atts['orderby'] ) && ! empty( $atts['orderby'] ) ) {
			if ( 'event_start_date' === $atts['orderby'] || 'event_end_date' === $atts['orderby'] ) {
				if ( 'event_end_date' === $atts['orderby'] ) {
					$eve_args['meta_key'] = 'end_ts'; // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key --Ignore.
				}
				$eve_args['orderby'] = 'meta_value';
			} else {
				$eve_args['orderby'] = sanitize_text_field( $atts['orderby'] );
			}
		} else {
			$eve_args['orderby'] = 'meta_value';
		}

		// Order.
		if ( isset( $atts['order'] ) && ! empty( $atts['order'] ) ) {
			if ( 'DESC' === strtoupper( $atts['order'] ) || 'ASC' === strtoupper( $atts['order'] ) ) {
				$eve_args['order'] = sanitize_text_field( $atts['order'] );
			}
		} else {
			if( isset( $atts['past_events'] ) && $atts['past_events'] === true ){
				$atts['past_events'] = "yes";
			}
			if ( isset( $atts['past_events'] ) && 'yes' === $atts['past_events'] && 'meta_value' === $eve_args['orderby'] ) {
				$eve_args['order'] = 'DESC';
			} else {
				$eve_args['order'] = 'ASC';
			}
		}

		$col       = 2;
		$css_class = 'col-ife-md-6';
		if ( isset( $atts['col'] ) && ! empty( $atts['col'] ) && is_numeric( $atts['col'] ) ) {
			$col = $atts['col'];
			switch ( $col ) {
				case '1':
					$css_class = 'col-ife-md-12';
					break;

				case '2':
					$css_class = 'col-ife-md-6';
					break;

				case '3':
					$css_class = 'col-ife-md-4';
					break;

				case '4':
					$css_class = 'col-ife-md-3';
					break;

				default:
					$css_class = 'col-ife-md-4';
					break;
			}
		}

		$facebook_events = new WP_Query( $eve_args );

		$wp_list_events = '';
		// Start the Loop.
		if ( is_front_page() ) {
			$curr_paged = $paged;
			global $paged;
			$temp_paged = $paged;
			$paged      = $curr_paged; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
		}
		$ife_options  = get_option( IFE_OPTIONS );
		$accent_color = isset( $ife_options['accent_color'] ) ? $ife_options['accent_color'] : '#039ED7';
		$direct_link  = isset( $ife_options['direct_link'] ) ? $ife_options['direct_link'] : 'no';
		if ( ! ife_is_pro() ) {
			$direct_link = 'no';
		}
		ob_start();
		?>
		<div class="row_grid">
			<?php
			$template_args                = array();
			$template_args['css_class']   = $css_class;
			$template_args['direct_link'] = $direct_link;
			if ( $facebook_events->have_posts() ) :
				while ( $facebook_events->have_posts() ) :
					$facebook_events->the_post();

					if( isset( $atts['layout'] ) && $atts['layout'] == 'style2' && ife_is_pro() ){
						get_ife_template( 'ife-archive-content2.php', $template_args );
					}else{
						get_ife_template( 'ife-archive-content.php', $template_args );
					}

				endwhile; // End of the loop.

				if ( $facebook_events->max_num_pages > 1 ) : // custom pagination.
					?>
					<div class="col-ife-md-12">
						<nav class="prev-next-posts">
							<div class="prev-posts-link alignright">
								<?php echo get_next_posts_link( 'Next Events &raquo;', $facebook_events->max_num_pages ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
							</div>
							<div class="next-posts-link alignleft">
								<?php echo get_previous_posts_link( '&laquo; Previous Events' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
							</div>
						</nav>
					</div>
					<?php
				endif;
				else :
					echo esc_attr( apply_filters( 'ife_no_events_found_message', __( 'No Events are found.', 'import-facebook-events' ) ) );
			endif;

				?>
		</div>
		<style type="text/css">
			.ife_event .event_date{
				background-color: <?php echo esc_attr( $accent_color ); ?>;
			}
			.ife_event .event_desc .event_title{
				color: <?php echo esc_attr( $accent_color ); ?>;
			}
		</style>
		<?php
		// Allow developers to do something here.
		do_action( 'ife_after_event_list', $facebook_events );
		$wp_list_events = ob_get_contents();
		ob_end_clean();
		wp_reset_postdata();
		if ( is_front_page() ) {
			global $paged;
			$paged = $temp_paged; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
		}
		return $wp_list_events;

	}

Code file location:

import-facebook-events/import-facebook-events/includes/class-import-facebook-events-cpt.php

Conclusion

Now that you’ve learned how to embed the Import Social Events 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 *