• Resolved ryanbowden

    (@ryanbowden)


    Hello,

    I am Currently building a big site for a charity which has multiple departments.

    First of I have used “Post Tags and Categories for Pages” plugin to make sure pages have got a category.

    and for each of them Category i have a user role that will be part of it.

    Now what i need to do is if someone with the user role training logs in, I need them to only see posts / pages with the category training on them, to make sure they do not edit another department pages / posts.

    Now the way I have done it for now is to attach a style sheet for each user role, and display none the posts row that do not have the category that user role is set to.

    Ofcouse this messes up the pagination and in some cases cause the admin page to show no post on page one and two but posts on page three. Also all it takes is someone to push f12 and they have bypassed the display none.

    If anyone know anyway to hock into them admin pages and change the page depending on the user role.

    Many Thanks.
    Ryan Bowden

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hello Ryan,

    There are several plugins available to restrict the WP users from editing the already published posts. Below are some better plugins:

    https://www.ads-software.com/plugins/role-scoper/
    https://www.ads-software.com/plugins/user-role-editor/

    Thread Starter ryanbowden

    (@ryanbowden)

    No don’t really want to use plugins as people may mess with them! and they have more functionality that we need.

    I found the function posts_where and have the following code:

    function my_load_edit_php_action() {
    		if (isset( $_POST['post_type'] ) && $_GET['post_type'] != 'page') return;
    			add_filter('posts_where', 'my_posts_where_filter');
    		}
    		add_action( 'admin_init', 'my_load_edit_php_action');
    
    		function my_posts_where_filter($sql) {
    			$roles = appthemes_check_user_role();
    			if (in_array('training', $roles)){
    
    		    	global $wpdb;
    				$sql = " AND $wpdb->posts.id IN (SELECT term_taxonomy_id FROM wp_term_relationships WHERE object_id =  $wpdb->posts.id IN (SELECT term_id FROM wp_term_taxonomy WHERE term_taxonomy_id = term_taxonomy_id IN (SELECT slug FROM wp_terms WHERE term_id = term_id AND slug = 'training') ))" . $sql;
    			}
    
    			return $sql;
    
    		}

    I am still playing with it but it add to the end of the clause to add my where and i just need to link the posts to the category this way.

    First time working with IN so the code may be completely wrong any idea where i am going wrong?

    Thread Starter ryanbowden

    (@ryanbowden)

    Found the Solution will post it so others in the near future know it.

    //Gtes the Current user role
    	function appthemes_check_user_role($user_id = null ) {
    	    if ( is_numeric( $user_id ) )
    			$user = get_userdata( $user_id );
    	    else
    	        $user = wp_get_current_user();
    
    	    if ( empty( $user ) )
    		return false;
    
    		$roles = '';
    		foreach ($user->roles as $role ) {
    			$roles .= $role;
    		}
    
    	    return array($roles);
    
    	}
    
    function my_load_edit_php_action() {
    		if (isset( $_POST['post_type'] ) && $_GET['post_type'] != 'page') return;
    		add_filter('posts_where', 'my_posts_where_filter');
    	}
    	add_action( 'admin_init', 'my_load_edit_php_action');
    
    	function my_posts_where_filter($sql) {
    		$roles = appthemes_check_user_role();
    		if (in_array('training', $roles)){
    	    	global $wpdb;
    			$sql = " AND ID IN (select object_id from wp_term_relationships where term_taxonomy_id IN (23))" . $sql;
    		}
    		if (in_array('showroom', $roles)){
    	    	global $wpdb;
    			$sql = " AND ID IN (select object_id from wp_term_relationships where term_taxonomy_id IN (19))" . $sql;
    		}
    		if (in_array('accomodation', $roles)){
    	    	global $wpdb;
    			$sql = " AND ID IN (select object_id from wp_term_relationships where term_taxonomy_id IN (20))" . $sql;
    		}
    		if (in_array('familysupport', $roles)){
    	    	global $wpdb;
    			$sql = " AND ID IN (select object_id from wp_term_relationships where term_taxonomy_id IN (21))" . $sql;
    		}
    		if (in_array('healthfitness', $roles)){
    	    	global $wpdb;
    	    	$sql = " AND ID IN (select object_id from wp_term_relationships where term_taxonomy_id IN (22))" . $sql;
    		}
    		return $sql;
    	}
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Hide Posts / Pages depending the user role / Category of the page / post’ is closed to new replies.