Halloween Sale 15% off Ends in See the plans
PHP

Members-Only Download Links

Adds a [wpide_member_download id="123"] shortcode that gives logged-in members a personal download link for a Media Library file and shows visitors a login link instead.

Don’t have WPIDE yet? Install it on your site

Code

add_shortcode( 'wpide_member_download', function ( $atts ) {
    $atts = shortcode_atts( array( 'id' => 0, 'label' => 'Download' ), $atts, 'wpide_member_download' );
    $id   = absint( $atts['id'] );

    if ( ! $id || 'attachment' !== get_post_type( $id ) ) {
        return '';
    }

    if ( ! is_user_logged_in() ) {
        return '<a class="wpide-member-download is-locked" href="' . esc_url( wp_login_url( get_permalink() ) ) . '">' . esc_html( 'Log in to download' ) . '</a>';
    }

    $url = wp_nonce_url( add_query_arg( 'wpide_download', $id, home_url( '/' ) ), 'wpide_download_' . $id );

    return '<a class="wpide-member-download" href="' . esc_url( $url ) . '">' . esc_html( $atts['label'] ) . '</a>';
} );

add_action( 'init', function () {
    if ( empty( $_GET['wpide_download'] ) ) {
        return;
    }

    $id = absint( $_GET['wpide_download'] );

    if ( ! is_user_logged_in() ) {
        auth_redirect();
    }

    if ( ! isset( $_GET['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( $_GET['_wpnonce'] ), 'wpide_download_' . $id ) ) {
        wp_die( 'This download link has expired. Please go back and click it again.', 'Download', array( 'response' => 403 ) );
    }

    $file    = get_attached_file( $id );
    $uploads = wp_get_upload_dir();

    if ( 'attachment' !== get_post_type( $id ) || ! $file || ! is_readable( $file ) || 0 !== strpos( wp_normalize_path( realpath( $file ) ), wp_normalize_path( realpath( $uploads['basedir'] ) ) ) ) {
        wp_die( 'File not found.', 'Download', array( 'response' => 404 ) );
    }

    nocache_headers();
    header( 'Content-Type: ' . ( get_post_mime_type( $id ) ? get_post_mime_type( $id ) : 'application/octet-stream' ) );
    header( 'Content-Disposition: attachment; filename="' . sanitize_file_name( wp_basename( $file ) ) . '"' );
    header( 'Content-Length: ' . filesize( $file ) );
    header( 'X-Content-Type-Options: nosniff' );

    while ( ob_get_level() ) {
        ob_end_clean();
    }

    readfile( $file );
    exit;
} );

Notes

Links are tied to the member's session, so shared links do not work for others. The file's normal uploads URL is still public unless your server blocks it.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.