CoursePress: Email link doesn't work

When someone comments in the discussion module for the unit, a notification email is sent. But the link in that email doesn't work. For example the url appear in this way
https://for**********te.com/module/didactica-4-comentarios-2/#comments
or this https://for**********te.com/courses/modulo-9/units/modulo-9#module-33860
and the url should be https://for**********te.com/courses/modulo-9/units/unidad-2-13/#comments

  • Konstantinos Xenos

    Hi Alejandra ,

    Could you please add this code into your functions.php ( preferably the child themes ) and give it a try if you're now receiving a correct URL? Everything works properly on my setups, but there might be differences between your site and mine.

    Always make sure to keep a backup before doing code edits.

    add_filter(
    	'comment_moderation_text',
    	function( $notify_message, $comment_id ) {
    
    		$comment = get_comment( $comment_id );
    		$post_id = $comment->comment_post_ID;
    		$post    = get_post( $post_id );
    
    		if ( 'module' != $post->post_type ) {
    			return $notify_message;
    		}
    
    		$unit_id               = get_post_field( 'post_parent', $post_id );
    		$course_id             = get_post_field( 'post_parent', $unit_id );
    		$course_link           = get_permalink( $course_id );
    		$location              = esc_url_raw( $course_link . CoursePress_Core::get_slug( 'unit/' ) . get_post_field( 'post_name', $unit_id ) . '/page/' . get_post_meta( $post->ID, 'module_page', true ) . '/module_id/' . $post->ID . '#comment-' . $comment_id );
    		$comment_author_domain = @gethostbyaddr( $comment->comment_author_IP );
    		$comment_content       = wp_specialchars_decode( $comment->comment_content );
    
    		$notify_message  = sprintf( __('A new comment on the post "%s" is waiting for your approval'), $post->post_title ) . "\r\n";
    		$notify_message .= "{$location}\r\n\r\n";
    		$notify_message .= sprintf( __( 'Author: %1$s (IP address: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
    		$notify_message .= sprintf( __( 'Email: %s' ), $comment->comment_author_email ) . "\r\n";
    		$notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n";
    		$notify_message .= sprintf( __( 'Comment: %s' ), "\r\n" . $comment_content ) . "\r\n\r\n";
    
    		return $notify_message;
    
    	},
    	20,
    	2
    );

    Tell me if you need further help with this!

    Regards,
    Konstantinos

  • Alejandra

    Hello I don't use moderation for comments, just receive a email when new comment is added to my site, in this case the code going to work? Just receive you have a new comment and the email have the url to go to the comment, the problem there is that just in comment into a unit for coursepress the url doesn't work because should be something like:

    https://for**********te.com/courses/modulo-9/units/unidad-2-13/#comments

    And these are the url that I receive is:
    https://for**********te.com/module/didactica-4-comentarios-2/#comments
    https://for**********te.com/courses/modulo-9/units/modulo-9#module-33860

  • Panos

    Hi Alejandra ,

    In that case could you try the following snippet:

    add_filter( 'comment_notification_text', function( $notify_message, $comment_id ){
    
    	$comment 	= get_comment($comment_id);
    	$post_id 	= $comment->comment_post_ID;
    	$post 		= get_post( $post_id );
    
    	if ( 'module' != $post->post_type ) {
    		return $notify_message;
    	}
    
    	$unit_id 					= get_post_field( 'post_parent', $post_id );
    	$course_id 					= get_post_field( 'post_parent', $unit_id );
    	$course_link 				= get_permalink( $course_id );
    	$location 					= esc_url_raw( $course_link . CoursePress_Core::get_slug( 'unit/' ) . get_post_field( 'post_name', $unit_id ) . '/page/' . get_post_meta( $post->ID, 'module_page', true ) . '/module_id/' . $post->ID . '#comment-' . $comment_id );
    	$comment_author_domain 		= @gethostbyaddr($comment->comment_author_IP);
    	$comment_content 			= wp_specialchars_decode( $comment->comment_content );
    
    	$notify_message  			= sprintf( __( 'New comment on your post "%s"' ), $post->post_title ) . "\r\n";
    
    	$notify_message 			.= sprintf( __( 'Author: %1$s (IP address: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
    	$notify_message 			.= sprintf( __( 'Email: %s' ), $comment->comment_author_email ) . "\r\n";
    	$notify_message 			.= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n";
    	$notify_message 			.= sprintf( __('Comment: %s' ), "\r\n" . $comment_content ) . "\r\n\r\n";
    	$notify_message 			.= __( 'You can see all comments on this post here:' ) . "\r\n";
    	$notify_message 			.= "{$location}\r\n\r\n";//get_permalink($comment->comment_post_ID) . "#comments\r\n\r\n";
    	$notify_message 			.= sprintf( __('Permalink: %s'), $location) . "\r\n";
    
    	if ( user_can( $post->post_author, 'edit_comment', $comment->comment_ID ) ) {
    		if ( EMPTY_TRASH_DAYS ) {
    			$notify_message .= sprintf( __( 'Trash it: %s' ), admin_url( "comment.php?action=trash&c={$comment->comment_ID}#wpbody-content" ) ) . "\r\n";
    		} else {
    			$notify_message .= sprintf( __( 'Delete it: %s' ), admin_url( "comment.php?action=delete&c={$comment->comment_ID}#wpbody-content" ) ) . "\r\n";
    		}
    		$notify_message .= sprintf( __( 'Spam it: %s' ), admin_url( "comment.php?action=spam&c={$comment->comment_ID}#wpbody-content" ) ) . "\r\n";
    	}
    
    	return $notify_message;
    
    }, 20, 2 );

    Cheers!

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.