Quick Info
I am using the Posts 2 Posts plugin in order to create a relation between Teachers and the Classes they teach.
- The teacher’s page is a custom post type of
faculty
in which teachers are added to. -
A
faculty
post has a custom meta field labeledemail_address
. -
The classes page is a custom post type of
class
in which classes are added to.
The Posts 2 Posts plugin allows me to connect the two post types, class
and faculty
together. By doing so, I can grab a teacher’s email_address
and title
and display it on the class
page, and I can display a link to the class
page on the teachers page.
Outside of this… Teachers will be able to create accounts using their school email. That same email address should match the email_address
that was entered by an admin on the faculty
post.
The Goal
I want to allow teachers to log in and only be able to edit the class
page that is associated with the faculty
page that has a custom meta field email_address
that matches their user email address.
I know this is possible, as the Role Scoper plugin allows you to do just that. The only difference is that in the Role Scoper plugin, you have to manually go into the settings, show all the pages associated with class
post type, select a user by username (not email), and add them to that page. It is cumbersome and a pain for a non-technical user to go about doing it this way.
My way would allow you to create a faculty
with a specific email_address
and link it to a class
page. Once a user logs in with a matched email address, they would see that class
page to edit.
Hopefully all this makes sense, I’m not really sure where to begin and I’ve spent half the day looking for answers… all which lead back to Role Scoper (which I do not want to do).
The Question
How do I allow a user to edit a single page, that is related to another page, which has a custom meta field that contains an email address that matches theirs?
Boy does that sound complicated… Much thanks and appreciation in advance for any and all help!
Process Walkthrough
- User logs in
- Grab user’s email address
- Match
faculty
post where custom meta field ofemail_address
matches user’s email - Get that
faculty
post ID - Find
class
post(s) that is/are linked (via Posts 2 Posts) to thatfaculty
ID. - Give user permission to edit
class
post(s).
While the process seems pretty straight forward, I have no idea how to actually give a user permission to edit a specific custom post type page. Again, Role Scoper is capable of doing it, the next question is how.
I think the simplest way to do this would be to give the users a customized Author role when they register. The capabilities of the default Author role are that they can only create and edit their own posts. You’ll have to customize this to enable capabilities for your ‘class’ custom post type, not just vanilla posts. You can hook the
user_register
action to query for a faculty post with meta value matching the registered user’s email, then update the user to give them your special role if a matching faculty post is found. For the class posts, hooksave_post
to add a connection between that post and the user’s faculty page whenever they create a new class post.Instead of Faculty, why not add user meta and use Posts 2 Posts to relate Posts to Users. I’ve used this approach to successfully build several membership-based sites. That way you get to lean on all the nice wp_user based functions and securities.