cliffordp
12/22/2017 - 12:25 AM

The Events Calendar: Turn the "All Events" link into a true "Back" button.

The Events Calendar: Turn the "All Events" link into a true "Back" button.

<?php
/**
 * The Events Calendar: Turn the "All Events" link into a true "Back" button.
 *
 * Generally not advisable but provided at customer's request:
 * https://theeventscalendar.com/support/forums/topic/upcoming-events-8/
 *
 * @link https://gist.github.com/cliffordp/60b5acf003fa68ad7803313d662c08e7
 * @link https://css-tricks.com/snippets/javascript/go-back-button/
 * @link https://api.jquery.com/replacewith/
 */
add_action( 'wp_footer', 'cliff_all_events_link_replaced_with_back_button' );
function cliff_all_events_link_replaced_with_back_button() {
	wp_enqueue_script( 'jquery' );
	?>
	<script type="text/javascript">
		jQuery( document ).ready( function () {
			var all_events = jQuery( 'p.tribe-events-back a' );

			// The following line is commented out because an "All Events" link that goes back to say the Blog Posts page would confuse the user.
			// var button_text = all_events.text().trim();
			var button_text = '&laquo; Go Back';

			var new_element = '<input type="button" value="';
			new_element += button_text;
			new_element += '" onclick="history.back(-1)" />';

			all_events.replaceWith( new_element );
		} );
	</script>
	<?php
}