• Ello there.

    I’m new to WordPress plugin development (new to Php in general) and I’m looking for my plugin to run some if checks and then forward to a new page after a submit/button was clicked.

    I am currently running something like:

    function plugin_run_function() {
        if (isset($_POST['button-name']))
            if (something == true) {
                header("Location: https://my.com/forumpage/");
            }
        }
    }
    add_action('init', 'plugin_run_function');

    My questions are:
    how can I call the function on a button click (does add_action(‘init’, ‘plugin_run_function’) work)?
    how can I redirect to a new page (wp_safe_redirect();)?

    Currently I’m just learning so security and OOP style plugin will come later on.

    Thanks in advance for the help.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The code you posted should work. Everytime a page is loaded your function runs and checks for the $_POST variable.

    To set the $_POST variable you could use a simple form:

    <form>
    	<input type="submit" value="click me">
    	<input type="hidden" name="button-name" value="submitted">
    </form>

    You would need to change your code to:

    function plugin_run_function() {
        if (isset($_POST['button-name']))
            if ($_POST['button-name'] == "submitted") {
                header("Location: https://my.com/forumpage/");
            }
        }
    }
    add_action('init', 'plugin_run_function');
    Thread Starter LjasonH

    (@ljasonh)

    Hmm…The way I posted doesn’t seem to work. No errors output either.

    Are there any other ways to check for errors other than (to see why the call isn’t working):
    define( ‘WP_DEBUG’, true );
    ini_set(‘display_errors’, 1);
    error_reporting(E_ALL);

    Cool

    Try checking the contents of the $_POST variable after you submit.

    var_dump($_POST);

    Thread Starter LjasonH

    (@ljasonh)

    All variables are set and the statements run well and forwards with ‘header’ after I stop other output exit(0);.

    However, if I call a function that redirects and stops output

    header("Location: https://my.page/error/");
        exit(0);

    it will pull up the ‘Too many redirects’ page.

    Is there any way around this (as it won’t work without the exit)?

    Note: I have come a little off topic but I feel opening a new thread isn’t necessary.

    Thread Starter LjasonH

    (@ljasonh)

    I seem to have fixed it somehow :s (not sure if I had removed all my outputs before the header call).

    Thank you all for the help!

    No problem!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Call plugin function from button and then forward’ is closed to new replies.