• Resolved heldegar

    (@heldegar)


    Hello on my wordpress i got an url named /rex/ , is it possible to be displayed only for a specific role or block other role to see it ?

    The problem is that this page is not build on wordpress it’s a endpoint (it has been build by another dev than me and i’m not ok with endpont)

    function latty_add_rex_endpoint() {
        add_rewrite_endpoint( 'rex', EP_ROOT | EP_PAGES );
    }

    so if anybody got a solution i would be great ! thank you

Viewing 8 replies - 1 through 8 (of 8 total)
  • The neat thing about add_rewrite_endpoint() is that it also automatically adds the query var for you. That means in most hooks you can simply pass your endpoint to get_query_var() and run your logic if it’s found. In this case you could use the template_redirect hook and redirect non-permissible roles.

    /**
     * Redirects
     *
     * @return void
     */
    function wpf12477272_template_redirects() {
    	
    	// Check if we've hit our endpoint
    	if( false !== get_query_var( 'rex' ) ) {
    		
    		// Check permissions
    		if( ! current_user_can( 'edit_pages' ) ) {
    			
    			wp_safe_redirect( home_url() );
    			exit();
    			
    		}
    		
    	}
    	
    }
    add_action( 'template_redirect', 'wpf12477272_template_redirects' );
    Thread Starter heldegar

    (@heldegar)

    Thank you a lot for your answer.
    it looks like another role can’t access to the whole website, it loads infinitely
    My role is a custom one created with a plugin. It appear that :
    current_user_can( 'custom_role' )
    is not working too

    Do you got any clue ?
    Thank you again

    • This reply was modified 5 years, 1 month ago by heldegar.

    You can grab the current user and check against their roles:

    $user = wp_get_current_user();
    
    if( in_array( 'custom_role_slug', (array)$user->roles ) ) {
    
    }

    If you’re not sure what the slug is, log in as the user with the role and simply print out the users roles:

    printf( '<pre>%s</pre>', print_r( $user->roles, 1 ) );

    Thread Starter heldegar

    (@heldegar)

    Hum i tried and it enters in the “if” correctly but i get a white page looks like the redirection is not working. even if i go to the homepage, i got white page too.

    Thank you

    It’s difficult to say what the issue is based on the given information. You’ll need to enable debugging and report back with errors.

    Debugging in WordPress

    I suggest enabling all 3 debug flags if you’re not on a production site:

    define( 'WP_DEBUG', true );
    define( 'WP_DEBUG_LOG', true );
    define( 'WP_DEBUG_DISPLAY', true );
    Thread Starter heldegar

    (@heldegar)

    Hello,
    i did but i get a mozila page error named “page not correctly redirect”.
    When i try to connect to my website with a basic account.

    Thread Starter heldegar

    (@heldegar)

    Warning: Cannot modify header information – headers already sent by

    Thread Starter heldegar

    (@heldegar)

    Sorry about the spam but, it works with this code :

    if( false !== get_query_var( 'rex', false ) )

    Thank you a lot for your help !

    • This reply was modified 5 years ago by heldegar.
Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Block url to certain role’ is closed to new replies.