我创建了这段代码,这是工作很好,如果保持在头,但不工作在函数PHP wordpress


i created this code which is working fine if kept in header but not working in functions php wordpress

if ( !function_exists('nopaymentmade') ) {
    function nopaymentmade( $user_id ){
        global $wpdb;
        //print_r($wpdb);
        $listing_accounts_table_name = $wpdb->prefix . 'ht_listing_accounts';
        //$myrows = $wpdb->get_results( "SELECT * FROM wp_ht_listing_accounts WHERE account_type=paid-membership " );
        //print_r($myrows);
        $today =date("Y-m-d H:i:s");
        foreach( $wpdb->get_results("SELECT * FROM wp_ht_listing_accounts WHERE DATEDIFF(end_date,curdate())<=15 AND account_type='paid-membership'") as $key => $row) {
            // each column in your row will be accessible like this
            $userid = $row->user_id;
            //echo DATEDIFF($row->end_date,date("Y-m-d H:i:s"));
            foreach( $wpdb->get_results("SELECT * FROM wp_users WHERE ID=$userid ") as $key1 => $row1) {
                $row1->user_email;
                //$user = new WP_User( $user_id );
                echo $user_login = stripslashes( $row1->user_login );
                echo $user_email = stripslashes( $row1->user_email );
                //$message  = sprintf( __('New user registration on %s:'), get_option('blogname') ) . "'r'n'r'n";
                //$message .= sprintf( __('Username: %s'), $user_login ) . "'r'n'r'n";
                //$message .= sprintf( __('E-mail: %s'), $user_email ) . "'r'n";
                $message="User id in your website IIHF has expired at IIHF.ie. Please ask your user to renew it Immediately. A user with the username:".$user_login."and email id :".$user_email."";
                @wp_mail(
                    'singh.manjot007@gmail.com',
                    sprintf(__('[%s] Hello admin a User is Expiring'), get_option('blogname') ),
                    $message
                );

                /*$message  = __('Hi there,') . "'r'n'r'n";
                $message .= sprintf( __("Welcome to %s! Here's how to log in:"), get_option('blogname')) . "'r'n'r'n";
                $message .= wp_login_url() . "'r'n";
                $message .= sprintf( __('Username: %s'), $user_login ) . "'r'n";
                $message .= sprintf( __('Password: %s'), $plaintext_pass ) . "'r'n'r'n";
                $message .= sprintf( __('If you have any problems, please contact me at %s.'), get_option('admin_email') ) . "'r'n'r'n";
                $message .= __('Adios!');*/
                $message1="Your Account User id has expired at IIHF.ie. Please renew it Immediately. Please contact the site administrator for the Support";

                wp_mail(
                    $user_email,
                    sprintf( __('[%s] Your username and password'), get_option('blogname') ),
                    $message1
                );
                mail($user_email,'Your registration is expiring',$message1);
            }
        }
    }
}

这是我创建的功能,它的工作很好,因为它是创建发送电子邮件给管理员和用户当任何用户注册结束日期即将在15天内接近,然后它发送电子邮件给他们两个通知他们,他们的注册即将到期。问题是,如果我保持这段代码在header.php它发送一个电子邮件一次又一次,每次当页面得到刷新,所以我创建和功能,并在wordpress的functions.php函数中添加代码,但它不工作,然后。请告诉我该怎么做,并帮助我。

在function.php中使用wp_head()钩子调用该函数。我希望这对你有帮助。

你好,我正在回答这个问题,因为我通过函数和wordpress codex搜索了很多。最后,我找到了解决问题的方法。我觉得我应该分享一下。现在我已经这样做了,现在我的函数每天只运行一次。我已经为它添加了一个钩子到wordpress。请核对我的答案。

            //add_action('user_register', 'nopaymentmade');
            add_action( 'prefix_daily_event', 'nopaymentmade' );
            add_action( 'wp', 'prefix_setup_schedule' );
            /**
             * On an early action hook, check if the hook is scheduled - if not, schedule it.
             */
            function prefix_setup_schedule() {
                if ( ! wp_next_scheduled( 'prefix_daily_event' ) ) {
                    wp_schedule_event( time(), 'daily', 'prefix_daily_event');
                }
            }