0
X
Add Snippet To Project
New Project
Add To Existing Project
<?php
function wpturbo_back_button_shortcode( $atts ) {
$atts = shortcode_atts(
array(
'text' => __( 'Back', 'wpturbo' ),
),
$atts
);
$referer = wp_get_referer();
if ( empty( $referer ) ) {
$referer = home_url();
}
$button = '<a href="' . esc_url( $referer ) . '">' . esc_html( $atts['text'] ) . '</a>';
return $button;
}
add_shortcode( 'back_button', 'wpturbo_back_button_shortcode' );
The function first checks whether there is a previous referring URL using wp_get_referer(), and if not, it sets the $referer variable to the site's home URL. It then creates the "Back" button using the $referer URL and the $atts['text'] attribute.
To use the shortcode, simply add "[back_button]" to the page or post where you want to include the button.
Don't forget to prefix the function name with wpturbo_ and use the wpturbo text-domain for translations as per best practices.