• Resolved gabrieluntura

    (@gabrieluntura)


    Hello! I’m trying to setup two new email tags and failing to do it. Actually, the tags are created already, but the function it outputs is coming up empty.

    One tag is to display in the admin email a custom field called “Company” that the user must fill when is registering (this is already created and saved in the DB as a user meta field). The other is a simpler tag that will display the Users full name (First Name + Last Name).
    Can anyone help me with the function to output it? I’m confused especially with the $attributes parameter.

    Here is the code I have:

    <?php
    
    /**
     * Add a new email tag
     * $tags an array of the current email tags
     */
    function add_email_tag($email_tags) {
        
        $email_tags[] = array(
        'tag'         => 'user_company',
        'description' => __( 'The user Company' ),
        'function'    => 'company_email_tag',
        'context'     => array( 'email' ),
        );
        
        $email_tags[] = array(
        'tag'         => 'user_fullname',
        'description' => __( 'The user First and Last name' ),
        'function'    => 'fullname_email_tag',
        'context'     => array( 'email' ),
        );
        return $email_tags;
    }
    add_filter( 'nua_email_tags', 'add_email_tag', 5);
     
    /**
     * Now when I add '{user_company}' to an email template, that text will be
     * replaced by using the function below.
     */
    function company_email_tag( $attributes ) {
        return $attributes['company'];
    }
    
    function fullname_email_tag() {
        return esc_attr( $user->first_name ) . esc_attr( $user->last_name );
    }

    I also tried:

    function company_email_tag( $attributes ) {
        $current_user = wp_get_current_user();
        $company = get_user_meta( $current_user->ID, 'company', true );
        
        return $company;
    }

    But no success on the email (it works on a normal page when echoing this function).

    Thanks!

Viewing 6 replies - 1 through 6 (of 6 total)
  • Hi @gabrieluntura,

    Your code to add custom email tags is correct. The issue is the way you are getting the user meta fields. In the first example above, Please replace the ‘company_email_tag’ and ‘fullname_email_tag’ with the below functions and let us know if it works or not.

    The tags should work on the ‘User Approval’ and ‘Registration Approved’ email.

    function company_email_tag ( $attributes ) {
        $user = get_user_by( 'login', $attributes['user_login'] );
        $company = get_user_meta( $user->ID, 'company', true );
        return $company;
    }
    
    function fullname_email_tag ( $attributes ) {
        $user = get_user_by( 'login', $attributes['user_login'] );
        return esc_attr( $user->first_name ) . esc_attr( $user->last_name );
    }

    Thanks

    Thread Starter gabrieluntura

    (@gabrieluntura)

    Thanks for the reply @wpexpertssupportteam

    Unfortunately, it didn’t work, the email still comes up empty for both tags. Thanks

    Hi @gabrieluntura,

    Our technical team needs some more information from your side. Kindly open a support ticket on our official website so we will help you accordingly.

    Thanks

    Thread Starter gabrieluntura

    (@gabrieluntura)

    Hi @wpexpertssupportteam
    I tested on another site that another developer worked on, and this code was working there:

    function nua_email_tag_company( $attributes ) {
    	$user = $attributes['user'];
    
    	return get_user_meta( $user->ID, 'company', true);
    }
    
    function nua_email_tag_fullname( $attributes ) {
    	$user = $attributes['user'];
    
    	return sprintf( __( '%s %s', 'new-user-approve' ), $user->first_name, $user->last_name );
    }

    But it’s not working on the site that I’m building. It’s probably something else I’m doing wrong or maybe the order that the Company fields are saving vs. the email being sent is wrong.
    I’m going to debug it more, but if you have any tips let me know.

    Thanks!

    Hi @gabrieluntura,

    It seems the code to add the tag values into the email executes before the code to save the user meta fields in DB(name and company). Due to this, those meta values are not returned in email.

    We managed to get the required output by getting those meta values from post request.

    Thanks

    Thread Starter gabrieluntura

    (@gabrieluntura)

    I got in touch with the support team via ticket and they solved my problem. I am very happy with all the help you provided!

    Here’s the reply in case anyone wants to do the same in the future:

    It seems the code to add the tag values into the email executes before the code to save the user meta fields in DB(name and company). Due to this, those meta values are not returned in email.
    We managed to get the required output by getting those meta values from post request.

    And here is the actual working code

    /**
     * Add a new email tag
     * $tags an array of the current email tags
     */
    function add_email_tag($email_tags) {
        
        $email_tags[] = array(
        'tag'         => 'user_company',
        'description' => __( 'The user Company' ),
        'function'    => 'nua_email_tag_company',
        'context'     => array( 'email' ),
        );
        
        $email_tags[] = array(
        'tag'         => 'user_fullname',
        'description' => __( 'The user First and Last name' ),
        'function'    => 'nua_email_tag_fullname',
        'context'     => array( 'email' ),
        );
        return $email_tags;
    }
    add_filter( 'nua_email_tags', 'add_email_tag');
     
    /**
     * Email template tag: user_company
     * User's company name
     *
     * @param array $attributes
     *
     * @return string Company label and Company name
     */
    function nua_email_tag_company( $attributes ) {
    	//$user = $attributes['user'];
    	//$user = get_user_by( 'login', $attributes['user_login'] );
    
    	if ( isset( $_POST['account_company'] ) ) {
    		return sanitize_text_field($_POST['account_company'] );
    	} else {
    		return '';
    	}
    }
    
    /**
     * Email template tag: user_fullname
     * User's full name
     *
     * @param array $attributes
     *
     * @return string Name label and full name
     */
    function nua_email_tag_fullname( $attributes ) {
    	if ( isset( $_POST['billing_first_name'] ) && isset( $_POST['billing_last_name'] ) ) {
    		return sprintf( __( '%s %s', 'new-user-approve' ), sanitize_text_field($_POST['billing_first_name'] ), sanitize_text_field($_POST['billing_last_name'] ) );
    	} else {
    		return '';
    	}
    	
    }
Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Creating new email Tags’ is closed to new replies.