WordPress.org

Ready to get started?Download WordPress

Forums

A way to restrict category choices (per user) on post.php (16 posts)

  1. andre_gr
    Member
    Posted 9 years ago #

    I have seen similar questions asked a few different ways here, but with no recent responses...

    The setup:
    I have a blog with multiple users and multiple categories. Each user should make posts in only one category - i.e., user A only posts in category A, user B only posts in category B. (Multiple users might share a category, but they would still be restricted to only that category - i.e., users B and C are both restricted to only posting in category B.) The Admin user (me) can still post in any category.

    Is it possible to do this:
    When a user logs in to make a post (on the /wp-admin/post.php page), the "Categories" option on the right side of the page only shows the category that she can post in (this category should also be checked). Or, the variable could be passed as a hidden field - it doesn't matter, as long as there's no way that a user can post in any category but their own.

    It seems like I might be able to modify the limit categories plugin to do this, but I can't figure out how to get started... any ideas? Has anyone made something like this work?

  2. Joshua Sigar
    Member
    Posted 9 years ago #

    Limit Categories plugin: Limits the categories to which users of certain levels can make posts.

    The plugin check for user level, for your purpose, modify it to check for username.

  3. andre_gr
    Member
    Posted 9 years ago #

    Well, I have taken a look at the limit_categories plugin (which helped out with quite a few things), and now the plan is to write a plugin that does the following:

    -When admin adds a new user, there is a new section on users.php that has a dropdown menu of all categories, so the appropriate one for this user can be selected. This selection writes the category choice (by category ID or category name) to a new column called "user_allowedcategory" in the wp_users table.

    -Then, there's an action hooked onto admin_head that triggers when the user is on post.php. It gets the database info on the currently logged-in user, checks for a value in the "user_allowedcategory" column, and only outputs the allowed category to the page.

    Is this possible (especially the database additions), and does it sound like a reasonably sound way to do things?
    Has anyone written any tutorials on working with saving data to the database in plugins, or can anyone get me started on that part?

  4. Lorelle
    Member
    Posted 9 years ago #

  5. InFnit
    Member
    Posted 9 years ago #

    andre_gr how's this coming along?

  6. InFnit
    Member
    Posted 9 years ago #

    I had an idea - you add a new field in the $user_data and call it user_catid

    So started editing this:

    <?
    /*
    Plugin Name: Limit Categories
    Plugin URI: http://www.asymptomatic.net/wp-hacks
    Description: Limits the categories to which users of certain levels can make posts.
    Version: 1.1
    Author: Owen Winkler
    Author URI: http://www.asymptomatic.net
    */
    /*
    Limit Categories - Limits the categories to which users of
    certain levels can make posts.

    This code is licensed under the MIT License.
    http://www.opensource.org/licenses/mit-license.php
    Copyright (c) 2005 Owen Winkler

    Permission is hereby granted, free of charge, to any person
    obtaining a copy of this software and associated
    documentation files (the "Software"), to deal in the
    Software without restriction, including without limitation
    the rights to use, copy, modify, merge, publish,
    distribute, sublicense, and/or sell copies of the Software,
    and to permit persons to whom the Software is furnished to
    do so, subject to the following conditions:

    The above copyright notice and this permission notice shall
    be included in all copies or substantial portions of the
    Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
    KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
    WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
    PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
    OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
    OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
    OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
    SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    */

    $limit_minlevel = 2; // The minimum level you need to see ALL categories.
    $limit_onlycats = array('Uncategorized', 'FFAF'); //The categories that you can see when you have a low user level
    $limit_dosubcats = true; // Show subcategories for the limited categories

    add_action('admin_head', array('catlimit', 'admin_head'));

    class catlimit {
    function admin_head($unused) {
    global $userdata, $limit_minlevel;
    get_currentuserinfo();

    if($userdata->user_level < $limit_minlevel) {
    if(preg_match('|/wp-admin/post.php|', $_SERVER['REQUEST_URI'])) {
    ob_start(array('catlimit', 'ob_done'));
    }
    }
    }

    function ob_done($content) {
    return preg_replace('|<fieldset id="categorydiv">.*?</fieldset>|sie', 'catlimit::return_catshell();', $content);
    }

    function return_catshell() {
    $output = '<fieldset id="categorydiv"><legend><a href="http://wordpress.org/docs/reference/post/#category" title="' . __('Help on categories') . '">' . __('Categories') . '</a></legend><div>';

    $categories = get_nested_categories();
    $output .= catlimit::return_catlist($categories);

    $output .= '</div></fieldset>';
    return $output;
    }

    function return_catlist($categories, $use_children = false) {
    global $limit_onlycats, $limit_dosubcats;

    foreach($categories as $category) {
    if($use_children || in_array($category['cat_name'], $limit_onlycats)) {
    $output .= '<label for="category-' . $category['cat_ID'] . '" class="selectit"><input value="' . $category['cat_ID']
    . '" type="checkbox" name="post_category[]" id="category-' . $category['cat_ID'] . '"'
    . ($category['checked'] ? ' checked="checked"' : "") . '/> ' . wp_specialchars($category['cat_name']) . "</label>n";

    if($limit_dosubcats && isset($category['children'])) {
    $output .= "n<span class='cat-nest'>n";
    $output .= catlimit::return_catlist($category['children'], true);
    $output .= "</span>n";
    }
    }
    }
    return $output;
    }
    }
    ?>

    to get this:

    <?
    /*
    Plugin Name: Limit Categories
    Plugin URI: http://www.asymptomatic.net/wp-hacks
    Description: Limits the categories to which users of certain levels can make posts.
    Version: 1.1
    Author: Owen Winkler
    Author URI: http://www.asymptomatic.net
    */
    /*
    Limit Categories - Limits the categories to which users of
    certain levels can make posts.

    This code is licensed under the MIT License.
    http://www.opensource.org/licenses/mit-license.php
    Copyright (c) 2005 Owen Winkler

    Permission is hereby granted, free of charge, to any person
    obtaining a copy of this software and associated
    documentation files (the "Software"), to deal in the
    Software without restriction, including without limitation
    the rights to use, copy, modify, merge, publish,
    distribute, sublicense, and/or sell copies of the Software,
    and to permit persons to whom the Software is furnished to
    do so, subject to the following conditions:

    The above copyright notice and this permission notice shall
    be included in all copies or substantial portions of the
    Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
    KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
    WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
    PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
    OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
    OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
    OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
    SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    */

    $limit_minlevel = 5; // The minimum level you need to see ALL categories.
    $limit_onlycats = echo'$userdata->user_catid'; //The categories that you can see when you have a low user level
    $limit_dosubcats = true; // Show subcategories for the limited categories

    add_action('admin_head', array('catlimit', 'admin_head'));

    class catlimit {
    function admin_head($unused) {
    global $userdata, $limit_minlevel;
    get_currentuserinfo();

    if($userdata->user_catid ='0') {
    if(preg_match('|/wp-admin/post.php|', $_SERVER['REQUEST_URI'])) {
    ob_start(array('catlimit', 'ob_done'));
    }
    }
    }

    function ob_done($content) {
    return preg_replace('|<fieldset id="categorydiv">.*?</fieldset>|sie', 'catlimit::return_catshell();', $content);
    }

    function return_catshell() {
    $output = '<fieldset id="categorydiv"><legend><a href="http://wordpress.org/docs/reference/post/#category" title="' . __('Help on categories') . '">' . __('Categories') . '</a></legend><div>';

    $categories = get_nested_categories();
    $output .= catlimit::return_catlist($categories);

    $output .= '</div></fieldset>';
    return $output;
    }

    function return_catlist($categories, $use_children = false) {
    global $limit_onlycats, $limit_dosubcats;

    foreach($categories as $category) {
    if($use_children || in_array($category['cat_name'], $limit_onlycats)) {
    $output .= '<label for="category-' . $category['cat_ID'] . '" class="selectit"><input value="' . $category['cat_ID']
    . '" type="checkbox" name="post_category[]" id="category-' . $category['cat_ID'] . '"'
    . ($category['checked'] ? ' checked="checked"' : "") . '/> ' . wp_specialchars($category['cat_name']) . "</label>n";

    if($limit_dosubcats && isset($category['children'])) {
    $output .= "n<span class='cat-nest'>n";
    $output .= catlimit::return_catlist($category['children'], true);
    $output .= "</span>n";
    }
    }
    }
    return $output;
    }
    }
    ?>

    But I don't know where to go from there as the rest is gibberish!

    I hope you can understand from what I have changed what I want to accomplish and then edit the code for me :D

    The only problem then is getting a way to add the new field into the profile so only admin can edit it :S

    BTW These are the edit's I made:

    $limit_minlevel = 5; // The minimum level you need to see ALL categories.
    $limit_onlycats = echo'$userdata->user_catid'; //The categories that you can see when you have a low user level
    $limit_dosubcats = true; // Show subcategories for the limited categories

    add_action('admin_head', array('catlimit', 'admin_head'));

    class catlimit {
    function admin_head($unused) {
    global $userdata, $limit_minlevel;
    get_currentuserinfo();

    if($userdata->user_catid ='0') {

    Can be found in there. Where I said if($userdata->user_catid ='0') {
    I basically wanted to say that

    if {$userdata->user_catid="0"} //Admin will be 0

    then {$post_ability="all"} //They can post everywhere

    else {$userdata->user_catid=$post_ability}; ?> //Else they post in the category corresponding to their catid

    Hope you can help and that this made sense.......
    (Im a a php newbie at the moment :( )

  7. Randyd
    Member
    Posted 8 years ago #

    Hey,

    This is what I need too.

    Has this been resolved?

    Please let me know.

    Thanks

  8. InFnit
    Member
    Posted 8 years ago #

    SOrry no, I'm not a PHP genius so I can't edit that code as I don't know what it means :( I was hoping someone else who's very familiar with it could change to the code

  9. onenationunderdog
    Member
    Posted 8 years ago #

    anybody got this working?

    it would be so so helpful to restrict cat posting access by user for those of us that run group blogs but do want to run mu or multiply

  10. onenationunderdog
    Member
    Posted 8 years ago #

    haven't tried this yet but looks very promising:
    http://dev.wp-plugins.org/wiki/Userextra

  11. onenationunderdog
    Member
    Posted 8 years ago #

    er, maybe not. useful for other purposes though.
    http://log.squish.net/2005/09/20/usermeta-userextra/

  12. squish
    Member
    Posted 8 years ago #

    I've tried to add some functionality to Userextra to do this. See version 0.2 at http://dev.wp-plugins.org/wiki/Userextra to download.

    The three things I've added are:
    - There's now an additional option:
    "List of category names users can't post to by default"
    (as before, the per-user category allow/deny rules apply)
    - The Write Post window now only has the categories you can post to listed.
    - Attempts to post with a restricted category will throw up an error (just incase someone crafts a request by hand)

    Let me know if this is any good for you.

  13. wubbonius
    Member
    Posted 8 years ago #

    Hi Squish,

    i can't get the whole plugin to work! No restrictions at all. I can view all categories, no matter how I am logged in - or not...
    Let alone do the post-to-specific-category thing...
    If you know what's on here - please help! I am going to love this plugin like crazy - if it works! ;-)

  14. coppit
    Member
    Posted 8 years ago #

  15. onenationunderdog
    Member
    Posted 8 years ago #

    no from the looks your plugin is for restricting the *reading* of posts by user level

    and the link above returns a 404; the plugin is at the bottom of this page:
    http://www.coppit.org/code/index.php

  16. mAg
    Member
    Posted 8 years ago #

    Nice work squish! Took a while finding it, but the combination of the usermeta and userextra plugins is awesome! It is *so close* to what I've been looking for.

    I am encountering a couple undesireable behaviours however:

    * I have one author set up to post to one category, but the one containing the apostrophe still appears when logged in (and it's checked by default). What if the category slug, or category id was used instead of the name?

    * Also, is there a way that authors can still be able to view all categories while just browsing the blog? I am trying to just designate which categories various authors can post to...not limit what they see when browsing. Currently, if they are logged in, they can only browse content in the categories they can post to. To browse the whole site, they need to logout.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.