Custom code provided by WPMUDEV support stopped working for plugin DW Question & Answer plugin

More than a year ago, one of your colleagues helped me
https://premium.wpmudev.org/forums/topic/email-issue-in-dwqa-plugin?replies=6#post-1351338

Now the customization made for DW Question & Answer plugin stopped working

  • Ash
    • WordPress Hacker

    Hello Sander

    I am not sure why it's not working now, I can see the hook is still available in the code. But you may try the following:

    add_action('dwqa_add_question', 'new_question_notify_to_user', 10, 2);
    add_action('dwqa_new_question_notify', 'new_question_notify_to_user', 10, 2);
    function new_question_notify_to_user( $question_id, $user_id )
    {
    	$notifications = new DWQA_Notifications();
    	// receivers
    	$user_emails = array();
    
    	$args = array( 'orderby' => 'display_name' );
    	$wp_user_query = new WP_User_Query( $args );
    	$authors = $wp_user_query->get_results();
    	if ( ! empty( $authors ) ) {
    		foreach ( $authors as $author ) {
    			$author_info = get_userdata( $author->ID );
    			array_push( $user_emails, $author_info->user_email );
    		}
    	}
    
    	$enabled = get_option( 'dwqa_subscrible_enable_new_question_notification', 1 );
    	if ( ! $enabled ) {
    		return false;
    	}
    	$question = get_post( $question_id );
    	if ( ! $question ) {
    		return false;
    	}
    	$subject = get_option( 'dwqa_subscrible_new_question_email_subject' );
    	if ( ! $subject ) {
    		$subject = __( 'A new question was posted on {site_name}', 'dwqa' );
    	}
    	$subject = str_replace( '{site_name}', get_bloginfo( 'name' ), $subject );
    	$subject = str_replace( '{question_title}', $question->post_title, $subject );
    	$subject = str_replace( '{question_id}', $question->ID, $subject );
    	$subject = str_replace( '{username}', get_the_author_meta( 'display_name', $user_id ), $subject );
    
    	$message = dwqa_get_mail_template( 'dwqa_subscrible_new_question_email', 'new-question' );
    	if ( ! $message ) {
    		return false;
    	}
    	// Replacement
    
    	$admin = get_user_by( 'email', $admin_email[0] );
    	if ( $admin ) {
    		$message = str_replace( '{admin}', get_the_author_meta( 'display_name', $admin->ID ), $message );
    	}
    	//sender
    	$message = str_replace( '{user_avatar}', get_avatar( $user_id, '60' ), $message );
    	$message = str_replace( '{user_link}', dwqa_get_author_link( $user_id ), $message );
    	$message = str_replace( '{username}', get_the_author_meta( 'display_name', $user_id ), $message );
    	//question
    	$message = str_replace( '{question_link}', get_permalink( $question_id ), $message );
    	$message = str_replace( '{question_title}', $question->post_title, $message );
    	$message = str_replace( '{question_content}', $question->post_content, $message );
    	// Site info
    	$logo = get_option( 'dwqa_subscrible_email_logo', '' );
    	$logo = $logo ? '<img src="' . $logo . '" alt="' . get_bloginfo( 'name' ) . '" style="max-width: 100%; height: auto;" />' : '';
    	$message = str_replace( '{site_logo}', $logo, $message );
    	$message = str_replace( '{site_name}', get_bloginfo( 'name' ), $message );
    	$message = str_replace( '{site_description}', get_bloginfo( 'description' ), $message );
    	$message = str_replace( '{site_url}', site_url(), $message );
    	$headers = array(
    		"From: {$notifications->get_from_name()} <{$notifications->get_from_address()}>",
    		"Reply-To: {$notifications->get_from_address()}",
    		"Content-Type: {$notifications->get_content_type()}; charset=utf-8"
    	);
    
    	// start send out email
    	foreach( $user_emails as $to ) {
    		if ( is_email( $to ) )
    			$sended = $notifications->send( sanitize_email( $to ), $subject, $message, $headers );
    	}
    }

    I just added a new line at the beginning. If it still doesn't work, you may need to contact that plugin support to know about the changes. Feel free to share the code with them if needed.

    Have a nice day!

    Cheers,
    Ash

  • Ash
    • WordPress Hacker

    Hello Sander

    As I said, I am not familiar with the plugin, I just wrote the code based on logic. If that works, then yes, it would send email to all users.

    You need to replace my previous code with the code I gave on this thread. And then, post a new question to check if it sends email.

    Have a nice day!

    Cheers,
    Ash

  • Sander
    • Design Lord, Child of Thor

    Hi Ash,

    I've found the mu-plugin folder (anything.php), where the original code was.

    Should i replace everything?

    Original code is pasted here:
    `<?php

    add_action('dwqa_new_question_notify', 'new_question_notify_to_user', 10, 2);
    function new_question_notify_to_user( $question_id, $user_id )
    {
    $notifications = new DWQA_Notifications();
    // receivers
    $user_emails = array();

    $args = array( 'orderby' => 'display_name' );
    $wp_user_query = new WP_User_Query( $args );
    $authors = $wp_user_query->get_results();
    if ( ! empty( $authors ) ) {
    foreach ( $authors as $author ) {
    $author_info = get_userdata( $author->ID );
    array_push( $user_emails, $author_info->user_email );
    }
    }

    $enabled = get_option( 'dwqa_subscrible_enable_new_question_notification', 1 );
    if ( ! $enabled ) {
    return false;
    }
    $question = get_post( $question_id );
    if ( ! $question ) {
    return false;
    }
    $subject = get_option( 'dwqa_subscrible_new_question_email_subject' );
    if ( ! $subject ) {
    $subject = __( 'A new question was posted on {site_name}', 'dwqa' );
    }
    $subject = str_replace( '{site_name}', get_bloginfo( 'name' ), $subject );
    $subject = str_replace( '{question_title}', $question->post_title, $subject );
    $subject = str_replace( '{question_id}', $question->ID, $subject );
    $subject = str_replace( '{username}', get_the_author_meta( 'display_name', $user_id ), $subject );

    $message = dwqa_get_mail_template( 'dwqa_subscrible_new_question_email', 'new-question' );
    if ( ! $message ) {
    return false;
    }
    // Replacement

    $admin = get_user_by( 'email', $admin_email[0] );
    if ( $admin ) {
    $message = str_replace( '{admin}', get_the_author_meta( 'display_name', $admin->ID ), $message );
    }
    //sender
    $message = str_replace( '{user_avatar}', get_avatar( $user_id, '60' ), $message );
    $message = str_replace( '{user_link}', dwqa_get_author_link( $user_id ), $message );
    $message = str_replace( '{username}', get_the_author_meta( 'display_name', $user_id ), $message );
    //question
    $message = str_replace( '{question_link}', get_permalink( $question_id ), $message );
    $message = str_replace( '{question_title}', $question->post_title, $message );
    $message = str_replace( '{question_content}', $question->post_content, $message );
    // Site info
    $logo = get_option( 'dwqa_subscrible_email_logo', '' );
    $logo = $logo ? '<img src="' . $logo . '" alt="' . get_bloginfo( 'name' ) . '" style="max-width: 100%; height: auto;" />' : '';
    $message = str_replace( '{site_logo}', $logo, $message );
    $message = str_replace( '{site_name}', get_bloginfo( 'name' ), $message );
    $message = str_replace( '{site_description}', get_bloginfo( 'description' ), $message );
    $message = str_replace( '{site_url}', site_url(), $message );
    $headers = array(
    "From: {$notifications->get_from_name()} <{$notifications->get_from_address()}>",
    "Reply-To: {$notifications->get_from_address()}",
    "Content-Type: {$notifications->get_content_type()}; charset=utf-8"
    );

    // start send out email
    foreach( $user_emails as $to ) {
    if ( is_email( $to ) )
    $sended = $notifications->send( sanitize_email( $to ), $subject, $message, $headers );
    }
    }

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.