[toolset_access role="Administrator,Organization" operator="allow"]
[wpv-post-body view_template="member-single-info-template"]
[/toolset_access]
[toolset_access role="Contractor" operator="allow"]
[wpv-conditional if="( '[wpv-post-id item="@contractor-member.parent"]' ne '' )"]
[wpv-conditional if="( '[parent_check_user_access]' eq '1' )"]
[wpv-post-body view_template="member-single-info-template"]
[/wpv-conditional]
[/wpv-conditional]
[/toolset_access]
In certain instances (no parent) my shortcode displays an error. The shortcode is on the 7th line and part of a conditional statement.
The shortcode should only be processed if the current user is logged in as the Contractor user level and the current post has a parent.
When logged in as an Administrator or Organization, the shortcode is displaying an error.
Hello,
In the codes you mentioned above, line 7, there is a custom shortcode: [parent_check_user_access].
How do you create it? With custom PHP codes?
And please follow our document to add it here:
Toolset -> Settings page and click the Front-end Content tab. There, simply add your shortcode to the Third-party shortcode arguments section.
See our document:
https://toolset.com/documentation/user-guides/views/conditional-html-output-in-views/using-shortcodes-in-conditions/#checking-custom-shortcodes
If I'm logged in as an administrator, the contents of the shortcode shouldn't matter because Toolset access should be restricting me from it. Also, the post where the that's giving me an error doesn't have a parent post, so the conditional statement in line 6 should also restrict it.
I admittedly (and obviously) am not an expert with this so......
The code of the shortcode is something I found in the support forum here and altered.
I have two shortcodes, one that checks the current user's email address against a repeating field in the current post (works great) and one that checks the current user's email address against a repeating field in the parent of the current post.
I already had the shortcodes registered and added a screenshot to this post.
The custom code tab in the Toolset settings is new! I moved my stuff from the functions.php and made two snippets.
Here's the contents of the snippet that checks the current user's email address against a repeating field in current post's parent.
<?php
/**
* Checks the current user's email address against a list of email addresses in a repeating field from the parent post
*/
toolset_snippet_security_check() or die( 'Direct access is not allowed' );
// Put the code of your snippet below this comment.
/** Check repeating field in parent post **/
add_shortcode('parent_check_user_access', 'func_parent_check_user_access');
function func_parent_check_user_access($atts, $content = '') {
global $post;
// get post id of the member page
$member_id = $post->ID;
// use the member page id to get the id of the parent (contractor) page
$contractor_parent_id = toolset_get_related_post( $member_id, 'contractor-member', 'parent' );
// use the parent (contractor) page id to get the repeating field contents
$all = get_post_meta($contractor_parent_id,'wpcf-contractor-access-usernames');
// first get the current user, then get their email address
$current_user = wp_get_current_user();
$current_user_access = $current_user->user_email;
// check the current user's email address against the ones in the repeating field
foreach($all as $k=>$v):
if( $v == $current_user_access)
return 1;
endforeach;
return 0;
}
What PHP error do you get?
Since it is a custom PHP codes problem, please check these:
1) In case it is a compatibility problem, please deactivate all other plugins, and switch to wordpress default theme 2020, deactivate all custom PHP/JS code snippets, and test again
2) Also check if there is any PHP/JS error in your website:
https://toolset.com/documentation/programmer-reference/debugging-sites-built-with-toolset/
3) If the problem still persists, please provide database dump file(ZIP file) of your website, also point out the problem page URL(where I can see the error you mentioned above: the shortcode is displaying an error), I need to test and debug it in my localhost, thanks
https://toolset.com/faq/provide-supporters-copy-site/
Thanks for the details, I can install your database dump file in my localhost with a fresh WordPress installation + the latest version of Toolset plugins, here are detail steps I tried:
1) Login as user "Darryl", it is the only administrator user of your website.
2) Open URL as you mentioned above:
enlace oculto
I don't see any PHP errors.
3) Edit theme file "functions.php", add the PHP codes you mentioned above:
https://toolset.com/forums/topic/conditionals-and-access-to-not-have-a-shortcode-processed-doesnt-work/#post-1561801
4) Test the URL again, I get these PHP error:
Warning: Invalid argument supplied for foreach()
Is it the same problem as you mentioned above?
If it is, this expected result, the problem is in your custom PHP codes, Toolsetshortcodes [toolset_access] and [wpv-conditional] shortcodes work as below, if the conditions are satisfied then display those specific content, but even those condition are not satisified, it will parse those contents into HTML codes, but do not display them.
So in your case, you just need to change your PHP codes as below:
add_shortcode('parent_check_user_access', 'func_parent_check_user_access');
function func_parent_check_user_access($atts, $content = '') {
global $post;
// get post id of the member page
$member_id = $post->ID;
// use the member page id to get the id of the parent (contractor) page
$contractor_parent_id = toolset_get_related_post( $member_id, 'contractor-member', 'parent' );
// use the parent (contractor) page id to get the repeating field contents
$all = get_post_meta($contractor_parent_id,'wpcf-contractor-access-usernames');
// add below three lines
if(!$all){
//$all = array();
}
// first get the current user, then get their email address
$current_user = wp_get_current_user();
$current_user_access = $current_user->user_email;
// check the current user's email address against the ones in the repeating field
foreach($all as $k=>$v):
if( $v == $current_user_access)
return 1;
endforeach;
return 0;
}
And test again
That got rid of the PHP error (thank you) but should I be concerned that the PHP code is running in the first place? The shortcode shouldn't have been executed because it was being restricted by both an access statement and conditional statement.
No, you don't need to concerned that the PHP code is running in the first place.
This is expected result, the shortcode will be excuted before outputting, see WordPress document:
When the_content is displayed, the shortcode API will parse any registered shortcodes such as "[myshortcode]", separate and parse the attributes and content, if any, and pass them the corresponding shortcode handler function. Any string returned (not echoed) by the shortcode handler will be inserted into the post body in place of the shortcode itself.
https://codex.wordpress.org/Shortcode_API#Overview
My issue is resolved now. Thank you!