• 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

    (@krauter)

    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.