Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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 labeled email_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

  1. User logs in
  2. Grab user's email address
  3. Match faculty post where custom meta field of email_address matches user's email
  4. Get that faculty post ID
  5. Find class post(s) that is/are linked (via Posts 2 Posts) to that faculty ID.
  6. 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.

share|improve this question
    
Have you tried to build any code yet? You seem to have your approach down pat. –  Brian Fegter Sep 23 '12 at 3:25
    
Let me just throw this out there: a faculty member is a WordPress user to begin with. That user could be assigned to a custom role called faculty. Any additional data that needs to be saved for those users in that role can be stored via user meta data. Now, for your class custom post type, you'd have a taxonomy. The taxonomy options would be mirrored to users of type faculty. You can then select, per class, one or more faculty members (in case a class is taught by 1+). When the user logs in of type faculty, you display only those classes in which they are assigned for in the taxonomy. –  NightHawk Nov 2 '12 at 4:13
    
There are a few benefits. 1. If email addresses change, they change in one place and everything continues to work. 2. Since you are using built-in WordPress components (users, taxonomy, roles), there are WordPress functions to help make what you want happen. 3. It keeps things flexible, in case you want to build on them or generate reports later. –  NightHawk Nov 2 '12 at 4:18

2 Answers 2

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, hook save_post to add a connection between that post and the user's faculty page whenever they create a new class post.

share|improve this answer
    
There is already a connection between the class post and faculty post established. As for giving them a special role... that still doesn't take into account that I want them to specifically edit a single page. How would WP know that user X can edit class post 2? You've given me a few other things to think about though. I'll look and see what I can find out. –  rebz Sep 19 '12 at 0:26
    
update their class post(s) to make them the author, they can only edit posts they are the author of. –  Milo Sep 19 '12 at 0:40
    
I see what you're saying... I'll look into that as well. I believe I have just found a different solution. I'll have to work on a bit more; refine it. But it looks promising. I'll post the solution once I get it. –  rebz Sep 19 '12 at 0:41
    
additionally, although the posts they are not author of will not be click-able in the admin panel, you can hide them entirely using the pre_get_posts action and setting the author argument to the logged in user's author ID. –  Milo Sep 19 '12 at 0:45

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.