CoursePress and New BlogTemplates

When we create a Blog template (New Blog Templates) based on site with CoursePress, the new sites inherit the email settings (CoursePress -> Settings -> Email Settings), so, it will use the "From" email from the blog template, instead of update to the from email of the new site.

Is there any way (custom function) to force the CoursePress Email Settings to replace the "from" email to the email from the new site?

  • Panos

    Hey antKat ,

    Could you please try inserting the following snippet in your main site's functions.php file or even better in a mu-plugin ( please read hear about mu-plugins ) :

    add_action( 'blog_templates-copy-after_copying', function( $template, $new_blog_id, $user_id ){
    
    	switch_to_blog( $new_blog_id );
    
    	$admin_email = get_bloginfo( 'admin_email' );
    	$blog_name = get_bloginfo( 'name' );
    
    	$cp_settings = get_option( 'coursepress_settings' );
    
    	$cp_email_vars = array(
    		'basic_certificate',
    		'registration', 'enrollment_confirm',
    		'course_invitation',
    		'course_invitation_password',
    		'instructor_invitation',
    		'new_order'  );
    
    	foreach( $cp_email_vars as $cp_email_var ){
    		$cp_settings['email'][ $cp_email_var ]['email'] = $admin_email;
    		$cp_settings['email'][ $cp_email_var ]['from'] = $blog_name;
    
    	}
    
    	update_option( 'coursepress_settings', $cp_settings );
    
    	restore_current_blog();
    
    }, 10, 3 );

    and let us know if this works for you?

    Thanks!

  • antKat

    Hi Panos,
    Thank you for the help. I created a mu-plugin with the code that you provided but after installing via ftp to the mu-plugins folder, the code displayed at the top on the front-end . Here is a link of file in dropbox: https://www.dropbox.com/s/x400zrduu8tgxv6/coursepress-blogtemplate-update-user.zip?dl=0

    This will be great if it works. Another item I would love to see changed in coursepress when a new site is created using template is the Course Instructor. It would be great to remove the copied instructor and replace with the user ID of the blog admin_email. I currently have 11 courses that will be included in the template but the instructor has to be fixed for all of them.

    One last item, but I believe it is in the export/import of coursepress is that the course category is not included in the process.

    Again, thank you for your help.

  • Panos

    Hey antKat ,

    It's php code so it requires the php tag in front of it. So in top of your file add
    <?php
    and then add a new line and paste the code.

    This is for the mu-plugin file. For the theme's functions.php you shouldn't add this php tag again as it already exists there and would through an error.

    So for the mu-plugin the entire code should be:

    <?php
    add_action( 'blog_templates-copy-after_copying', function( $template, $new_blog_id, $user_id ){
    
    	switch_to_blog( $new_blog_id );
    
    	$admin_email = get_bloginfo( 'admin_email' );
    	$blog_name = get_bloginfo( 'name' );
    
    	$cp_settings = get_option( 'coursepress_settings' );
    
    	$cp_email_vars = array(
    		'basic_certificate',
    		'registration', 'enrollment_confirm',
    		'course_invitation',
    		'course_invitation_password',
    		'instructor_invitation',
    		'new_order'  );
    
    	foreach( $cp_email_vars as $cp_email_var ){
    		$cp_settings['email'][ $cp_email_var ]['email'] = $admin_email;
    		$cp_settings['email'][ $cp_email_var ]['from'] = $blog_name;
    
    	}
    
    	update_option( 'coursepress_settings', $cp_settings );
    
    	restore_current_blog();
    
    }, 10, 3 );

    Please let me know if you need any further assistance :slight_smile:

    Cheers!

  • Panos

    Hi antKat ,

    This requires some additions, it's simpler to just replace all the previous code with the following:

    <?php
    add_action( 'blog_templates-copy-after_copying', function( $template, $new_blog_id, $user_id ){
    
    	switch_to_blog( $new_blog_id );
    
    	if( ! class_exists( 'CoursePress_Data_Course' ) ){
    		return;
    	}
    
    	$admin_email = get_bloginfo( 'admin_email' );
    	$blog_name = get_bloginfo( 'name' );
    
    	$cp_settings = get_option( 'coursepress_settings' );
    
    	$cp_email_vars = array(
    		'basic_certificate',
    		'registration', 'enrollment_confirm',
    		'course_invitation',
    		'course_invitation_password',
    		'instructor_invitation',
    		'new_order'  );
    
    	foreach( $cp_email_vars as $cp_email_var ){
    		$cp_settings['email'][ $cp_email_var ]['email'] = $admin_email;
    		$cp_settings['email'][ $cp_email_var ]['from'] = $blog_name;
    
    	}
    
    	update_option( 'coursepress_settings', $cp_settings );
    
    	$course_args = array(
    		'post_type' => CoursePress_Data_Course::get_post_type_name(),
    	);
    	$courses = new WP_Query( $course_args );
    
    	foreach( $courses->posts as $course ){
    
    		$course_instructors = CoursePress_Data_Course::get_setting( $course->ID, 'instructors', array() );
    
    		if( ! empty( $course_instructors ) ){
    			foreach( $course_instructors as $course_instructor ){
    				CoursePress_Data_Course::remove_instructor( $course->ID, $course_instructor );
    			}
    		}
    
    		CoursePress_Data_Course::add_instructor( $course->ID, $user_id );
    
    	}	
    
    	restore_current_blog();
    
    }, 10, 3 );

    I have included the opening php tag "<?php".

    Please give it a try and let us know how it goes :slight_smile:

    Thanks!

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.