web-dev-qa-db-ja.com

プラグインなしでカスタムウェルカムメールを作成する

Wordpressの登録プロセス中に送信されるウェルカムメールと確認メールのEメールの内容と件名をカスタマイズする方法はありますか?プラグインや「プラガブル」機能を使わずにフックまたはフィルタリングしたいのですが。

誰かが私を正しい方向に向けることができたら、私はとても感謝しています。ありがとうございます。

4
Jake Lisby

プラグイン可能な関数機能を使う必要があると思います - これらの関数の中にはフィルタもフックもありません(以下のコードからわかるように)。さらに悪いことに、あなたにとっては、プラグイン可能な関数 をプラグイン の中で使うほうが良いです。

これは、テーマのfunctions.phpで新しいプラガブル関数を定義すると、関数内で新しい関数の定義を使用するように要求されるためです(すべてのプラグインが完全に読み込まれるとすぐに呼び出すため)。この記事)、しかし、その一方で、それは動作します - 最初のものの下のコードを参照してください。

プラグインに反対していない人のために、プラグイン可能な関数を書き換えるものがあります - それをmy_plugin.php(または他の何か)にpluginsディレクトリに保存して、あなたの管理者から起動してください:

<?php
/*
Plugin Name: Name Of The Plugin
Plugin URI: http://URI_Of_Page_Describing_Plugin_and_Updates
Description: A brief description of the Plugin.
Version: The Plugin's Version Number, e.g.: 1.0
Author: Name Of The Plugin Author
Author URI: http://URI_Of_The_Plugin_Author
License: A "Slug" license name e.g. GPL2
*/
if( !function_exists('new_user_notification') ){
function new_user_notifiaction(){
        /**
         * Notify the blog admin of a new user, normally via email.
         *
         * @since 2.0
         *
         * @param int $user_id User ID
         * @param string $plaintext_pass Optional. The user's plaintext password
         */
        function wp_new_user_notification($user_id, $plaintext_pass = '') {
            $user = get_userdata( $user_id );

            $user_login = stripslashes($user->user_login);
            $user_email = stripslashes($user->user_email);

            // The blogname option is escaped with esc_html on the way into the database in sanitize_option
            // we want to reverse this for the plain text arena of emails.
            $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);

            $message  = sprintf(__('New user registration on your site %s:'), $blogname) . "\r\n\r\n";
            $message .= sprintf(__('Username: %s'), $user_login) . "\r\n\r\n";
            $message .= sprintf(__('E-mail: %s'), $user_email) . "\r\n";

            @wp_mail(get_option('admin_email'), sprintf(__('[%s] New User Registration'), $blogname), $message);

            if ( empty($plaintext_pass) )
                return;

            $message  = sprintf(__('Username: %s'), $user_login) . "\r\n";
            $message .= sprintf(__('Password: %s'), $plaintext_pass) . "\r\n";
            $message .= wp_login_url() . "\r\n";

            wp_mail($user_email, sprintf(__('[%s] Your username and password'), $blogname), $message);

        }
    }
}

あなたが興味を持っているのであれば、ここに同じ効果が別の関数の中で定義された新しい機能を持つfunctions.phpから管理されます:

//redefine wp_new_user_notification as soon as all plugins are loaded
add_action( 'plugins_loaded', 'new_user_notifiaction' );

function new_user_notifiaction(){
    /**
     * Notify the blog admin of a new user, normally via email.
     *
     * @since 2.0
     *
     * @param int $user_id User ID
     * @param string $plaintext_pass Optional. The user's plaintext password
     */
    function wp_new_user_notification($user_id, $plaintext_pass = '') {
        $user = get_userdata( $user_id );

        $user_login = stripslashes($user->user_login);
        $user_email = stripslashes($user->user_email);

        // The blogname option is escaped with esc_html on the way into the database in sanitize_option
        // we want to reverse this for the plain text arena of emails.
        $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);

        $message  = sprintf(__('New user registration on your site %s:'), $blogname) . "\r\n\r\n";
        $message .= sprintf(__('Username: %s'), $user_login) . "\r\n\r\n";
        $message .= sprintf(__('E-mail: %s'), $user_email) . "\r\n";

        @wp_mail(get_option('admin_email'), sprintf(__('[%s] New User Registration'), $blogname), $message);

        if ( empty($plaintext_pass) )
            return;

        $message  = sprintf(__('Username: %s'), $user_login) . "\r\n";
        $message .= sprintf(__('Password: %s'), $plaintext_pass) . "\r\n";
        $message .= wp_login_url() . "\r\n";

        wp_mail($user_email, sprintf(__('[%s] Your username and password'), $blogname), $message);

    }
}
0
david.binda

これは素晴らしいアイデアのようです。これはfunctions.phpでも機能します。

// Add filter for registration email body
add_filter('wp_mail','handle_wp_mail');

function handle_wp_mail($atts) {
    /*"Your username and password" is the subject of the Email WordPress send from "function wp_new_user_notification" in file "wp-includes/pluggable.php"*/

    if (isset ($atts ['subject']) && substr_count($atts ['subject'],'Your username and password')>0 ) {
    if (isset($atts['message'])) {
       $atts['message'] = 'new body';
    }
    }
    return ($atts);
}

正しいサブジェクト文字列を取得するために言語に注意してください。 (情報源: http://wordpress.org/support/topic/how-to-change-registration-email-content?replies=3

0
optimiertes