Search code examples
wordpressbuttonback

How to add WordPress back button for single page only


I would like to add a "back" button to a single page of my WordPress site. How can I do this? I found a lot of different code options but none for WordPress specifically that will work on one page.

I can't find the individual *.PHP file for a specific WordPress page, which I think would be required to implement something like this code:

<button type="button" onclick="history.back();"> Back </button>

I tried adding this to the "Text" tab in WordPress, but there apparently is no history.back function built in.


Solution

  • try to add this code to your function.php

     add_action( 'back_button', 'wpse221640_back_button' );
    function wpse221640_back_button() {
    if ( wp_get_referer() ) {
        $back_text = __( '&laquo; Back' );
        $button    = "\n<button id='my-back-button' class='btn button my-back-button' onclick='javascript:history.back()'>$back_text</button>";
        echo ( $button );
    }
    }
    

    add this to your template page

    <?php do_action('back_button'); ?>
    

    Let me know.