WordPress PHP email script

I am working on a project at the moment in wordpress and I need a php script that sends an email to the admin (me) when a user clicks on a button (that redirects to the script) I was hoping the email could send the following data to the admin:

  1. username
  2. the post or URL in which this user was on when they clicked on the button

I have been looking for a plugin for hours and have searched for script everywhere, I’m not exactly the best coder and appreciate the help.

Related posts

1 comment

  1. Here is a PHP snippet that should send you the page and the information about the user.

    You may need to install the WordPress PHP addon to insert this code.
    https://wordpress.org/plugins/insert-php/

    <?php
    
        $email_body = '';
    
        // Get the previous post
        $previous_page = $_SERVER['HTTP_REFERER'];
        $email_body .= 'Post or URL: ' . $previous_page . '<br />';
    
        // Get the user information
        $current_user = wp_get_current_user();
        $email_body .= 'Username: ' . $current_user->user_login . '<br />';
        $email_body .= 'User email: ' . $current_user->user_email . '<br />';
        $email_body .= 'User first name: ' . $current_user->user_firstname . '<br />';
        $email_body .= 'User last name: ' . $current_user->user_lastname . '<br />';
        $email_body .= 'User display name: ' . $current_user->display_name . '<br />';
        $email_body .= 'User ID: ' . $current_user->ID . '<br />';
    
        // Send email
        $to = 'you@gmail.com';
        $subject = 'Button was clicked';
        $message = $email_body;
        wp_mail( $to, $subject, $message);
    
    ?>
    

Comments are closed.