Advertisement
skotracker

events manager

Feb 21st, 2023
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.39 KB | None | 0 0
  1. php
  2.  
  3. <?php
  4. /**
  5. * Plugin Name: Event Manager Shortcodes
  6. * Description: A plugin that provides shortcodes for displaying events on your WordPress website.
  7. * Version: 1.0
  8. * Author: Your Name
  9. * Author URI: http://yourwebsite.com
  10. */
  11.  
  12. // [events_list] shortcode
  13. function ems_events_list_shortcode( $atts ) {
  14. ob_start();
  15. $atts = shortcode_atts( array(
  16. 'category' => '',
  17. 'tag' => '',
  18. 'location' => '',
  19. 'limit' => 5,
  20. 'header' => 'Upcoming Events',
  21. 'footer' => 'See All Events',
  22. 'template' => ''
  23. ), $atts, 'events_list' );
  24.  
  25. $args = array(
  26. 'post_type' => 'event',
  27. 'posts_per_page' => $atts['limit'],
  28. 'meta_key' => 'event_start_date_time',
  29. 'orderby' => 'meta_value',
  30. 'order' => 'ASC'
  31. );
  32.  
  33. if ( !empty( $atts['category'] ) ) {
  34. $args['tax_query'] = array(
  35. array(
  36. 'taxonomy' => 'event_category',
  37. 'field' => 'slug',
  38. 'terms' => $atts['category']
  39. )
  40. );
  41. }
  42.  
  43. if ( !empty( $atts['tag'] ) ) {
  44. $args['tax_query'] = array(
  45. array(
  46. 'taxonomy' => 'event_tag',
  47. 'field' => 'slug',
  48. 'terms' => $atts['tag']
  49. )
  50. );
  51. }
  52.  
  53. if ( !empty( $atts['location'] ) ) {
  54. $args['meta_query'] = array(
  55. array(
  56. 'key' => 'event_location',
  57. 'value' => $atts['location'],
  58. 'compare' => 'LIKE'
  59. )
  60. );
  61. }
  62.  
  63. $events = new WP_Query( $args );
  64.  
  65. if ( $events->have_posts() ) {
  66. echo '<h3>' . esc_html( $atts['header'] ) . '</h3>';
  67. while ( $events->have_posts() ) {
  68. $events->the_post();
  69. if ( empty( $atts['template'] ) ) {
  70. include( plugin_dir_path( __FILE__ ) . 'templates/events-listing.php' );
  71. } else {
  72. include( plugin_dir_path( __FILE__ ) . 'templates/' . $atts['template'] );
  73. }
  74. }
  75. echo '<p class="see-all">' . esc_html( $atts['footer'] ) . '</p>';
  76. } else {
  77. echo '<p>No events found.</p>';
  78. }
  79.  
  80. wp_reset_postdata();
  81.  
  82. return ob_get_clean();
  83. }
  84. add_shortcode( 'events_list', 'ems_events_list_shortcode' );
  85.  
  86. // [events_calendar] shortcode
  87. function ems_events_calendar_shortcode( $atts ) {
  88. ob_start();
  89. $atts = shortcode_atts( array(
  90. 'category' => '',
  91. 'tag' => '',
  92. 'month' => '',
  93. 'template' => ''
  94. ), $atts, 'events_calendar' );
  95.  
  96. $args = array(
  97. 'post_type' => 'event',
  98. 'meta_key' => 'event_start_date_time',
  99. 'orderby' => 'meta_value',
  100. 'order' => 'ASC'
  101. );
  102.  
  103. if ( !empty( $atts['category'] ) ) {
  104. $args['tax_query'] = array(
  105. array(
  106. 'taxonomy' => 'event_category',
  107. 'field' => 'slug',
  108. 'terms' => $atts['category']
  109.  
  110. bryanreynolds1492@gmail.com
  111. is the above complete
  112.  
  113. I apologize, it looks like the code was cut off. Here is the rest of the code for the second shortcode:
  114.  
  115. php
  116.  
  117. );
  118. }
  119.  
  120. if ( !empty( $atts['tag'] ) ) {
  121. $args['tax_query'] = array(
  122. array(
  123. 'taxonomy' => 'event_tag',
  124. 'field' => 'slug',
  125. 'terms' => $atts['tag']
  126. )
  127. );
  128. }
  129.  
  130. if ( !empty( $atts['month'] ) ) {
  131. $args['meta_query'] = array(
  132. array(
  133. 'key' => 'event_start_date_time',
  134. 'value' => array( date( 'Y-m-01', strtotime( $atts['month'] ) ), date( 'Y-m-t', strtotime( $atts['month'] ) ) ),
  135. 'type' => 'DATE',
  136. 'compare' => 'BETWEEN'
  137. )
  138. );
  139. }
  140.  
  141. $events = new WP_Query( $args );
  142.  
  143. if ( empty( $atts['template'] ) ) {
  144. include( plugin_dir_path( __FILE__ ) . 'templates/events-calendar.php' );
  145. } else {
  146. include( plugin_dir_path( __FILE__ ) . 'templates/' . $atts['template'] );
  147. }
  148.  
  149. wp_reset_postdata();
  150.  
  151. return ob_get_clean();
  152. }
  153. add_shortcode( 'events_calendar', 'ems_events_calendar_shortcode' );
  154.  
  155. This plugin provides two shortcodes: [events_list] and [events_calendar]. You can use the [events_list] shortcode to display a list of events on your website with various filters and options, and you can use the [events_calendar] shortcode to display a calendar of events.
  156.  
  157. The plugin includes some default values for the shortcode parameters, which you can override in your shortcode calls. You can also create your own templates for displaying events by creating template files in the templates folder within your plugin.
  158.  
  159. Please note that this is just an example and may not be a complete or optimized solution for your specific needs. It's important to thoroughly test any custom code on a staging site before implementing it on a live website.
  160. bryanreynolds1492@gmail.com
  161. is the above complete?
  162.  
  163. Yes, the code above is complete for a basic WordPress plugin that provides two shortcodes [events_list] and [events_calendar] to display a list and calendar of events. However, this is just a starting point, and you may need to customize the plugin further to fit your specific needs. Additionally, you may need to optimize the plugin for performance, security, and usability, depending on your use case.
  164. bryanreynolds1492@gmail.com
  165. <?php
  166. /**
  167. * Plugin Name: Event Manager Shortcodes
  168. * Description: A plugin that provides shortcodes for displaying events on your WordPress website.
  169. * Version: 1.0
  170. * Author: Your Name
  171. * Author URI: http://yourwebsite.com
  172. */
  173.  
  174. // [events_list] shortcode
  175. function ems_events_list_shortcode( $atts ) {
  176. ob_start();
  177. $atts = shortcode_atts( array(
  178. 'category' => '',
  179. 'tag' => '',
  180. 'location' => '',
  181. 'limit' => 5,
  182. 'header' => 'Upcoming Events',
  183. 'footer' => 'See All Events',
  184. 'template' => ''
  185. ), $atts, 'events_list' );
  186.  
  187. $args = array(
  188. 'post_type' => 'event',
  189. 'posts_per_page' => $atts['limit'],
  190. 'meta_key' => 'event_start_date_time',
  191. 'orderby' => 'meta_value',
  192. 'order' => 'ASC'
  193. );
  194.  
  195. if ( !empty( $atts['category'] ) ) {
  196. $args['tax_query'] = array(
  197. array(
  198. 'taxonomy' => 'event_category',
  199. 'field' => 'slug',
  200. 'terms' => $atts['category']
  201. )
  202. );
  203. }
  204.  
  205. if ( !empty( $atts['tag'] ) ) {
  206. $args['tax_query'] = array(
  207. array(
  208. 'taxonomy' => 'event_tag',
  209. 'field' => 'slug',
  210. 'terms' => $atts['tag']
  211. )
  212. );
  213. }
  214.  
  215. if ( !empty( $atts['location'] ) ) {
  216. $args['meta_query'] = array(
  217. array(
  218. 'key' => 'event_location',
  219. 'value' => $atts['location'],
  220. 'compare' => 'LIKE'
  221. )
  222. );
  223. }
  224.  
  225. $events = new WP_Query( $args );
  226.  
  227. if ( $events->have_posts() ) {
  228. echo '<h3>' . esc_html( $atts['header'] ) . '</h3>';
  229. while ( $events->have_posts() ) {
  230. $events->the_post();
  231. if ( empty( $atts['template'] ) ) {
  232. include( plugin_dir_path( __FILE__ ) . 'templates/events-listing.php' );
  233. } else {
  234. include( plugin_dir_path( __FILE__ ) . 'templates/' . $atts['template'] );
  235. }
  236. }
  237. echo '<p class="see-all">' . esc_html( $atts['footer'] ) . '</p>';
  238. } else {
  239. echo '<p>No events found.</p>';
  240. }
  241.  
  242. wp_reset_postdata();
  243.  
  244. return ob_get_clean();
  245. }
  246. add_shortcode( 'events_list', 'ems_events_list_shortcode' );
  247.  
  248. // [events_calendar] shortcode
  249. function ems_events_calendar_shortcode( $atts ) {
  250. ob_start();
  251. $atts = shortcode_atts( array(
  252. 'category' => '',
  253. 'tag' => '',
  254. 'month' => '',
  255. 'template' => ''
  256. ), $atts, 'events_calendar' );
  257.  
  258. $args = array(
  259. 'post_type' => 'event',
  260. 'meta_key' => 'event_start_date_time',
  261. 'orderby' => 'meta_value',
  262. 'order' => 'ASC'
  263. );
  264.  
  265. if ( !empty( $atts['category'] ) ) {
  266. $args['tax_query'] = array(
  267. array(
  268. 'taxonomy' => 'event_category',
  269. 'field' => 'slug',
  270. 'terms' => $atts['category']
  271. );
  272. }
  273.  
  274. if ( !empty( $atts['tag'] ) ) {
  275. $args['tax_query'] = array(
  276. array(
  277. 'taxonomy' => 'event_tag',
  278. 'field' => 'slug',
  279. 'terms' => $atts['tag']
  280. )
  281. );
  282. }
  283.  
  284. if ( !empty( $atts['month'] ) ) {
  285. $args['meta_query'] = array(
  286. array(
  287. 'key' => 'event_start_date_time',
  288. 'value' => array( date( 'Y-m-01', strtotime( $atts['month'] ) ), date( 'Y-m-t', strtotime( $atts['month'] ) ) ),
  289. 'type' => 'DATE',
  290. 'compare' => 'BETWEEN'
  291. )
  292. );
  293. }
  294.  
  295. $events = new WP_Query( $args );
  296.  
  297. if ( empty( $atts['template'] ) ) {
  298. include( plugin_dir_path( __FILE__ ) . 'templates/events-calendar.php' );
  299. } else {
  300. include( plugin_dir_path( __FILE__ ) . 'templates/' . $atts['template'] );
  301. }
  302.  
  303. wp_reset_postdata();
  304.  
  305. return ob_get_clean();
  306. }
  307. add_shortcode( 'events_calendar', 'ems_events_calendar_shortcode' );
  308.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement