I want to send a download link to user by mailchimp, after they filled out a email form.

The PHP Request listener is calling my_function(), but inside this function the action is not called.

The functions and the action are working. Also after put the do_action(); inside the createDownloadButton() it will be called. Only after write the action inside my_function() it doesn't work?!

Inside the functions.php:

        * Place a button
        * @return string html-form
        * usage: [download_button download_name='my_download']
        function createDownloadButton($atts ){
          //shortcode input
          $a = shortcode_atts( array(
              'download_name' => ''
          ), $atts );

            $result = '<form class="form" method="post" action="' . esc_url( $_SERVER['REQUEST_URI'] ) . '">';
              $result .= '<input hidden name="title" value="'.$a['download_name'].'"  type="text" >'; 

              $result .= '<p>';
               $result .= '<input autofocus class="imput_mail" name="email" placeholder="email adress*" value="" type="email" required>'; 
               $result .= '<input class="bt" id="btn" onClick="ga(\'send\', \'event\', \'push_button\', \'some_analytics_event\');" value="" type="submit">';
              $result .= '</p>';

          $result .= '</form>';
          return $result;
        add shortcode('download_button', 'createDownloadButton' );

    *  PHP request listener
    if( isset($_POST['email']) && isset($_POST['title']) ){

    *  Do some action here
    function my_function($mail, $title){
     //echo works!
     echo "<script>console.log( 'email: ".$mail." | download: ".$title."' );</script>"; 
     //why the action doen't work here?
     do_action('memberToMailchimpList', '<API-KEY>', $mail, '<LIST-ID>', '<INTEREST>'); 
  • Do you even have a function hooked to memberToMailchimpList? – Jacob Peattie Dec 21 '17 at 12:41
  • Hey jacob! Yes, and the function behind that action is working fine. As i said, if I put the do_action inside the createDownloadButton($atts ) it works fine.. :( – murcoder Dec 21 '17 at 12:54

I just solved it!

The problem was, that the php request handling works different in wordpress. There has to be a workaround with a admin_post.php hook:

Instead of if( isset($_POST['email']) && isset($_POST['title']) ){...}

I had to use this hook:

add_action( 'admin_post_nopriv_process_form', 'process_form_data' );
add_action( 'admin_post_process_form', 'process_form_data' );
function process_form_data() {

A hidden form field is used to hook in this function. For the redirect, I created another hidden field in the form, to redirect to user back to the previous site:

  $result .= '<input type="hidden" name="url" value="'.$current_url.'">'; //redirect
  $result .= '<input type="hidden" name="action" value="process_form">'; //hook

Source: adaptiveweb.com

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.