Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WordPress CPT With Ability to Login and Register

Tags:

php

wordpress

We're responsible for a WordPress plugin which, as part of it's functionality, has a Custom Post Type called 'Applicant'. These are applicants looking to purchase property so against a post you can record things like their contact details, and search requirements.

Now... it's come to light that we need to enable these applicants to be able to login and perform various actions such as save properties to a 'Favourites' list, or edit their own requirements.

If we were building the plugin from scratch I would've just done them as users, however this is a plugin used by hundreds of people so we don't have that luxury and must keep it as a CPT.

My question is... how can I/should I keep it a CPT whilst allowing these people to login and register.

My two initial thoughts are:

  1. For every custom post you have a WordPress user and keep the two synced (i.e. if the user is deleted, the custom post gets deleted at the same time). That way you could use the built-in log in and security functionality provided by WordPress, but you have this nightmare of trying to keep the two in sync.

or

  1. We build our own custom 'login' and 'register' functionality. We store the email address and password against the custom post and use that to validate them. Then also perform our own session management etc.

or

  1. The final option is we do infact scrap the CPT altogether and just use 'users'. Then write some kind of migration script to move the CPT's over to users.

Hope that makes sense. Any thoughts/ideas most welcome.

like image 698
BIOSTALL Avatar asked Aug 12 '16 16:08

BIOSTALL


2 Answers

I've written and managed 2 page builders and over a dozen other plugins for WooThemes Storefront, WooThemes Canvas and WooCommerce and a few stand alone plugins, from my experience...

The final option is we do infact scrap the CPT altogether and just use 'users'. Then write some kind of migration script to move the CPT's over to users.

In my experience, migration scripts are fine for small changes, like, maybe in your case adding a new default meta field for post type, but changing something so fundamental to the plugin functions would not only require lot of work (like querying users instead posts) but also drive your users mad, who use like maybe use custom post loops... So this is Not Recommended

We build our own custom 'login' and 'register' functionality. We store the email address and password against the custom post and use that to validate them. Then also perform our own session management etc.

This is again too much of work for something that comes built into WordPress... Probably skip this as well...

For every custom post you have a WordPress user and keep the two synced (i.e. if the user is deleted, the custom post gets deleted at the same time). That way you could use the built-in log in and security functionality provided by WordPress, but you have this nightmare of trying to keep the two in sync.

So it turns out that the first option seems to be the best option... You can simply use user_register and profile_update hooks to keep posts synced with users and save_post hook to keep users synced with posts. And a plugin activation hook to generate users from posts on activation of new version.

Hope that helps. 🙂

like image 102
shramee Avatar answered Oct 29 '22 14:10

shramee


I would prefer to use users with roles in wordpress.

You can create another role "Applicant" and assign required capabilities to that. This way it will be part of user module and you can add as much as possible information in user_meta, which is by default used by wordpress to store user extra information.

Few reference of adding user role and capabilities and user_meta.

Add Role

Add Capability

Add User Meta

Hope this will help you to proceed in standard way of wordpress CMS.

like image 31
Kapil Yadav Avatar answered Oct 29 '22 14:10

Kapil Yadav