WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Restrict Reading by Category Using Functions.php? (16 posts)

  1. fredhead
    Member
    Posted 3 years ago #

    Hi,

    I have a question that may or may not be possible. There are great plugins to manage roles within WordPress. And I got one that works and does what I need. However, I'm trying as much as possible to use the functions.php file to minimize code and keep my site efficient.

    My question: Is there a set of WordPress functions that would let me not only create roles and capabilities (e.g. add_role, add_cap) but also assign categories to a specific role?

    Here's how it would work. I'd create a role called Group A and categories called Cat 1, Cat 2, and Cat 3. The function would simply say Group A can read Cat 1 and Cat 2. If I assign a user to Group A, only content from Cat 1 and Cat 2 would be displayed to them. If I create a Group B, the same function could be used to say Group B can read Cat 2. And so on.

    Anyone know if this is possible within WordPress using functions.php?

  2. keesiemeijer
    moderator
    Posted 3 years ago #

    try it with this: http://codex.wordpress.org/Function_Reference/current_user_can

    Maybe try with a capability plugins?

  3. keesiemeijer
    moderator
    Posted 3 years ago #

  4. fredhead
    Member
    Posted 3 years ago #

    [chuckling here] Role Scoper definitely does the trick. Problem is that it has a lot of screens and complexity that, in my case, feel spurious. I'll use it if I have to but my hunch is that WordPress either has native capability to what I want (limit display based on group and category) without alot of screens OR I can hack it using existing WordPress code.

    For example, I found this interesting article:

    http://www.problogdesign.com/wordpress/how-to-write-a-restricted-content-plugin/

    which has me thinking that, if WordPress doesn't have a native function to assign categories to groups to determine what is displayed (content and navigation links), then my question becomes, "How can I adapt the query/queries that generate the content and navigation links so the query pulls content based on the viewer's group and categories assigned to that group?" I would then write a few short functions to define the groups as roles and then define categories/capabilities for each role. Then modify the queries in my templates as needed.

    Any ideas how and where to affect the query (e.g. update it) that displays content and navigation links? Is that WP_Query? Is the best approach to modify the native WordPress query or override it?

  5. keesiemeijer
    moderator
    Posted 3 years ago #

    If this is about showing different content only on the front end of your website you can use the current_user_can() function to alter the query.

    something like this:

    if(current_user_can('groupA')) {
    $args = 'cat=1,2,3';
    $the_query = new WP_Query( $args );
    }
    elseif(current_user_can('groupB')){
    $args = 'cat=4,5,6';
    $the_query = new WP_Query( $args );
    } else {
    // default query
    $args = 'cat=7'
    $the_query = new WP_Query( $args );
    }
    // the loop
    ...
  6. fredhead
    Member
    Posted 3 years ago #

    Brilliant, that's exactly the kind of direction I'm looking for, thank you! I'll let you know how it goes. This is strictly limited to front end display which I should've made clear.

  7. fredhead
    Member
    Posted 3 years ago #

    Actually I wonder if there is a way to automate this across content types so that all queries that display data use the category restrictions? Like an add_action and/or add_filter?

  8. keesiemeijer
    moderator
    Posted 3 years ago #

  9. fredhead
    Member
    Posted 3 years ago #

    Interesting. In playing around, I realize my requirement actually is a bit different than what I thought. On the Author/User page, I need to add a set of checkboxes that only the Admin role sees (and can set). Then use the settings to build a tax_query as described at the link you point to.

    I'm realizing, in other words, that my external users will need to be assigned to multiple groups.

    If you have any favorite links around how to muck with the Author/User admin page, to add check boxes, in particular, I'd be grateful. At the least, I'm off to dive even more deeply into this stuff. Appreciate your help.

  10. keesiemeijer
    moderator
    Posted 3 years ago #

    On the Author/User page, I need to add a set of checkboxes that only the Admin role sees (and can set).

    I think it's best to make a plugin for that: http://codex.wordpress.org/Writing_a_Plugin

    Make your plugin show all users and when you select a user let it produce these checkboxes on their individual (plugin) user page.

  11. keesiemeijer
    moderator
    Posted 3 years ago #

  12. fredhead
    Member
    Posted 3 years ago #

    Thanks, keesie, for helping me explore the different options!

  13. fredhead
    Member
    Posted 3 years ago #

    Okay, I found this which appears to be the approach I want:

    http://wordpress.stackexchange.com/questions/6690/how-to-add-save-custom-field-in-user-settings-profile-checkbox-list

    With a tweak to pull from my post categories, it allows me to assign a user profile to one or more categories.

    The problem I'm having is adapting the query to filter content based on the categories assigned to the user. How would I adapt Hameedullah's response here:

    http://wordpress.stackexchange.com/questions/20368/filter-all-queries-with-a-specific-taxonomy

    To use this bit of code from above:

    'query_posts( array( 'tag_id' => get_the_author_meta( 'user_interests', $user_id ) ) );'

    The code feels like it should work in Hameedullah's code but I can't figure out how to tweak the query that generates content.

    Any ideas?

  14. fredhead
    Member
    Posted 3 years ago #

    I've figured out how to extract a cat= string of numeric category values. Now I need help figuring out how to add the cat= values to the query that generates the content data set, at the least, all queries (e.g. category lists) at best.

  15. keesiemeijer
    moderator
    Posted 3 years ago #

    Are these category values comma seperated?

  16. fredhead
    Member
    Posted 3 years ago #

    Found the solution:

    http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

    Thanks, again, for all your help! Later this month, I plan to post my results somewhere in detail, code and explanations, and link to it from here in case that's useful for people over time.

Topic Closed

This topic has been closed to new replies.

About this Topic