Force User Login For Free Units

I'd like to force users to sign-up and login in order to access the free units offered.

I've tried using Protected Content a few different ways but it causes a few issues, or doesn't work as I'd like:

1) I tried protecting Custom Course Types > "course". But that makes guests unable to view the course summary page (myurl.com/courses/mycourse/).

2) Tried Protecting Custom Course Types > "units" thinking maybe they'd be unable to access the units themselves, but that made the Course Structure disappear, and thus they are unable to navigate to the "Free" link. Aside from the fact they can't navigate to the free content (and thus be asked to log in), I'd still like the course structure to show up so potential students can see an overview.

3) Finally I tried using URL Restrictions and tried to restrict access to "myurl.com/courses/mycourse/units/" which should hide that and any child pages. This didn't seem to have any effect whatsoever. When clicking the "Free" link it would lead them to the page without signing in.

Any other ideas? Really I'm just trying to capture their email addresses so I can follow up if they don't continue on to the paid units of the course, and thought forcing them to login would be the easiest way.

Sidenote: After installing Protected Content the "Instructors" area on the course summary page has lost/altered the CSS which applies to it. I'm using a child theme, but have done little CSS editing (aside from changing some colouring) to the default CoursePress theme.

  • Ash

    Hello @youngkyleknapp

    Welcome to WPMU community!

    I hope you are well today and thanks for asking the question.

    Thanks for enabling support access. I wanted to check, but it seems the courses page never loaded for me. It was just loading, and it was not complete. Would you please check again?

    If you give me an url of the free page, I can give you code that if logged in users view that page, it will redirect you to your login page.

    Please let us know.

    Cheers
    Ash

  • youngkyleknapp

    Not sure why the courses page isn't loading. I never noticed but there are no courses listed there anymore. It's the default virtual page created by the plugin, so not sure why it's empty (I have 2 courses created for testing purposes):
    https://musicindustry101.net/courses/

    The free content are units within this course. If you navigate to the "free" link on any of the course units, the destination is where I would like to restrict access to people who have registered for the site, but not necessarily purchased the course:
    https://musicindustry101.net/courses/music-industry-101/

  • Ash

    Hello @youngkyleknapp

    I hope you are well today and I am sorry for the delay.

    Please use the following code:

    function my_page_template_redirect()
    {
    	$url = current_page_url();
    
        if( strpos( $url,'?try') !== false )
        {
    
        }else{
    	wp_redirect( 'https://musicindustry101.net/student-login/?msg=You+need+to+login+to+view+the+free+units' );
    	exit();
        }
    }
    add_action( 'template_redirect', 'my_page_template_redirect' );
    
    add_action( 'wp_footer', 'login_msg' );
    function login_msg() {
    	$msg = isset( $_REQUEST['msg'] ) ? str_replace( '+', ' ', $_REQUEST['msg'] ) : '';
    	if( $msg != '' ){
    		?>
    		<script type="text/javascript">
    		jQuery(function($) {
    			$('from#student-settings').prepend('<p><?php echo $msg; ?></p>');
    		});
    		</script>
    		<?php
    	}
    }
    
    function current_page_url() {
        $url = 'http';
        if ($_SERVER["HTTPS"] == "on")
            $url .= "s";
        $url .= "://";
        if ($_SERVER["SERVER_PORT"] != "80")
            $url .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
        else
            $url .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
    
        return $url;
    }

    You can add those codes in your functions.php in the theme, if you think your theme won’t be changed. Otherwise mu-plugins is the best solution. To use mu-plugins, go to /wp-content/ and find the folder with name 'mu-plugins'. If there is no folder in that name, then create a folder, name it 'mu-plugins', create a file inside that, give any name you like and paste the code in there. You don't need to activate that plugin. Mu-plugins means must use plugins, so it will be activated automatically always. If you use mu-plugins then add a php start tag at the beginning of the code.

    Hope it helps :slight_smile: Please feel free to ask more question if you have any.

    Cheers
    Ash

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.