1

I'm redirecting users to the login page only when the user tries to access a single post page using:

function force_login() {

    if (!is_single()) return;

    if (!is_user_logged_in()) {
        auth_redirect();
    }
}

Now, I want to display a message in the login page just in this case, saying that the user needs to login to get access to this post.

I found that you can display a message in the login page using:

function post_login_message() {
    return '<p>Sample Login Message</p>';
}
add_filter( 'login_message', 'post_login_message' );

But this will display the same message everytime the user tries to login.

Thanks in advance.

2
  • is_single doesn't mean what you think it means, and has nothing to do with post pagination
    – Tom J Nowell
    Mar 7, 2018 at 15:23
  • 1
    This also looks like an XY problem, the root problem here is "How do I get the number of pages in a post?"
    – Tom J Nowell
    Mar 7, 2018 at 15:29

1 Answer 1

1

The problem here is a misunderstanding of what is_single means, reading the docs clarifies this:

https://developer.wordpress.org/reference/functions/is_single/

Is the query for an existing single post?

Description

Works for any post type, except attachments and pages

If the $post parameter is specified, this function will additionally check if the query is for one of the Posts specified.

So in a post listing, is_single is false, but on a blog post it is true. The same is true for a product, or another custom post type.

So this has more in common with is_page than pagination. A catch all function for this is is_singular

Remember, you can read the docs to verify things, and don't always assume. E.g. alternative bad assumptions that could be made:

  • is_single determines if the site has a single blog, and returns false if it's a multisite
  • is_single returns true if the post is the only post in a category
  • is_single checks if the user has a single item in their shopping cart

So How Do I Find Out How Many Pages a Post Has?

To display the pagination links, we use wp_link_pages, so we can pass the echo parameter and set it to false. This lets us test if it returned markup. If it did, then yes there's more than one page. If it did not, then no there is only 1 page

$pages = wp_link_pages( ['echo' => false ] );
if ( !empty( $pages ) ) {
    // more than 1 page
} else {
    // only 1 page
}

A More Specific But Not As Elegant Method

There is a global variable named $multipage that is true if there is more than one page

global $multipage;
if ( $multipage ) {
    // there's more than one page
}
3
  • Sorry, bad english. I really meant "single post page" where I wrote "single page posts". The single.php page. The problem is that I want to display a message in the login page only if it is redirecting the user after the login to a single post page.
    – sharon
    Mar 7, 2018 at 16:40
  • Oh, that's very different to what your question described, you should edit your question to say that
    – Tom J Nowell
    Mar 7, 2018 at 17:22
  • Edited. Thanks Tom, and sorry for the misunderstanding.
    – sharon
    Mar 8, 2018 at 14:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.