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;
}
}
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;
}
}
If nothing has been placed into your shortcode "wpv-current-user", you won't see anything in any of your output.
You should use the built-in function wp_get_current_user()
for this.
Just beware that the Codex page warning. This may need to be called within an action that fires with or after the 'init' hook.
I have modified your code so you should get results from both error_log() and from placing your shortcode [show_css_code_conditionally]
in a post/page.
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 = wp_get_current_user();
error_log($current_user->ID!==0 ? $current_user->user_email : 'User not logged in',0);
<?php
if ($current_user->ID!==0) {
// this is confirmed that the user is logged in
return $CSS_output;
} else {
return "\n<!-- No valid user currently logged in -->\n\n";
}
}
Since I don't know what the shortcode [wpv-current-user]
refers to, I just left it out. Usually, you would just do_shortcodes()
to execute all applicable shortcodes, especially when you are already in a shortcode function.
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 ♦ Commented Aug 29, 2019 at 18:58