Title: Call plugin function from button and then forward
Last modified: August 22, 2016

---

# Call plugin function from button and then forward

 *  [LjasonH](https://wordpress.org/support/users/ljasonh/)
 * (@ljasonh)
 * [11 years, 5 months ago](https://wordpress.org/support/topic/call-plugin-function-from-and-then-forward/)
 * 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: http://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)

 *  [fried_eggz](https://wordpress.org/support/users/fried_eggz/)
 * (@fried_eggz)
 * [11 years, 5 months ago](https://wordpress.org/support/topic/call-plugin-function-from-and-then-forward/#post-5536091)
 * 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: http://my.com/forumpage/");
               }
           }
       }
       add_action('init', 'plugin_run_function');
       ```
   
 *  Thread Starter [LjasonH](https://wordpress.org/support/users/ljasonh/)
 * (@ljasonh)
 * [11 years, 5 months ago](https://wordpress.org/support/topic/call-plugin-function-from-and-then-forward/#post-5536098)
 * 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);
 *  [yepie wpiee](https://wordpress.org/support/users/sorcererayush/)
 * (@sorcererayush)
 * [11 years, 5 months ago](https://wordpress.org/support/topic/call-plugin-function-from-and-then-forward/#post-5536110)
 * Cool
 *  [fried_eggz](https://wordpress.org/support/users/fried_eggz/)
 * (@fried_eggz)
 * [11 years, 5 months ago](https://wordpress.org/support/topic/call-plugin-function-from-and-then-forward/#post-5536124)
 * Try checking the contents of the $_POST variable after you submit.
 * `var_dump($_POST);`
 *  Thread Starter [LjasonH](https://wordpress.org/support/users/ljasonh/)
 * (@ljasonh)
 * [11 years, 5 months ago](https://wordpress.org/support/topic/call-plugin-function-from-and-then-forward/#post-5536204)
 * 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: http://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](https://wordpress.org/support/users/ljasonh/)
 * (@ljasonh)
 * [11 years, 5 months ago](https://wordpress.org/support/topic/call-plugin-function-from-and-then-forward/#post-5536223)
 * 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!
 *  [fried_eggz](https://wordpress.org/support/users/fried_eggz/)
 * (@fried_eggz)
 * [11 years, 5 months ago](https://wordpress.org/support/topic/call-plugin-function-from-and-then-forward/#post-5536231)
 * 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.

## Tags

 * [button](https://wordpress.org/support/topic-tag/button/)
 * [call](https://wordpress.org/support/topic-tag/call/)
 * [function](https://wordpress.org/support/topic-tag/function/)

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 7 replies
 * 3 participants
 * Last reply from: [fried_eggz](https://wordpress.org/support/users/fried_eggz/)
 * Last activity: [11 years, 5 months ago](https://wordpress.org/support/topic/call-plugin-function-from-and-then-forward/#post-5536231)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
