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. Increase of failed login attempts, brute force attacks? [closed]
  8. How to prefill WordPress registration with social details
  9. Redirect user using the ‘wp_login_failed’ action hook if the error is ’empty_username’ or ’empty_password’
  10. wp_signon() does not authenticate user guidance needed
  11. Disable WordPress 3.6 idle logout / login modal window / session expiration
  12. How to pass users back and forth using session data?
  13. Avoid to load default WP styles in login screen
  14. Integrate recaptcha and wp_signon – what is needed?
  15. Programmatically log in a wordpress user
  16. Send reset password link to user from custom lost password form
  17. Change sign-on URLs for security purposes
  18. Can’t stop hacker trying to get admin access in WordPress blog after trying many ways [closed]
  19. Hook for fail and successful login actions
  20. How long do users stay logged in if they DON’T check remember me?
  21. How does WordPress track that a certain User is Logged-In
  22. Mobile users redirected to a different page on login unless linked to another post
  23. 2 wordpress blogs with 1 users table and 1 login
  24. How can i add validation to this login form with out it redirecting to the wp-login.php page
  25. Bootstrap Modal as login page
  26. Custom login page always redirecting to wp-login.php
  27. Force users to register in order to view website [duplicate]
  28. auto login after registeration for wp-members plugin
  29. Passing username to login screen
  30. Sniffing wordpress user’s credentials
  31. How to Get Logged-in to “Remote WP Site” from my local script (in Same Browser)?
  32. v5.6.2 User cannot stay logged in – wordpress_test cookie placed but not auth cookies
  33. How to set JWT token with PHP on successful login?
  34. wp-admin redirects to subdirectory after moving installation to subdirectory
  35. Share WordPress login info with other PHP app
  36. Facebook login for private group members
  37. WordPress c-panel login error [closed]
  38. Possible to display a button only if user are login?
  39. Disable all other page except index,register,login till user login
  40. Is wp_login_form secure on a non secure page?
  41. Which modification to login only certain role?
  42. Auto login after reset password
  43. Require login for specific templates
  44. Is there anyway to get the inputted password string from the login form?
  45. Invalidate username if it contains @ symbol
  46. WordPress Login and Register Link
  47. Login and Forgot password in Lightbox
  48. WordPress Login redirection according to user role
  49. Getting a person’s username from a wordpress cookie
  50. Prevent display password on wp-login.php
  51. WordPress ‘limit_login_lockouts’ using internal ip adress
  52. wp_get_current_user does not work properly on log in page
  53. How do I limit access to wp-admin to an IP range?
  54. Where is the php file, that does the checks for login information?
  55. WordPress Logout Problem [closed]
  56. “if is logged in” doesn’t work for me [closed]
  57. Timezone Change Locked Me Out? [closed]
  58. One time login on 2 different WordPress sites
  59. How are all users now set to inactive?
  60. Membership Plugin with Facebook integration [closed]
  61. Abnormal activity at url /my-account/add-payment-method/
  62. Login Based on ip
  63. Blacklist and Whitelist on login
  64. Is there a way to call via javascript if a user is logged-in on a static html file?
  65. URL Restrictions? Need only people who are logged in AND have a specific role (or roles) to access all pages for a site
  66. How to add custom authentication to wordpress login and register
  67. WordPress new version Version 5.3.2, user login not working after upgrade
  68. How the wordpress login and signup in react native app
  69. Link Users to external login db
  70. WordPress password reset not working
  71. Change WordPress Login URL to External URL
  72. User does not exist
  73. WordPress login is now working, it just refresh the page and nothing happens.
  74. New user password confirmation sending wrong URL
  75. is_logged_in not working after login
  76. wp_signon() does not authenticate user guidance needed
  77. How to login to wordpress via Cpanel
  78. Redirect to previous page after login
  79. How to password protect pages in WordPress
  80. User account activation links are lacking query strings
  81. Creating login for client / customer that will take them to customized part of site
  82. wp-login.php just refreshes the form fields
  83. Login just resets/reset password link also does not work
  84. WP login pages redirect to homepage (cannot login)
  85. How to place wp-login.php in page or page template?
  86. How to get rid of the username of registration form in theme my login wp plugin?
  87. Register and Connect links: where are they located?
  88. Adding A Login Fail Notice
  89. Give user some feedback when they land on custom login page
  90. Trouble logging in and/or changing password
  91. redirect not logged in users to specific page and redirect them back where they come from
  92. How to find out what’s causing (broken) ajax login
  93. How to put Login, Register and newsletter widget on the same page?
  94. Creating Custom Login Form Where Password Field is Dropdown Menu
  95. Cannot login into an old wp site. Fatal Error: Cannot create references to/from string offsets
  96. Browser Caching for Logged in Users
  97. how to login admin by using ip address
  98. Login form- no feedback
  99. Profile Builder Plugiin – how to add customize Login form fields?
  100. Can’t access wordpress mgt dashboard until propogation finished?
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
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 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