When I attempt the following I get Warning: Cannot modify header information - headers already sent by......
I'm trying to require a user to be logged in before they access my page with the shortcode on it.
What am I missing? Thanks a million for any help you can offer.
add_shortcode( 'guest-posts', 'guestposts_shortcode' );
function guestposts_shortcode( $atts ) {
auth_redirect();
}
It may work if you parse the post content before to render it. Then you should check if you find the shortcode inside the content.
Here is a small generic function to check it :
function has_shortcode($shortcode = '') {
global $post;
if (!$shortcode || $post == null) {
return false;
}
if ( stripos($post->post_content, '[' . $shortcode) !== false ) {
return true;
}
return false;
}
Now we have to create a function that will check for our specific shortcode :
function unlogged_guest_posts_redirect() {
if(has_shortcode('guest-posts') && !is_user_logged_in()) {
auth_redirect();
}
}
Then we have to hook our function (I think this can work in "wp" hook, but you can try anohter if it does not) :
add_action('wp', 'unlogged_guest_posts_redirect');
To finish, we have to ensure that the shortcode won't echo anything :
add_shortcode( 'guest-posts', 'guestposts_shortcode' );
function guestposts_shortcode( $atts ) {
return false;
}
Actually we are dealing with shortcodes but we're not using the WordPress shortcode API. This functionnality should be done using a custom field, it would be simpler !