• I am looking to redirect all non-logged on visitors and non-admin users to a specific page, this is for a maintenance mode and I want to redirect them to my under maintenance page, all but the admins (me) of course who are working on the site.

    I am looking either for an already existing plugin (or code) that allows me to pick a page like the Menus does (not just a URL, but one if I want), and to allow me to turn it on and off if I am not in maintenance mode.

    I am very new to writing plugins but will try if I have code, or a sample.

Viewing 9 replies - 1 through 9 (of 9 total)
  • Hey kjgbriggs,

    I have not used any of them before, but there are a lot of potential maintenance plugins out there.

    Good luck!

    Thread Starter kjgbriggs

    (@kjgbriggs)

    I have looked at many of these but they provide custom styling, I prefer to build my pages using the WP page editor, I am using the Divi theme which has a very powerful page editor and would prefer to have my maintenance page powered with that, rather than the actual plugin because I have more customizability and can put more stuff on it(and have better formatting).

    Got it. How about something like [WordPress] How to Redirect users if not logged in?

    Thread Starter kjgbriggs

    (@kjgbriggs)

    Looked into this for about an hour and so far I have this:

    <?php
    /**
     * Plugin Name: Redirect To Page
     * Description: A simple plugin for redirecting non-admins to a specific page.
     */
    
    add_action('admin_menu', 'rtp_plugin_settings');
    
    function rtp_plugin_settings() {
    
        add_menu_page('Redirect Settings', 'Redirect Settings', 'administrator', 'rtp_settings', 'rtp_display_settings');
    
    }
    
    function rtp_display_settings() {
        $pageurl = (get_option('rtp_pageurl') != '') ? get_option('rtp_pageurl') : '2000';
    
        $enableredirect  = (get_option('rtp_enabled') == 'enabled') ? 'checked' : '' ;
    
        $html = '</pre>
    <div class="wrap"><form action="options.php" method="post" name="options">
    <h2>Redirect Settings</h2>
    ' . wp_nonce_field('update-options') . '
    <table class="form-table" width="100%" cellpadding="10">
    <tbody>
    <tr>
    <td scope="row" align="left">
     <label>Enable Redirect</label>
     <label> </label>
     <input type="checkbox" name="rtp_enabled" value="enabled" /></td>
    </tr>
    <tr>
    <td scope="row" align="left">
     <label>Page URL</label>
     <label> </label>
     <input type="text" name="rtp_pageurl" value="' . $pageurl . '" />
    </td>
    </tr>
    </tbody>
    </table>
     <input type="hidden" name="action" value="update" />
    
     <input type="hidden" name="page_options" value="enableredirect, rtp_pageurl" />
    
     <input type="submit" name="Submit" value="Update" /></form></div>
    <pre>
    ';
    
        echo $html;
    
    }

    My main issue now is that I cannot seem to get another function added without an error and the plugin going inactive.
    Would you know how to finish this (or add the function)?

    P.S: Try to bare with me, I am more than fluent in Javascript, UnityScript and quickly learning C#, but this is my absolute first time with php, and making WordPress plugins so try to bare that in mind if my code is complete gibberish.

    Thread Starter kjgbriggs

    (@kjgbriggs)

    Bump

    Thread Starter kjgbriggs

    (@kjgbriggs)

    I now have this but it is still doing absolutely nothing (and the if statement is printed as text).
    Ill check back tomorrow evening.

    <?php
    /*
     Plugin Name: Redirect To Page
     Description: A simple plugin for redirecting non-admins to a specific page.
     Version: 1.0
     */
    
    add_action('admin_menu', 'rtp_plugin_settings');
    
    function rtp_plugin_settings() {
    
        add_menu_page('Redirect Settings', 'Redirect Settings', 'administrator', 'rtp_settings', 'rtp_display_settings');
    
    }
    
    function rtp_display_settings() {
        $pageurl = (get_option('rtp_pageurl') != '') ? get_option('rtp_pageurl') : 'https://www.example.com/';
    
        $enableredirect  = (get_option('rtp_enabled') == 'enabled') ? 'checked' : '' ;
    
        $html = '</pre>
    <div class="wrap"><form action="options.php" method="post" name="options">
    <h2>Redirect Settings</h2>
    ' . wp_nonce_field('update-options') . '
    <table class="form-table" width="100%" cellpadding="10">
    <tbody>
    <tr>
    <td scope="row" align="left">
     <label>Enable Redirect</label>
     <label> </label>
     <input type="checkbox" name="rtp_enabled" value="enabled" /></td>
    </tr>
    <tr>
    <td scope="row" align="left">
     <label>Page URL</label>
     <label> </label>
     <input type="text" name="rtp_pageurl" value="' . $pageurl . '" />
    </td>
    </tr>
    </tbody>
    </table>
     <input type="hidden" name="action" value="update" />
    
     <input type="hidden" name="page_options" value="enableredirect, rtp_pageurl" />
    
     <input type="submit" name="Submit" value="Update" /></form></div>
    
    if (!is_super_admin())
    {
    redirect_user();
    }
    <pre>
    ';
    
        echo $html;
    
    }
    
    function redirect_user()
    {
     wp_redirect(home_url().rtp_pageurl);
     exit;
    }

    I haven’t had time to hack around with you’ve got yet, but you’re missing a <?php toward the bottom and the plugin is lacking update_option.

    https://codex.www.ads-software.com/Writing_a_Plugin

    Thread Starter kjgbriggs

    (@kjgbriggs)

    As I said, I am very new to this, this is my first plugin, and I have been at college 3d modelling all day so I have not had a chance to work on it, I just got back.

    Thread Starter kjgbriggs

    (@kjgbriggs)

    I have already looked through the WordPress plugin API, I am usually quite good at programming and picking things up but this just seems so thrown together to me, the API is not very well structured in comparison to things like Microsoft .Net or even UnityScript.

    Nothing I have tried since the posted code works and I am at a loss with this.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Redirect non-logged on users’ is closed to new replies.