Skip to content

Instantly share code, notes, and snippets.

@raazon
Last active December 13, 2020 08:03
Show Gist options
  • Save raazon/b4136fbdee1ebb203a800bd7dd0e7d06 to your computer and use it in GitHub Desktop.
Save raazon/b4136fbdee1ebb203a800bd7dd0e7d06 to your computer and use it in GitHub Desktop.

Revisions

  1. raazon revised this gist Dec 13, 2020. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions regex-for-wp-redirect.php
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@
    function ic_icredirection()
    {
    if (!is_user_logged_in()) {
    $regex = '/^(?:(?!\/(?:hello-world|hello-regex)(?:\/|$)).)*$/gm';
    $regex = '/^(?:(?!\/(?:hello-world|hello-regex)(?:\/|$)).)*$/m';
    $str = home_url($_SERVER['REQUEST_URI']);
    if (preg_match_all($regex, $str, $matches, PREG_SET_ORDER, 0)) {
    wp_redirect(home_url());
    @@ -22,7 +22,7 @@ function ic_icredirection()
    function ic_icredirection()
    {
    if (!is_user_logged_in()) {
    $regex = '/(http(s)?:\/\/)?([\w-]+\.)+[\w-]+(\/[\w- ;,.\/?%&=]*)?(hello-world|hello-regex)/gm';
    $regex = '/(http(s)?:\/\/)?([\w-]+\.)+[\w-]+(\/[\w- ;,.\/?%&=]*)?(hello-world|hello-regex)/m';
    $str = home_url($_SERVER['REQUEST_URI']);
    if (preg_match_all($regex, $str, $matches, PREG_SET_ORDER, 0)) {
    wp_redirect(home_url());
  2. raazon created this gist Dec 13, 2020.
    31 changes: 31 additions & 0 deletions regex-for-wp-redirect.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    <?php

    /*
    * Redirect if specific slug "hello-world OR hello-regex" not found in URL
    */
    add_action('template_redirect', 'ic_icredirection');
    function ic_icredirection()
    {
    if (!is_user_logged_in()) {
    $regex = '/^(?:(?!\/(?:hello-world|hello-regex)(?:\/|$)).)*$/gm';
    $str = home_url($_SERVER['REQUEST_URI']);
    if (preg_match_all($regex, $str, $matches, PREG_SET_ORDER, 0)) {
    wp_redirect(home_url());
    }
    }
    }

    /*
    * Redirect if specific slug "hello-world OR hello-regex" found in URL
    */
    add_action('template_redirect', 'ic_icredirection');
    function ic_icredirection()
    {
    if (!is_user_logged_in()) {
    $regex = '/(http(s)?:\/\/)?([\w-]+\.)+[\w-]+(\/[\w- ;,.\/?%&=]*)?(hello-world|hello-regex)/gm';
    $str = home_url($_SERVER['REQUEST_URI']);
    if (preg_match_all($regex, $str, $matches, PREG_SET_ORDER, 0)) {
    wp_redirect(home_url());
    }
    }
    }