Email issue in DWQA plugin

We are using the DW Questions Answers plugin. The idea is that all subscribers get an email when a new post is DW Question & Answer is placed. I thought that this was the point. But the plugin only sends a New Question Notifications (to Admin) and only New Answer Notifications | New Comment Notifications to the follower of that post. What I want is, when a new question is posted, every WP user on the site should get a notification. We've found the code of the plugin where it determines the admin emails to send. If this can be tweaked a little to get the email addresses of all the users instead of the admins, that'd work fine for me. https://github.com/designwall/dw-question-answer/blob/59f1cf5b38ebbc10c2775cb5c9fbcc0a92d84ab3/inc/Notifications.php#L36

Any help is appreciated. Thank you very much!

  • Ash

    Hello Sander

    You don't need to edit core code, rather try to use this code:

    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 );
    	}
    }

    You can use this code in your child theme's functions.php if the theme is not changed. Otherwise mu-plugin is the best option. To create a mu-plugin, go to wp-content/mu-plugins folder. If there is no mu-plugins folder then, create one. Now, inside the mu-plugins folder create file with name anything.php (make sure file extension is .php). Now start with a <?php tag and then put the above code.

    Hope it helps! Please feel free to ask more questions if you have any.

    Have a nice day!

    Cheers,
    Ash

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.