Title: Using auth_redirect() from shortcode inititated code
Last modified: August 19, 2016

---

# Using auth_redirect() from shortcode inititated code

 *  [Warren Harrison](https://wordpress.org/support/users/hungrymedia/)
 * (@hungrymedia)
 * [15 years, 2 months ago](https://wordpress.org/support/topic/using-auth_redirect-from-shortcode-inititated-code/)
 * I am working on a plugin that uses a shortcode to allow the site admin to add
   the plugin functionality to any page. The issue I’m running into is that the 
   plugin requires the current user to be logged in, so I’m using auth_redirect()
   as the first statement in the function called by the shortcode. The user is never
   redirected to the login page, however.
 * If checking the logs I see that this auth_redirect() is occurring after the page
   headers have been output:
    `PHP Warning: Cannot modify header information - headers
   already sent by (output started at /var/www/sites/xxxx/wp-includes/nav-menu-template.
   php:235) in /var/www/sites/xxxx/wp-includes/pluggable.php on line 897`
 * Makes sense to me… the shortcode is within the page body. By this time, the page
   headers are already sent.
 * I’m wondering if there’s a better way to accomplish the following prior to headers
   being sent
    1) Check that the current page contains the required shortcode 2)
   If the shortcode matches and user is not logged in, redirect to login page
 * Any pointers are greatly appreciated.

Viewing 1 replies (of 1 total)

 *  [krauter](https://wordpress.org/support/users/krauter/)
 * (@krauter)
 * [15 years ago](https://wordpress.org/support/topic/using-auth_redirect-from-shortcode-inititated-code/#post-2026777)
 * You can add this to functions.php to buffer the entire output of wordpress:
 *     ```
       function ob_start_global ()
       {
       	ob_start ();
       }
       add_action ('init', 'ob_start_global');
   
       function ob_flush_global ()
       {
       	echo ob_get_clean ();
       }
       add_action ('shutdown', 'ob_flush_global');
       ```
   
 * I use that to do redirects from shortcodes.
 * Hope this helps.

Viewing 1 replies (of 1 total)

The topic ‘Using auth_redirect() from shortcode inititated code’ is closed to new
replies.

## Tags

 * [auth_redirect](https://wordpress.org/support/topic-tag/auth_redirect/)
 * [redirect](https://wordpress.org/support/topic-tag/redirect/)
 * [shortcode](https://wordpress.org/support/topic-tag/shortcode/)

 * 1 reply
 * 2 participants
 * Last reply from: [krauter](https://wordpress.org/support/users/krauter/)
 * Last activity: [15 years ago](https://wordpress.org/support/topic/using-auth_redirect-from-shortcode-inititated-code/#post-2026777)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
