1

I have a few restricted pages. If a user tries to access those, I warn them by email using wp_mail and alert admin with some data such as username, URL, time, etc. For that, I am using the template_redirect hook.

All is working fine but wp_mail sending the email twice. I am not sure what is wrong in my code and why it is sending twice.

Callback Function and Hook

cp_write_log() is a custom log function that write a log to debug.log. Entries also adding twice to the debug.log file.

function module_permission_check() {

    if ( is_page() ) {

        $template  = get_page_template_slug();
        $post_type = get_cpt_for_template( $template );

        if ( ! is_current_user_granted_for_module( $post_type ) ) {
            $user = wp_get_current_user();

            //$headers = [ 'From: ' . get_bloginfo( 'name' ) . ' <' . get_bloginfo( 'admin_email' ) . '>' ];

            // user message
            $message = str_replace(
                [ '%%name%%', '%%sitename%%' ],
                [
                    $user->data->user_login,
                    get_bloginfo( 'name' ),
                ],
                cp_get_setting( 'warning_unauthorized_email_message' )
            );

            if ( wp_mail( $user->data->user_email, 'Warning: Unauthorized access', $message ) ) {
                cp_write_log( 'Email sent to ' . $user->data->user_email );
            }

            global $wp;
            $current_url   = home_url( add_query_arg( [], $wp->request ) );
            $admin_message = "Hello,\n\nA member has tried to access unauthorized content.\n\nUsername: {$user->data->user_login}\nURL: {$current_url}\nLocal Time: " . current_time( 'l, j F Y \a\t H:i ' ) . "\nUTC Time: " . current_time( 'l, j F Y \a\t H:i ', TRUE ) . "\n\nRegards,\n" . get_bloginfo( 'name' );

            if ( wp_mail( get_bloginfo( 'admin_email' ), 'Alert: Unauthorized access', $admin_message ) ) {
                cp_write_log( 'Email sent to admin' );
            }

            wp_redirect(home_url());
            exit();
        }
    }

}

add_action('template_redirect', 'module_permission_check');

Update

I found it is only happening on Safari browser. Very strange.

Code Lover
  • 8,099
  • 20
  • 84
  • 154
  • 1
    I think Wordpress does some crazy stuff with redirects on templates. It may be redirecting to the page itself before landing on the final version. For example, loading page template and then custom page template. – Unbranded Manchester Jun 02 '20 at 06:28
  • 1
    Have a look at this too : https://wordpress.stackexchange.com/questions/158604/template-redirect-called-twice – Unbranded Manchester Jun 02 '20 at 06:29
  • @UnbrandedManchester, you are right at some point, but I found the issue, and I would still say it is a WordPress problem. Demonstrating the issue, in my theme, I have hook `template_redirect` two times with two different functions. That was causing the issue. But I would consider it as a crazy, so what if any parent theme or plugin also using the same hook? Will WordPress trigger it as many times as it has hooked up? I am still investigating, but I believe this could be the issue. I will back in a few minutes once I fix the code. Thanks a lot :) – Code Lover Jun 02 '20 at 06:41
  • 1
    @UnbrandedManchester Probably, I was wrong. Removing other `template_redirect` didn't fix the issue. I still need a fix. :( – Code Lover Jun 02 '20 at 07:04
  • Can't you just hook into init instead? – Unbranded Manchester Jun 02 '20 at 08:48
  • In fact, I've done something similar before and I just added an if statement around the base page template that checked to see if you had permission and then ran the "report" function if they didn't. No need for a hook? – Unbranded Manchester Jun 02 '20 at 08:50
  • 1
    @UnbrandedManchester It could be an alternative for fewer templates and CPTs. However, I have more than 14 CPTs templates that all I have created dynamically. All CPTs and Templates are handled dynamically, and I do not want to write conditions to check permission on every single template. It will become difficult to manage code also. – Code Lover Jun 03 '20 at 04:44
  • Try hooking into init and see if that resolves the problem, you'll then be able to work out whether it is actually the template redirect_redirect action you're currently hooking into. – Unbranded Manchester Jun 03 '20 at 05:51

0 Answers0