• Anonymous User 357386

    (@anonymized-357386)


    /* Limit access to only own content for subscriber */
    function limit_subscriber_backend( $query )
    {
    	// if is a subscriber in admin panel
    	if ( is_admin() && current_user_can('subscriber') && $query->is_main_query() )
    	{
    		$user = wp_get_current_user();
    		$user_id = $user->ID;
    		$query->set('author',  $user_id);
    	}
    }
    add_filter('pre_get_posts', 'limit_subscriber_backend');

    this limit only subscriber and permit editor, contributor and admin (all except subscriber) to view all – like from default.

    but can be simply changed to something like this:

    /* Limit access to only own content for all except administrators */
    function limit_subscriber_backend( $query )
    {
    	// if is not an admin and is in admin panel
    	if ( is_admin() && ! current_user_can('administrator') && $query->is_main_query() )
    	{
    		$user = wp_get_current_user();
    		$user_id = $user->ID;
    		$query->set('author',  $user_id);
    	}
    }
    add_filter('pre_get_posts', 'limit_subscriber_backend');

    so simple and… work ??

Viewing 8 replies - 1 through 8 (of 8 total)
  • Could you tell us where (file) to put this ?

    Thanks

    Thread Starter Anonymous User 357386

    (@anonymized-357386)

    function.php or a little custom plugin:

    eg, i’ve this for personal function.php (no lost during upgrade):

    with underscrore it go first of all in plugin list.

    then, u can simply click on “edit” from plugin list, to add new function (is best, more simple and more secure than go in appearance, theme, etc…)!

    <?php
    /**
     * Plugin Name: _Some fix in custom function/plugin
     * Description: _Some fix in custom function/plugin
     * Version: 1.0
     * Author: Salvatore Noschese
     */
    
    defined( 'ABSPATH' ) or exit;
    
    /* Limit access to only own content for subscriber */
    function limit_subscriber_backend( $query )
    {
    	// if is a subscriber in admin panel
    	if ( is_admin() && current_user_can('subscriber') && $query->is_main_query() )
    	{
    		$user = wp_get_current_user();
    		$user_id = $user->ID;
    		$query->set('author',  $user_id);
    	}
    }
    add_filter('pre_get_posts', 'limit_subscriber_backend');
    
    /* other function here... */
    
    Thread Starter Anonymous User 357386

    (@anonymized-357386)

    I’ve test this, and after of this I’ve make something of similar (but with static php file) by myself (like info in precedent reply).

    Thread Starter Anonymous User 357386

    (@anonymized-357386)

    Just to be clear: Seems to be a good plugin, and I think u can use without problem!
    But this work via php etc… Some user seems to have little problem with some code (view support for this plugin). Then, I’ve prefer to make a php clone of function.php (simple to edit and manage via plugin list).
    That’s is all.

    If u prefer to install 3d part plugin also when isn’t necessary… Ok! ??

    Great little script, I limited all but admins to view others posts, works great.

    However, on a custom post type, I have set several taxononies and all users need to be able to tick the correct options per post, however with this script running the taxonomy checkboxes are greyed out.

    Thread Starter Anonymous User 357386

    (@anonymized-357386)

    can u try with:

    <?php
    /**
     * Plugin Name: _Some fix in custom function/plugin
     * Description: _Some fix in custom function/plugin
     * Version: 1.0
     * Author: Salvatore Noschese
     */
    
    defined( 'ABSPATH' ) or exit;
    
    /* Limit access to only own content for subscriber */
    function limit_subscriber_backend( $query )
    {
    	// if is a subscriber in admin panel
    	if ( is_admin() && current_user_can('subscriber') )
    	{
    		$user = wp_get_current_user();
    		$user_id = $user->ID;
    		$query->set('author',  $user_id);
    	}
    }
    add_filter('pre_get_posts', 'limit_subscriber_backend');
    
    /* other function here... */

    (removed: && $query->is_main_query() )
    this has caused some little issue also to me ??

    • This reply was modified 8 years, 3 months ago by Anonymous User 357386.

    I think my issue may be partly due to how the custom post type (CPT) has been set up. I have created a new user role ‘authority’, when they log in, all they should see is the Job CPT and also just view their own jobs/posts.

    It seems to be working great apart from the fact they cannot tick any taxonomies.

    Here’s my CPT, maybe there is something obvious in there that I am missing

    <?php
    /**
     * Custom post type for jobs and taxonimies
     */
     
    add_action( 'init', 'create_custom_post_types' );
    function create_custom_post_types() {
    	
    
    	register_post_type( 'sc_jobs',
    		array(
    			'labels' => array(
    					'name' => 'Job',
    					'singular_name' => 'Job',
    					'add_new' => 'Add New',
    					'add_new_item' => 'Add New',
    					'edit_item' => 'Edit',
    					'new_item' => 'New',
    					'all_items' => 'All Jobs',
    					'view_item' => 'View',
    					'search_items' => 'Search',
    					'not_found' =>  'No jobs found',
    					'not_found_in_trash' => 'No jobs found in Trash',
    					'parent_item_colon' => '',
    					'menu_name' => 'Jobs'
    			),
    			'public' => true,
    			'has_archive' => true,
    			'rewrite' => array('slug' => 'job'),
    			'capability_type' => 'job',
    			'capabilities' => array(
    				'publish_posts' => 'publish_jobs',
    				'edit_posts' => 'edit_jobs',
    				'edit_others_posts' => 'edit_others_jobs',
    				'delete_posts' => 'delete_jobs',
    				'delete_others_posts' => 'delete_others_jobs',
    				'read_private_posts' => 'read_private_jobs',
    				'edit_post' => 'edit_jobs',
    				'delete_post' => 'delete_jobs',
    				'read_post' => 'read_jobs',
    				'assign_terms'  => 'edit_taxo'
    			),
    			'supports' => array('title', 'editor', 'thumbnail')
    		)
    	);
    	
    	register_taxonomy(
    		'job_categories',
    		array('sc_jobs'),
    		array(
    			'hierarchical' => true,
    			'label' => __( 'Job Categories' ),
    			'rewrite' => array( 'slug' => 'job-category' ),
    			'show_ui' => true,
    			'query_var' => 'job-category',
    		)
    	);
    
    	register_taxonomy(
    		'contract_type',
    		array('sc_jobs'),
    		array(
    			'hierarchical' => true,
    			'label' => __( 'Contract type' ),
    			'rewrite' => array( 'slug' => 'job-contract-type' ),
    			'show_ui' => true,
    			'query_var' => 'job-contract-type',
    		)
    	);
    
    	register_taxonomy(
    		'working_pattern',
    		array('sc_jobs'),
    		array(
    			'hierarchical' => true,
    			'label' => __( 'Working Pattern' ),
    			'rewrite' => array( 'slug' => 'job-working-pattern' ),
    			'show_ui' => true,
    			'query_var' => 'job-working-pattern',
    		)
    	);
    
    	register_taxonomy(
    		'salary_band',
    		array('sc_jobs'),
    		array(
    			'hierarchical' => true,
    			'label' => __( 'Salary Band' ),
    			'rewrite' => array( 'slug' => 'salary-band' ),
    			'show_ui' => true,
    			'query_var' => 'salary-band',
    		)
    	);
    	
    	register_taxonomy(
    		'the_advertiser',
    		array('sc_jobs'),
    		array(
    			'hierarchical' => true,
    			'label' => __( 'Advertiser' ),
    			'rewrite' => array( 'slug' => 'job-advertiser' ),
    			'show_ui' => true,
    			'query_var' => 'job-advertiser',
    		)
    	);
    	
    	
    }
    
    function add_jobs_caps() {
       
        $admins = get_role( 'authority' );
    
        $admins->add_cap( 'edit_jobs' );  
        $admins->add_cap( 'publish_jobs' ); 
        $admins->add_cap( 'read_jobs' ); 
        $admins->add_cap( 'read_private_jobs' ); 
        $admins->add_cap( 'delete_jobs' ); 
    	$admins->add_cap( 'edit_taxo' );
    	$admins->remove_cap( 'edit_others_jobs' );
    	$admins->remove_cap( 'delete_others_jobs' );
    	
    	
    }
    add_action( 'admin_init', 'add_jobs_caps');
    
    function add_jobs_caps1() {
       
        $admins1 = get_role( 'administrator' );
    
        $admins1->add_cap( 'edit_jobs' );  
        $admins1->add_cap( 'publish_jobs' ); 
        $admins1->add_cap( 'read_jobs' ); 
        $admins1->add_cap( 'read_private_jobs' ); 
        $admins1->add_cap( 'delete_jobs' ); 
    	$admins1->add_cap( 'edit_others_jobs' );
    	$admins1->add_cap( 'delete_others_jobs' );
    	
    }
    add_action( 'admin_init', 'add_jobs_caps1');
    
    		
    ?>
Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘why not use just this little code?’ is closed to new replies.