How to register custom post types in a plugin?

The init hook is the first hook allowed. If called earlier it won’t work.

See WP Codex: https://developer.wordpress.org/plugins/post-types/registering-custom-post-types/

Create or modify a post type. register_post_type should only be invoked through the ‘init’ action. It will not work if called before ‘init’, and aspects of the newly created or modified post type will work incorrectly if called later.