Hi!
I have frontend form where unregistered and registered users can submit a custom post.
So how to show some content on published post, only if post author is registered user...?
For example I want to show who is the user who submitted post if he is registered, his "nickname" with link on his profile.
So content for conditional output should be "Submitted by Registered user: [wpv-post-author format="link"]"
If a user in unregistered this line shouldn't be visible and there will be some other content from fields in form for Guests.
Thanks in a advance,
Sinisa
Hello Sinisa and thank you for contacting Toolset support.
When a post is created by a guest user from a Toolset form, it will not have any author. Actually, the author ID will be 0. So you can check against it in a conditional block(while using the blocks editor). Check this screenshot hidden link
If you are using the legacy editor you can use something like:
[wpv-conditional if="( '[wpv-post-author format="meta" meta="ID"]' ne '' )"]
Submitted by Registered user: [wpv-post-author format="link"]
[/wpv-conditional]
Please keep in mind, that once the post is edited on the backend it will get affected an author. It will be the first user in the list of authors. So, if you intend to edit the posts on the backend, you may need a different solution. For example, use custom code, hooked to the cred_save_data hook, and set a custom field(checkbox) to 1 if the author is a registered user, and 0 if the user is a non-registered guest. Then use that custom field in the conditions. Does it make sense?
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
Hi Jamal,
I didn't know that a guest author has ID=0.
I try also another way and works, unregistered users see two additional custom fields in the frontend post form (name, email) and "IF those fields are not filled THEN the submitter is a Registered user (those fields are not visible for registered users)".
Yes, that is it... Conditional output works but posts go on 'pending' status and publishing now is a problem.
But!
I've noticed that all edited customs posts or 'pending' posts after are marked as published by the Administrator, goes under the Admin account. I don't know how to make those published posts as Guests' posts...
As I mention I have in a frontend post form, two custom fields (publisher name and email) are visible for input only to unregistered users.
Those fields need to be required, but the post (after the frontend form is submitted) goes on the pending status and when is edited system requires those fields what is a problem which stops publishing. But if I change the post status from the "quick edit" panel in the backend there is no problem... It is some kind of improvised solution for my case, but it works... Maybe it is OK that guests posts can go under the admin account.
So as you told me, I need some custom PHP code and API hooks, but I don't know-how, no knowledge for that? Do you have some presented steps of a similar problem, I can try to find some logic for my situation?
Thanks,
Sinisa
Hello Sinisa,
As I have explained in my previous email, once you edit the posts from the backend, they will get the first account on the author list as an author. That's how WordPress works, and we can't do anything about it. Check this screenshot about the author's dropdown. hidden link
Because the name and email fields are required, you can(as a workaround) enter a specific name and email(Jhon, doe@example.com). Then, in your content template, you can set a condition on these specific values, when they match, then it is a guest user's post. Does it make sense?
Regarding the custom code, you can find examples of the cred_save_data hook on our forum with this scoped Google search hidden link
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
You can use the wp_get_current_user to determine if the current user is logged in(registered) or not. For guest users the function will return 0. Then you can use the update_post_meta to set a value for the post depending on the current user nature(logged-in/guest). Then you can create a condition on that custom field.
https://developer.wordpress.org/reference/functions/wp_get_current_user/
https://developer.wordpress.org/reference/functions/update_post_meta/
I hope this helps. Let me know if you have any questions.
My issue is resolved now. Thank you!
P.S.
***Because the name and email fields are required, you can(as a workaround) enter a specific name and email(Jhon, doe@example.com). Then, in your content template, you can set a condition on these specific values, when they match, then it is a guest user's post. Does it make sense?***
Yes, my solution is if those fields are empty or not in cred from, fields are hidden for registered users...
I asked only if there is a more "professional" solution...
I have to explore and learn more about custom codes and api hooks...
Thank you for the links and effort!
My issue is resolved now. Thank you!