How to dynamically register custom post type

That’s not how post type and taxonomy registration work, they are already dynamic. You must call register_post_type on every request, no exceptions, and never later than the init hook. If you do not do this, then the CPT doesn’t exist.

register_post_type and register_taxonomy store their data in PHP variables, and do not save that data anywhere, so once the request ends that information disappears and needs to be re-registered on the next request. So it doesn’t make sense to modify a CPT’s definition the way you describe.

It also can’t be called later than the init hook. This means it is not possible to register a custom post type in an form/AJAX/REST API handler, that is too late and also won’t be passed on to the next request because of the way registration works.

Instead, you can receive new values the same way you would any other form or AJAX, save them in the database as options or another way. Then on the init hook, before you call register_post_type/register_taxonomy, grab those values you saved earlier and use them in the arguments.


Remember PHP applications aren’t like Node.js sites, they don’t have long-running background tasks that wait for requests. Instead they’re loaded fresh from the disk, generate a response, then they end. All variables dissapear at the end of the request, and the next request starts from a blank slate.

Additionally, if you try to change the URL or anything that’s used in the URL, then you need to flush and resave permalinks.