!is_admin() condition turns true in admin pages

redirecting not logged in user is the only thing that works 🙂 in the
backend.

What you are describing is very peculiar. template_redirect is a front-end hook. Your code will never run on the backend, as far as I can tell. You can demonstrate that with this line:

add_action('template_redirect',function() {die;});

You can traverse the back-end all you want, but try to get to the front and you get nothing. I really think you have the analysis backwards. Your code should never be executing in the back-end at all.

I put die; at the top of your function and get the same results. It does not execute in the back-end.

What you are probably seeing is “not logged in” users getting shuffled to the front end, at which point that code runs.

All I can think is that there is some other redirect that is giving the illusion of this execution on the back-end.

While initially writing this answer, I missed the obvious. You shouldn’t need to redirect a “not logged in” user from the backend to the login screen. WordPress will do that automatically– or should unless something has done something unwise to your site.

All you should really need is this:

add_action( 
  'template_redirect', 
  function () {
    wp_redirect(admin_url());
  }
  ,10
);

That pretty much completely prevents access to the front end, which sound like what you describe:

I need users to redirect to login if not logged and once they are,
redirect them to backend if they try to see front end pages.

Is that really what you want?

error code: 523