Skip Navigation

[Resolved] Including a custom shortcode in a conditional

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

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

Supporter timezone: America/Jamaica (GMT-05:00)

Tagged: 

This topic contains 14 replies, has 2 voices.

Last updated by Shane 4 years, 8 months ago.

Assisted by: Shane.

Author
Posts
#1292989

I have registered a shortcode on my site (check_usersrole) to return '1' if a string is found inside the user role array.

In a view, I want to display an icon if the value '1' is returned. I have confirmed that the shortcode is opertaing correctly by outputting the retunred value but when I try to use a conditional to display E when 1 isreturned, E does not appear.

I have registered the shortcode in the Toolset settings for third party shortcodes.

Is there something wrong with my syntax or is this not allowed?

[wpv-conditional if="('[check_usersrole user_id='[wpv-user field='ID']' checkfor='external']' eq '1' )"]

#1293079

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Nick,

Thank you for getting in touch.

I suspect the issue here is because of the nesting of the shortcodes and the missmatch of the quotations.

Is this inside a view ? or is the shortcode on a content template.

Please let me know.
Thanks,
Shane

#1293109

This is inside a content template which is referred to by a view. Here is some of the content template

<td>
[wpv-conditional if="('[types usermeta='activity' option='9'][/types]' eq 'Test Account' )"]
<span class="fa-stack fa-1x"><i class="fa fa-square fa-stack-2x" style="color:black"><strong class="fa-stack-1x" title="Test Account" style="color:white">T</span>
[/wpv-conditional]
[check_usersrole user_id='[wpv-user field='ID']' checkfor='external']

[wpv-conditional if="('[check_usersrole user_id='[wpv-user field='ID']' checkfor='external']' eq '1' )"]
<span class="fa-stack fa-1x"><i class="fa fa-square fa-stack-2x" style="color:yellow"><strong class="fa-stack-1x" title="External" style="color:white">E</span>
[/wpv-conditional]

[wpv-conditional if="('[types usermeta='activity' option='9'][/types]' ne 'Test Account' )"]
<span class="fa-stack fa-1x"><i class="fa fa-square fa-stack-2x" style="color:#e2e2e2"></span>
[/wpv-conditional]
</td>

#1293129

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Nick,

Could you send me the code of the shortcode to see if i can get this working for you ?

Thanks,
Shane

#1293641

Thanks a lot Shane; I really appreciate it.

I have confirmed that the shortcode is returning '0' or '1' correctly. Here is the shortcode:

/* Check for a role within the user role array and return 1 if found */

function check_usersrole( $atts ) {

// Attributes
$atts = shortcode_atts(
array(
'user_id' => '',
'checkfor' => ''
),
$atts
);

$user_info = get_userdata($atts['user_id']);

$tocheck = implode(', ', $user_info->roles);

$pos = strpos($tocheck, $atts['checkfor']);

if ($pos === false) {
$return=0;
}
else {
$return='1';
}
return $return;
}
add_shortcode( 'check_usersrole', 'check_usersrole' );

#1293955

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Nick,

So I think you should make use of the get_user_by() function to do this since the [wpv-user] will return the user's display name by default.

Could you let me know what you are using as the users display name as we could use this to retrieve the information.

Thanks,
Shane

#1294175

Thanks Shame.

I know the use of [wpv-user field='ID'] is working because if I include [check_usersrole user_id='[wpv-user field='ID']' checkfor='external'] in the content template, it will, when called by the view, produce 1s and 0s depending on whether each user's role includes "external". It just doesn't seem like the conditional is working even thought it includes the same code which it evaluates against.

#1294195

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Nick,

The problem is because of the many quotations that is causing the conditional shortcode to break.

Have a look here


[wpv-conditional if="('[check_usersrole user_id='[wpv-user field='ID']' checkfor='external']' eq '1' )"]

Notice the many use of the single quotes in particular for the user_id. If you manually provide a user ID you will notice that the conditional still works.

Please try this as a test and let me know what happens. We may need to restructure this entire code.

Thanks,
Shane

#1295601

Actually, inserting a user ID I know has the role "external" into the conditional doesn't work either. So

[wpv-conditional if="('[check_usersrole user_id='892' checkfor='external']' eq '1' )"]

is evaluated as false even though userid 892 has the role external so maybe the issue is the inclusion of my custom shortcode

#1296351

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Nick,

Would you mind providing me with admin access to the site as well as the location to where you are testing this ?

Thanks,
Shane

#1298815

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Nick,

I tested your shortcode here
[check_usersrole user_id='[wpv-user field='ID']' checkfor='external']

In every case in the table 0 is returned. I'm not getting a result where 1 is actually being returned from this shortcode.

Thanks,
Shane

#1298825

Shane

I'm really sorry, I forgot that we made a subsequent change from the role of external to the role contributor so there are no users with the role external. I have changed the shortcode test and the conditional to reflect this now.

#1301665

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Nick,

I've tried working on a solution for you, however you need to set all the users display name to their emails. This way we can use the display names to get there emails.

Take a look here hidden link As you can see i've created a custom code that only takes the [wpv-user] shortcode as input and I can return the ID just as a test inside the function.

So if you set all the display names to their emails then we can modify your function to only take the [wpv-user] shortcode as a parameter and then get the user's ID from that.

Please let me know what you think of this workaround.

Thanks,
Shane

#1307559

Thanks Shane.

I really don't want to use this as a workaround as I don't want to set display names to email addresses. Is there a reason why the enclosure of a Toolset shortcode in a custom one doesn't work?

#1308023

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Nick,

The problem is the nesting of the quotations and strings.

For e.g


[wpv-conditional if="('[check_usersrole user_id='[wpv-user field='ID']' checkfor='external']' eq '1' )"]

As you can see the use of the multiple ' ' is closing the previous ones and opening again so the ID attribute for the field isn't being passed properly.

I hope this clears this up for you. This would happen with any shortcode when you're doing a 3 layer shortcode.

Thanks,
Shane

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.