
Hi there,
I have created a ACF True/False field which is attached to my users. This is used as a access level option, so if the option is True they will see a link in the menu, if false, they will see a fallback link.
Below is the code iv added to my functions.php where iv created a shortcode: “customLink” which is then added to my menu link.
Below is what i have so far, which hopefully shows what im trying to do. If the users role us ‘my_custom_role’, then to get the ACF value, and if true, show one link, and if false show another.
Where am i going wrong?
add_shortcode('customLink', 'cm_link');
function cm_link($atts) {
$atts = shortcode_atts(array('title' => 'Custom Link', 'fallbackurl' => '#'), $atts, 'customLink');
$advcontent = get_field('acf_field', $current_user->ID);
$user = wp_get_current_user();
if (!empty($user->roles) && is_array($user->roles)) {
foreach ( $user->roles as $role ) {
if ($role=='my_custom_role') {
if ($advcontent == 'true') {
return "<a href='/my-link/'>".$atts['title']."</a>";
}
if ($advcontent == 'false') {
return "<a>".$atts['title']."</a>";
}
}
}
}
}
$advcontent = get_field('acf_field', 'user_'.$current_user->ID);