0

In a page template, I have a variable that stores an array of post IDs.

I would like users to be able to send that data in an email, by clicking a button on the front end page of that template. The email should include post_title and permalink of each post in the array, and preferably additional meta values. I would also need to include info about the current logged in user - display_name and user_email.

Using query vars I don't believe is a viable option because there can be up to hundreds of IDs in the array.

The closest guide I've found is here, but this is for passing current post data to the form.

  • Is this a mailto link, or are you sending the email yourself? – Milo Jun 9 '17 at 15:06
  • Which plugin are you using to create a form? – Jiten Gaikwad Jun 9 '17 at 15:09
  • @Milo User is triggering a 'send', which passes the info to site admin. I'm picturing a form with only the submit button visible on the front end, and hidden fields containing the data that I need. – Alvin Jun 9 '17 at 15:10
  • @JitenGaikwad It can be any plugin, I don't have any requirement there. Doesn't necessarily need to be with any plugin. – Alvin Jun 9 '17 at 15:12
1

The short answer- don't send all that data with the form.

You know what data goes with the form, and the email has to be sent from the server, so there's no reason for that data to make a round trip out to a browser and back to your server.

Set some sort of unique identifier and use that to get whatever data you need to send, whether it's attached to the current page, the current logged in user, etc..

Here's a quick proof of concept.

First, a Shortcode to output the form. Here we show two possible methods for identifying the form- an ID supplied with the shortcode, or the current page ID. We also check if the user is logged in or not, and whether to display the form or show a message that it's just been submitted.

function wpd_form_shortcode( $atts ){
    $a = shortcode_atts( array(
        'form_id' => 0,
    ), $atts );

    if( is_user_logged_in() ){

        if( isset( $_GET['sent'] ) ){
            $form = 'form submitted';
        } else {
            $form = '<form method="post" action="' . admin_url( 'admin-post.php' ) . '">';
            $form .= '<input type="hidden" name="action" value="wpd_send_form">';
            $form .= '<input type="hidden" name="form_id" value="' . $a['form_id'] . '">';
            $form .= '<input type="hidden" name="post_id" value="' . get_the_ID() . '">';
            $form .= '<input type="submit" value="Submit">';
            $form .= '</form>';
        }

    } else {

        $form = 'You must be logged in';

    }

    return $form;
}
add_shortcode( 'wpd_form', 'wpd_form_shortcode' );

Now the action hooked to admin_post that the above form points to. Here we show how to fetch the passed values, get the current logged in user, and send an email. This is where you should get those post IDs, whether from post meta, user meta, etc., and iterate over each to build the email.

function wpd_send_form_function() {

    // get the form ID or post ID
    // use these to get whatever data you need, get_post_meta, etc.
    $form_id = $_POST['form_id'];
    $post_id = $_POST['post_id'];

    // get the current logged in user info
    $current_user = wp_get_current_user();

    // put the data in the content of the email and send it
    $to = 'you@example.com';
    $subject = 'Email from ' . $current_user->user_login;
    $body = 'Email from post_id ' . $post_id;

    wp_mail( $to, $subject, $body );

    // redirect back to the page that contained the form we just submitted
    wp_safe_redirect( $_SERVER['HTTP_REFERER'] . '?sent=true', 303 );
    exit;
}
add_action( 'admin_post_wpd_send_form', 'wpd_send_form_function' );
// uncomment next line if you want to allow users who aren't logged in
// add_action( 'admin_post_nopriv_wpd_send_form', 'wpd_send_form_function' );

You should also look into using a nonce with the form, as well as doing some basic checks that you've got a valid post ID, that the user has permission to access the data for that post, etc..

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.