I am trying to inspect a variable obtained in functions.php for debugging purposes. I am defining a variable $current_user by executing the shortcode to obtain the current logged-in user. Then I am using an if statement to validate that there is a currently logged-in user, in which case the CSS styling defined earlier in the function should be applied. It's not currently working, so I'm trying to investigate the $current_user variable, but none of the variations I have tried are working, including error_log.
functions.php (fragment):
add_shortcode('show_css_code_conditionally', 'show_css_code_conditionally_fn');
function show_css_code_conditionally_fn($atts) {
ob_start(); ?>
// CSS code to show the button conditionally
<style type="text/css">
.special-button {
display: block !important;
}
</style> <?php
$CSS_output = ob_get_clean();
$current_user = do_shortcode('[wpv-current-user]');
error_log($current_user);
echo($current_user);
echo do_shortocde('[wpv-current-user]');
print 'zzzy';
?><pre><?php var_dump( $current_user ); echo do_shortocde('[wpv-current-user]'); print 'zzzy'; ?></pre><?php
if ($current_user) {
// this is confirmed that the user is logged in
return $CSS_output;
}
}
do_shortocde
should bedo_shortcode
, but have you tried just executing the function? You don't need to turn ti into a shortcode to run it. Keep in mind that shortcodes only run when they're used inside post content. Can you explain what your original problem is that lead to this? What were you trying to do before you needed to debug things? – Tom J Nowell♦ Aug 29 '19 at 18:58