Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

Get WordPress login functions without printing anything

I think you mean the Content-Disposition header.

I recently did a very similar thing for a client. My weapon of choice: custom rewrite rules.

First, add the rewrite:

<?php
add_action( 'init', 'wpse27232_add_rewrite' );
/**
 * Adds the rewrite rule for the download.
 * 
 * @uses add_rewrite_rule
 */
function wpse27232_add_rewrite()
{
    add_rewrite_rule(
        '^download/?$',
        'index.php?file_download=true',
        'top'
    );
}

When someone visits yoursite.com/download they will catch this rewrite. Notice the index.php?file_download=true bit. WordPress has no clue that file_download should be a recognized query variable. So let’s make that recognizable by filtering query_vars.

<?php
add_filter( 'query_vars', 'wpse27232_query_vars' );
/**
 * Filter our query vars so WordPress recognizes 'file_download'
 */
function wpse27232_query_vars( $vars )
{
    $vars[] = 'file_download';
    return $vars;
}

Now the fun part: catch the file_download query variable and send the file.

<?php
add_action( 'template_redirect', 'wpse27232_catch_file_dl' );
/**
 * Catches when the file_download query variable is present.  Sends the content
 * header, the file, and then exits.
 */
function wpse27232_catch_file_dl()
{
    // No query var?  bail.
    if( ! get_query_var( 'file_download' ) ) return;
    
    // change this, obviously. Should be a path to the pdf file
    // I wrote this as a plugin, hence `plugin_dir_path`
    $f = plugin_dir_path( __FILE__ ) . 'your-file.pdf';
    if( file_exists( $f ) && is_user_logged_in() )
    {
        // Do your additional checks and setup here
        
        // Send the headers
        header('Content-Type: application/pdf');
        header('Content-Disposition: attachment; filename=" . basename( $f ) );
        header( "Content-Length: ' . filesize($f));
        // You may want to make sure the content buffer is clear here

        // read the pdf output
        readfile($f);
        exit();
    }
    else
    {
        global $wp_query;
        $wp_query->is_404 = true;
    }
}

Finally to get this little plugin to work, an activation hook to flush the rewrite rules.

<?php
register_activation_hook( __FILE__, 'wpse27232_activation' );
function wpse27232_activation()
{
    wpse27232_add_rewrite();
    flush_rewrite_rules();
}

Related Posts:

  1. Custom Connect to Facebook, problem logging in/logging out
  2. What would be the best way to implement Magic Link logins in WordPress?
  3. Best option to implement external register/login to WP from self-made API
  4. Set logged in user based on API response
  5. How to use google api for wordpress login
  6. Login and register by API
  7. How to use WordPress REST API to login a user with dart/flutter?
  8. In Django, how do I know the currently logged-in user?
  9. Can I programmatically login a user without a password?
  10. Can’t log in: “ERROR: Cookies are blocked or not supported by your browser. You must enable cookies to use WordPress.”
  11. Is there any way to rename or hide wp-login.php?
  12. How to login with email only no username?
  13. How can I redirect user after entering wrong password?
  14. Increase of failed login attempts, brute force attacks? [closed]
  15. Login page ERROR: Cookies are blocked due to unexpected output
  16. Separate registration and login for different roles
  17. SSO / authentication integration with external ‘directory service’
  18. Preventing session timeout
  19. How reduce wordpress login session timeout time?
  20. How to prefill WordPress registration with social details
  21. Check for correct username on custom login form
  22. Disallow user from editing their own profile information
  23. I can’t access my site via wp-admin
  24. ‘Password field is empty’ error when using autofill in Chrome
  25. Removing username from the ‘wordpress_logged_in’ cookie
  26. How to show ‘login error’ and ‘lost password’ on my template page?
  27. What is $interim_login?
  28. Custom login form
  29. How to prefill the username/password fields on the login page
  30. wp_signon returns user, but the user is not logged in
  31. Adding extra authentication field in login page
  32. Prevent wp_login_form() from redirecting to wp-admin when there are errors
  33. Redirect user using the ‘wp_login_failed’ action hook if the error is ’empty_username’ or ’empty_password’
  34. wp_signon() does not authenticate user guidance needed
  35. What exactly is ReAuth?
  36. What are the differences between wp_users and wp_usermeta tables?
  37. Login members using web services
  38. Make my wordpress blog remember my login “forever”
  39. How to check in timber if user is loggedin?
  40. How do I change the language of only the login page?
  41. Disable WordPress 3.6 idle logout / login modal window / session expiration
  42. Stop WordPress from logging me out (need to keep me logged in)
  43. Woocommerce registration page [closed]
  44. How to disable autocomplete on the wp-login.php page
  45. Share login data/cookies between multiple installations
  46. Synchronize WordPress user accounts across multiple domains and installations without using WordPress MU
  47. How to pass users back and forth using session data?
  48. How do I change the logo on the login page?
  49. Why does WordPress hide the reset password key from the URL?
  50. Is it possible to sign in with user_email in WordPress?
  51. How to use current_user_can()?
  52. Avoid to load default WP styles in login screen
  53. WordPress registration message
  54. How to fake a WordPress login?
  55. how to display the wordpress login and register forms on a page?
  56. Does wp_logout_url() destroy a session? (Logging out question)
  57. How can I send a welcome email to a user AFTER they login for the first time?
  58. Can not login with correct username and password
  59. Website Visible only to Registered users
  60. How can i increase the login expiration length?
  61. How do I use add_action from a class method?
  62. How to remove the WordPress logo from login and register page?
  63. How can I add a custom script to footer of login page?
  64. Brute force attack?
  65. Customize wp_new_user_notification_email()
  66. Need to execute a cron job
  67. Login email after registration never sent or received
  68. How can I create a separate blog that is private?
  69. How to keep always logged in development environment
  70. Add Confirm Password field in wp-login.php Password Reset page
  71. Integrate recaptcha and wp_signon – what is needed?
  72. Stop users from logging in from multiple locations
  73. I want to disable E-Mail verifcation / activation when a user signs up for my WordPress site
  74. custom login page redirect to logged in user profile page
  75. Email address or username used to login in wordpress
  76. How do I check if a post is private?
  77. Front-end login: Redirect user to the post they had created
  78. Receiving “This content cannot be displayed in a frame” error on login page
  79. My login form does not work
  80. Programmatically log in a wordpress user
  81. Action wp_login_failed not working if only one field is filled out
  82. Getting “Cookies are blocked or not supported by your browser” on login page
  83. What is the purpose of logging out after WordPress upgrade?
  84. Is it alright for two people to simultaneously be logged into a WP site as administrator?
  85. wp-login.php redirecting to HTTPS
  86. Display last login time
  87. How to customise wp-login.php only for users who are setting a password for the first time?
  88. Intentionally Force Failed Login WordPress
  89. How do I turn off the ability to login?
  90. Gaining Login Access via the Database
  91. How can I test the login for an expired session?
  92. Give visitor access to password protected page/post via external script
  93. Send reset password link to user from custom lost password form
  94. What hooks should I use for pre-login and pre-registration actions?
  95. WordPress Login Footer URL
  96. Remove built in wordpress login and use only google auth
  97. Websites defaced by uploading script using theme editor
  98. wp_login action hook not working
  99. Make wordpress admin failed login attempt return 401
  100. Cookie settings for session across WPML subdomains using custom AJAX login
Categories login Tags api, login
How to use taxonomy term as permalink folder
Shortcode, Concatenating & Parse error: syntax error, unexpected T_FOREACH

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Post Navigation Elementor
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • WPFacet multiple loop displaying duplicate content
  • Get the name of the template/*html file used
  • Fix: WordPress.DB.PreparedSQL.NotPrepared Plugin Check (PCP)
  • Removing unnecessary CSS and JS code from wp_head()
  • hexagonal image gallery. Am I on the right track? [closed]
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress