Home / download / Force users to be logged-in to download purchased files
  • How to Force users to be logged-in to download purchased files

    disable-new-user-notification-email-for-admin-and-users

    I know many of you are as big a fan of Easy Digital Downloads as I am, so you will love this snippet. This snippet for EDD will force users to be logged-in to download purchased files. It is also probably best that you use this as a plugin how Pippin Williamson intended.

    If you are looking to add a little more functionality to the login process check out this snippet to Capture users last login date and time.

    ( click code to copy )

    wordpress snippet : PHP

    <>
    class EDD_Force_Login {
    	function __construct() {
    		add_action( 'plugins_loaded', array( $this, 'load' ) );
    	}
    	public function load() {
    		add_action( 'edd_process_verified_download', array( $this, 'force_login' ), 10, 2 );
    	}
    	public function force_login( $download, $email ) {
    		if(  ! is_user_logged_in() ) {
    			$login_url = wp_login_url( trailingslashit( home_url() ) . '?' . $_SERVER['QUERY_STRING'] );
    			$message = sprintf( 'You must be logged in to download files. <a href="%s">Login here</a>.', $login_url );
    			wp_die( $message, 'Error' );
    		}
    	}
    }
    $GLOBALS['edd_force_login'] = new EDD_Force_Login();
    

    ( WordPress codex functions, hooks, in this snippet. )

    home, query, query_string, plugins_loaded, is_user_logged_in, add_action, trailingslashit, __, home_url, wp, wp_die,