Back Button Shortcode

Home » Snippets » Back Button Shortcode

Created with:



Creator: Jules Webb

Customize with WPTurbo AI

Add Snippet To Project

New Project
Add To Existing Project
function wpturbo_back_button_shortcode( $atts ) {
    $atts = shortcode_atts(
            'text' => __( 'Back', 'wpturbo' ),
    $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.

Register an account to save your snippets or go Pro to get more features.