Skip Navigation

[Resuelto] Access Control of Users content

Este hilo está resuelto. Aquí tiene una descripción del problema y la solución.

Problem:

If current page is not written by current logged in user then redirect to home page.

Solution:

There isn't such kind of built-in feature within Toolset Plugins, you can consider custom codes, for example:

https://toolset.com/forums/topic/access-control-of-users-content/#post-1376053

Relevant Documentation:

https://codex.wordpress.org/Function_Reference/get_post_field

This support ticket is created hace 5 años. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Hoy no hay técnicos de soporte disponibles en el foro Juego de herramientas. Siéntase libre de enviar sus tiques y les daremos trámite tan pronto como estemos disponibles en línea. Gracias por su comprensión.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

Etiquetado: ,

Este tema contiene 11 respuestas, tiene 2 mensajes.

Última actualización por geoffD hace 5 años.

Asistido por: Luo Yang.

Autor
Mensajes
#1371777

Hello

This may seem a fairly basic question but I just can't find an answer...

How can I use only let a logged in user access their own CPT posts?.... So for example I have 100 different users who all have a CPT 'About Myself' but I only want the logged in user to access their own post and not the other 99..

It feels like it should be a simple thing!
Regards
Geoff

#1371839

Dear Geoff,

I assume each 'About Myself' post's author is different WordPress users.

I suggest you try these:
1) Create a WordPress page, for example "Myself", in this page display below post view:
2) Create a post view:
- Query posts of CPT 'About Myself'
- Filter by post author is current logged in user
- Display the post information

So the logged in user can only his own 'About Myself' post in that "Myself" page
See our document:
https://toolset.com/documentation/user-guides/filtering-views-query-by-author/

#1372271

Hi Luo

Thank you for looking into this for me..

Unfortunately the way my site is set up I have created a template for my CPT 'User Will' and each user is only allowed to create one of these posts... The url is like this:

enlace oculto
enlace oculto
enlace oculto

etc...

So for example user-1 is able to access user-2 and user-3's page...

Best regards

Geoff

#1372767

In your case, the user "user-1" is post author of post: enlace oculto

So you can use [wpv-conditional] shortcode to check if current logged in user is current post's author, then display what you want.

For example:

[wpv-conditional if="( '[wpv-current-user info="login"]' eq '[wpv-post-author format="meta" meta="user_login"]' ) "]
current user is same as the post author of current post.
[/wpv-conditional]

More help:
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-post-author
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-user
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-conditional

#1373271

Hi Luo

Thanks for getting back to me... Indeed the conditional method would be a good way of controlling what content a user can see on the page but the page would still get generated i.e. enlace oculto would still be accessible to user-1 even though the page would be blank..

The problem with this is that the url rather than carry user-1, user-2 etc actually carry the user's first and surnames i.e.. John-smith etc.. So I need a way that the user isn't allowed to access the page rather than the content..

so a function similar to this:

'If current page is not written by current logged in user then redirect to home page'

Thanks in advance

Geoff

#1373553

Dear Geoff,

There isn't such kind of built-in feature within Toolset Plugins, you can consider custom codes, for example:
1) When "user-1" open URL:
enlace oculto

Us WordPress action hook "init" to trigger a custom PHP function:
https://developer.wordpress.org/reference/hooks/init/

2) In this custom PHP function, check if current user is the post's author:
https://developer.wordpress.org/reference/functions/get_the_author_meta/
https://developer.wordpress.org/reference/functions/get_current_user_id/

3) If it isn't the post's author, redirect him to the home page:
https://developer.wordpress.org/reference/functions/wp_redirect/

For your reference.

#1373913

Hi Luo

Thanks for your continued support..

I've written this:

function redirect_user_if_not_author() {
if ($current_user->ID == ! $post->post_author) {
	 wp_redirect( home_url() ); 
}}
add_shortcode( 'not_author', 'redirect_user_if_not_author' );

and inserted the shortcode on the template page but it is just redirecting all users including the author of the custom post

What am I doing wrong?...best regards
Geoff

#1374395

Please try to modify your PHP codes as below, and test again:

add_action( 'template_redirect', function(){
	if( 
		is_singular('user-will') //replace user-will with your custom post type slug
		&& get_the_author_meta( 'ID' ) != get_current_user_id() 
	){
		wp_redirect(home_url( '/' )); // redirect to WordPress home URL
	}
} );
#1374551

Hi Luo

Not quite there yet...the code you sent over still re-directs all users to the home page even if the logged in user is the same as the author...so for example

enlace oculto (Myself as the Author)
enlace oculto (A different Author)

both get redirected..

Many thanks

Geoff

#1375225

I have tried those custom codes in my localhost, it works fine, since it is a custom codes problem, if you need more assistance for it, please provide a test site with the same problem, fill below private message box with website credentials and FTP access, also point out where I can edit your custom PHP codes, I need to test and debug it in a live website. Thanks

#1376053

Thanks for the details, please try to modify the PHP codes as below:

add_action( 'template_redirect', function(){
    if( 
        is_singular('user-will') //replace user-will with your custom post type slug
        && get_post_field( 'post_author' ) != get_current_user_id() 
    ){
        wp_redirect(home_url( '/' )); // redirect to WordPress home URL
    }
} );

And test again.

More help:
https://codex.wordpress.org/Function_Reference/get_post_field

#1376131

My issue is resolved now. Thank you!