Skip Navigation

[Resolved] Problem with own shortcode in Content Template

This support ticket is created 7 years, 4 months ago. 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

Tagged: 

This topic contains 10 replies, has 2 voices.

Last updated by Bochnacki 7 years, 4 months ago.

Assisted by: Nigel.

Author
Posts
#541032

In topic https://toolset.com/forums/topic/author-page-depending-on-the-role-of-the-user/page/2/#post-540155 Minesh wrote me a shortcode [get_user_type]. On a normal WordPress page, it works fine, but it does not work in the Content Template.
I make this code:

[wpv-conditional if="( '[get_user_type]' eq '1' )"]
A
[/wpv-conditional]
[wpv-conditional if="( '[get_user_type]' eq '2' )"]
B
[/wpv-conditional]

What should I do to make this shortcode work also in a Content Template?
I will be grateful for help.

#541317

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi there

There is no particular reason why a custom shortcode in a conditional will work on a standard page but not when added to a content template. If you modify your shortcode so that it simply returns some specific text and then test for that text in your condition you will see it should work.

I'm not sure from your other ticket what you ended up with in terms of your URL structure and parameters, could you confirm that?

The reason this is not working is probably because the code in the custom shortcode fails when used in a different context, if you could please describe that.

If you add debug="true" to your wpv-conditional shortcode you will see what the shortcode evaluates to.

#541457

Unfortunately, not everything from what you wrote I understood.

I have two roles: "customer" and "dealer".
I made a "user-type" user field and added them to the registration forms. It writes for "customer" the value "1" and for "dealer" value "2".
For this is the code:

add_action('cred_save_data_37', 'my_save_data_user_for_customer',10,2);
function my_save_data_user_for_customer($post_id, $form_data){
    if ($form_data['id']==37){
            update_user_meta($post_id, 'wpcf-user-type', $_POST['user-type']);
    }
}
add_action('cred_save_data_112', 'my_save_data_user_for_dealer',10,2);
function my_save_data_user_for_dealer($post_id, $form_data){
    if ($form_data['id']==112){
            update_user_meta($post_id, 'wpcf-user-type', $_POST['user-type']);
    }
}

For this is created shortcode [get_user_type]:

function func_get_user_type( $atts ){
	if(isset($_GET['author_id'])){
		$author_id = $_GET['author_id'];
		$x = get_user_meta($author_id,'wpcf-user-type',true);
		return $x;}
}
add_shortcode('get_user_type', 'func_get_user_type' );

I used this shortcode on one of the subpages:

[wpv-conditional if="( '[get_user_type]' eq '1' )"]
[wpv-view name="profil-customer"]
[/wpv-conditional]
[wpv-conditional if="( '[get_user_type]' eq '2' )"]
[wpv-view name="profil-dealera"]
[/wpv-conditional]

And it works well.

Now I used [get_user_type] in Toolset "Content Template" and it does not work.

[wpv-conditional if="( '[get_user_type]' eq '1' )" ]
<div class="">[types usermeta='phone-customer'][/types]</div>
    [/wpv-conditional]
    [wpv-conditional if="( '[get_user_type]' eq '2' )"]
<div class="">[types usermeta='phone-dealer'][/types]</div>
    [/wpv-conditional]

The debugger shows me this information:

wpv-conditional attributes
####################
Array
(
    [if] => ( '' = '1' )
    [debug] => true
)
####################
Debug information
####################
--------------------
Original expression: ( '' = '1' )
--------------------
After replacing 1 general variables and comparing strings: ( '' = 1 )
	Comparing  to 1

(This for [wpv-conditional if="( '[get_user_type]' eq '1' )" ])
But for me it does not mean anything, I do not understand it. I guess that [get_user_type] is invisible, unread, yes?

If I have described something unclean, or missing any explanation, please write what.

"I'm not sure from your other ticket what you ended up with in terms of your URL structure and parameters, could you confirm that?" - I do not understand this question 🙁

#541473

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Thanks for the clarification.

You can only use the custom shortcode you wrote on a page which has the url parameter "author_id" (e.g. mysite.com/?author_id=7).

The code for your custom shortcode takes the author_id and gets the user_meta for wpcf-user-type for the user with that id.

It is not working in your content template because the page where this is being used doesn't have the author_id url parameter.

What is the content template being used for? How should it know whether it should display content for a dealer or for a customer?

#541490

"You can only use the custom shortcode you wrote on a page which has the url parameter" author_id "(e.g. mysite.com/?author_id=7)." - I understand.

"What is the content template being used for?" - Are you asking for a template ID? ID = 21. It is used on simple pages - "osobowe".
"How should it know whether it should display content for a dealer or for a customer?" - I do not know. I thought such a shortcode would be good.

I have yet another shortcode:

function get_author_role()
{
    global $authordata;
    $author_roles = $authordata->roles;
    $author_role = array_shift($author_roles);
    return $author_role;
}
/* register shortcode handler function */
add_shortcode( 'user_role', 'get_author_role' );

Unfortunately, it only shows the user role, but the wpv-conditional does not work.

[wpv-conditional if="( '[user_role]' eq 'customer' )"]

It does not work.

#541962

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

I'd like to recommend a solution for you and help you implement it, but at the moment I don't understand what it is you are aiming to do.

I understand you have users, some of whom are dealers, and some of whom are customers.

Can you just describe in words rather than code what it is your site does and what the significance of the user role is when it comes to displaying what content where?

#541985

I would like to be able to show items (link, name, phone number, etc.) depending on the user role, but whether logged in or not.

#542011

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

You have a custom post type with some custom fields (link, name, phone number) and you want to change whether these fields can be seen according to whether the user is

- logged in as a customer
- logged in as a dealer
- not logged in

Is that what you are aiming to do?

#542039

Option 1.
I visit the site as a guest - not logged in.
I'm browsing the list of posts. Each post has the same format - "name" and "text".
- Some posts posted by users on the role of "customer" - "name" in these posts is in red.
- Some posts posted by users on the role of "dealer" - "name" in these posts is in blue.

Option 2.
I visit the site as a logged in administrator, or "customer" or "dealer".
I'm browsing the list of posts. Each post has the same format - "name" and "text".
- Some posts posted by users on the role of "customer" - "name" in these posts is in red.
- Some posts posted by users on the role of "dealer" - "name" in these posts is in blue.

I hope this time I was able to explain what I mean 🙂

#542533

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Screen Shot 2017-06-28 at 15.20.01.png

From what I can see there is no difference between option 1 and option 2, you display the same thing whether the user is logged in or not.

I registered the following custom shortcode which will return the role of the author of the current post in the loop:

/**
 * Custom shortcode to return role of author of current post
 * Assumes only has single role, will return first
 */
add_shortcode( 'author-role', function(){

	global $post;
	$author_id = $post->post_author;

	$author_meta = get_userdata( $author_id );

	$author_roles = $author_meta->roles;

	return $author_roles[0];
});

Then you can see how I used it in the screenshot.

There are a number of alternatives for what you might do with it, but I used it to construct unique classnames and added my colour styles using those classes.

Posts for whom the author was a dealer would be output in blue, and those for whom the author was a customer would be output in red.

#542565

You are great!

[wpv-conditional if="( '[author-role]' eq 'customer' )"]....
[wpv-conditional if="( '[author-role]' eq 'dealer' )"]....

This is what I meant ????
Your shortcode is very useful.

If possible it would be good to change the title of this topic to "[author-role] shortcode", which would be more appropriate. I think this shortcode can be useful for many people.