Redirect “stage-” prefixed slug pages if exists on WordPress

For testing/staging purposes, sometimes you need to create new block or content based pages before taking them fully live. To check or show them without making them publicly visible to visitors, you can add “stage-” prefix to that new private pages with the hook below. When you’re logged in, you’re automatically redirected to the new version of that page, if the “stage-” slugged page exists.

For example, existing page slug: “about-us”
New page with new block layout or new content: “stage-about-us”

When you’re logged in, “about-us” is redirecting itself to “stage-about-us” page, with the hook below:


 * For redirecting to staging pages
  function () {
    if (is_singular() && is_user_logged_in()) {
      global $post, $wpdb;

      if ($staging_post = $wpdb->get_row(
        "SELECT ID, post_name FROM $wpdb->posts
        WHERE post_name = 'stage-" . $post->post_name . "'
        AND post_type = '" . $post->post_type . "'",
      )) {
);Code language: PHP (php)






Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.