{"id":101,"date":"2019-12-08T09:35:42","date_gmt":"2019-12-08T09:35:42","guid":{"rendered":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.oxyset.com\/?p=101"},"modified":"2026-10-10T20:47:53","modified_gmt":"2026-10-10T20:47:53","slug":"back-button","status":"publish","type":"post","link":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/back-button\/","title":{"rendered":"WordPress Back Button Shortcode With a Fallback Link"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">A WordPress back button is handy on product, search result and detail pages: one click takes the visitor to the page they came from, like the browser&#8217;s own Back button.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The usual <code>&lt;button onclick=\"history.back()\"&gt;<\/code> does nothing when there is no previous page, for example when the visitor opened the link in a new tab, and it can send people back to Google. This shortcode prints a normal link instead. With JavaScript it goes back one step, but only when the previous page is on your site; otherwise it opens a fallback URL you choose.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>&#91;dpc_back_button&#93;<\/code> goes back, or to the home page<\/li>\n\n\n<li><code>&#91;dpc_back_button label=\"Back to the shop\" fallback=\"\/shop\/\" class=\"wp-element-button\"&#93;<\/code><\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">The snippet<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">&lt;?php\n\/**\n * &#91;dpc_back_button label=\"Go back\" fallback=\"\/shop\/\" class=\"my-button\"&#93;\n * A link that works like the browser's Back button. When there is no previous\n * page on this site (direct visit, new tab, link from Google) it opens the fallback URL.\n *\/\nadd_shortcode( 'dpc_back_button', 'dpc_back_button_shortcode' );\n\nfunction dpc_back_button_shortcode( $atts ): string {\n\t$atts = shortcode_atts(\n\t\tarray(\n\t\t\t'label'    =&gt; __( 'Go back', 'dpc' ),\n\t\t\t'fallback' =&gt; home_url( '\/' ),\n\t\t\t'class'    =&gt; '',\n\t\t),\n\t\t$atts,\n\t\t'dpc_back_button'\n\t);\n\n\t\/\/ esc_url() returns '' for javascript: and other unsafe URLs; use the home page then.\n\t$fallback = esc_url( $atts&#91;'fallback'&#93; );\n\tif ( '' === $fallback ) {\n\t\t$fallback = esc_url( home_url( '\/' ) );\n\t}\n\n\t$classes = array( 'dpc-back-button' );\n\tforeach ( preg_split( '\/\\s+\/', (string) $atts&#91;'class'&#93;, -1, PREG_SPLIT_NO_EMPTY ) as $class ) {\n\t\t$classes&#91;&#93; = sanitize_html_class( $class );\n\t}\n\n\tdpc_back_button_script();\n\n\treturn sprintf(\n\t\t'&lt;a class=\"%1$s\" href=\"%2$s\" data-dpc-back&gt;%3$s&lt;\/a&gt;',\n\t\tesc_attr( implode( ' ', array_filter( $classes ) ) ),\n\t\t$fallback,\n\t\tesc_html( $atts&#91;'label'&#93; )\n\t);\n}\n\n\/** One small footer script per page, and only on pages that show the button. *\/\nfunction dpc_back_button_script(): void {\n\tif ( wp_script_is( 'dpc-back-button', 'enqueued' ) ) {\n\t\treturn;\n\t}\n\n\t$js = &lt;&lt;&lt;'JS'\ndocument.addEventListener('click', function (event) {\n\tvar link = event.target.closest('&#91;data-dpc-back&#93;');\n\t\/\/ Let Ctrl\/Cmd\/Shift\/middle clicks open the fallback link in a new tab as usual.\n\tif (!link || event.button !== 0 || event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) {\n\t\treturn;\n\t}\n\tvar sameSite = false;\n\ttry {\n\t\tsameSite = document.referrer !== '' &amp;&amp; new URL(document.referrer).origin === window.location.origin;\n\t} catch (e) {\n\t\tsameSite = false;\n\t}\n\t\/\/ Only go back when the visitor came from a page on this site.\n\tif (sameSite &amp;&amp; window.history.length &gt; 1) {\n\t\tevent.preventDefault();\n\t\twindow.history.back();\n\t}\n});\nJS;\n\n\twp_register_script( 'dpc-back-button', false, array(), null, array( 'in_footer' =&gt; true ) );\n\twp_add_inline_script( 'dpc-back-button', $js );\n\twp_enqueue_script( 'dpc-back-button' );\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Add it with Scripts Organizer<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In <a href=\"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/dplugins.com\/downloads\/scripts-organizer\/\">Scripts Organizer<\/a> go to <strong>Code Blocks \u2192 Add New<\/strong> and give the block a name. Set <strong>Trigger location<\/strong> to <strong>Everywhere<\/strong>, tick <strong>PHP<\/strong> under <strong>Script location<\/strong>, paste the code into the PHP editor and publish. Leave <strong>Action\/Hook<\/strong> empty.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This is a normal <code>add_shortcode()<\/code> rather than a block with the <strong>Shortcode<\/strong> script location on purpose: a Shortcode-location block cannot read attributes, and here every button needs its own label and fallback URL. It also means <code>&#91;dpc_back_button&#93;<\/code> works the same in the block editor&#8217;s Shortcode block, Bricks, Elementor or a classic widget, and the code runs unchanged in a small plugin or an mu-plugin.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How it works<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The shortcode returns an <code>&lt;a&gt;<\/code> whose <code>href<\/code> is the fallback URL. Without JavaScript, or when the visitor Ctrl\/Cmd-clicks or middle-clicks it, it behaves like any other link.<\/li>\n\n\n<li>One click listener on the document handles every back button on the page. It calls <code>history.back()<\/code> only when <code>document.referrer<\/code> has the same origin as the current page and the tab has history. Visitors who came from a search engine or another site get the fallback link instead.<\/li>\n\n\n<li><code>esc_url()<\/code> removes <code>javascript:<\/code> and other unsafe URLs; the snippet then uses the home page. The label is escaped and the extra classes go through <code>sanitize_html_class()<\/code>.<\/li>\n\n\n<li>The script is registered as a handle without a file and printed once in the footer, only on pages that contain a back button. No jQuery, no global <code>goBack()<\/code> function.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Customise it<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use your theme&#8217;s button style: add <code>class=\"wp-element-button\"<\/code> in a block theme, or your own class.<\/li>\n\n\n<li>Fallback to the previous archive instead of the home page: pass the archive URL, for example <code>fallback=\"\/blog\/\"<\/code>.<\/li>\n\n\n<li>Inside a PHP template call <code>echo do_shortcode( '&#91;dpc_back_button&#93;' );<\/code>.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Gotchas<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Sites that send <code>Referrer-Policy: no-referrer<\/code> leave <code>document.referrer<\/code> empty, so the button always opens the fallback URL.<\/li>\n\n\n<li>After a form submission with a redirect, going back can return to the form. Use a fixed fallback URL for those pages.<\/li>\n\n\n<li>Full-page caching is fine: the decision is made in the browser.<\/li>\n\n<\/ul>\n\n","protected":false},"excerpt":{"rendered":"<p>A WordPress back button shortcode that returns visitors to the previous page, and opens a fallback URL when they arrived from another site.<\/p>\n","protected":false},"author":1,"featured_media":46792,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"WordPress Back Button Shortcode With a Fallback Link","description":"A WordPress back button shortcode that returns visitors to the previous page, and opens a fallback URL when they arrived from another site."},"_slim_seo_primary_term_category":0,"_slim_seo_primary_term_post_tag":0,"footnotes":""},"categories":[73],"tags":[],"dpc_library":[204],"class_list":["post-101","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript"],"_links":{"self":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/101","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/comments?post=101"}],"version-history":[{"count":1,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/101\/revisions"}],"predecessor-version":[{"id":46791,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/101\/revisions\/46791"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media\/46792"}],"wp:attachment":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media?parent=101"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/categories?post=101"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/tags?post=101"},{"taxonomy":"dpc_library","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/dpc_library?post=101"}],"curies":[{"name":"wp","href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/api.w.org\/{rel}","templated":true}]}}