What is wrong with code added to functions.php to selectively show styles based on login state

When I try to add the following code to functions.php I keep getting errors (site goes down, seeming to indicate a php e

When I try to add the following code to functions.php I keep getting errors (site goes down, seeming to indicate a php error). I've stared at this code forever and can't seem to figure out why it should give an error when it's added to functions.php.

Any suggestions for what I should change? Thanks!

function hide_prompt() {
   if(is_user_logged_in()) {
    echo '
    <style> .app { display: none!important; } </style>   
    ';  
   }
}
add_action('wp_footer', 'hide_prompt');

When I try to add the following code to functions.php I keep getting errors (site goes down, seeming to indicate a php error). I've stared at this code forever and can't seem to figure out why it should give an error when it's added to functions.php.

Any suggestions for what I should change? Thanks!

function hide_prompt() {
   if(is_user_logged_in()) {
    echo '
    <style> .app { display: none!important; } </style>   
    ';  
   }
}
add_action('wp_footer', 'hide_prompt');
Share Improve this question asked Sep 13, 2019 at 5:20 Vanessa HarrisVanessa Harris 1 6
  • from config file set WP_DEBUG true, refresh and check if you see any php error – Sangita Kendre Commented Sep 13, 2019 at 6:00
  • I have checked this above code into website and it's working perfect. so I think there is no issue with code. may be possible other coding issue for getting errors. – Tanmay Patel Commented Sep 13, 2019 at 6:21
  • You might have a plugin or your theme defining a function called hide_prompt - try changing the name of the function to something unique, eg vanessa_hide_prompt. – Peter HvD Commented Sep 13, 2019 at 9:54
  • 1 The original code in your question (the one in the text box for editing the question) contains invisible characters which result in "unexpected echo" error - and if you copy the rendered code on this page (not in the text box), then your code should work fine. So be extra careful when copying/pasting code. – Sally CJ Commented Sep 14, 2019 at 1:19
  • Thanks for the tips. Peter, I tried a number of different names and it still errors. Sally, how did you tell there were invisible characters? (I assume you mean beyond spaces and tabs?) – Vanessa Harris Commented Sep 14, 2019 at 1:53
 |  Show 1 more comment

1 Answer 1

Reset to default 1

You are using the wrong hook, and you are doing it the wrong way. Sorry to be clear.

Adding stylesheet to the footer is no good style at all and rather a beginner mistake. Have a look at loading scripts correctly.

You need to add that stylerule the to html-head of your login page/hook.

Since you want to edit the login form, the correct hook, which you need to use is login_head

See Codex: Login Hooks

Also see: Login Head

something like this (functions.php):

function se_css_output_hide_promt() { ?>
    <style type="text/css" id="se-answer-customized-css">
        <?php
        //switch to php to check the login status, add css when true
        if(is_user_logged_in()): ?>

        .app { display: none!important; }

        <?php endif; ?>
    </style>
<?php }
add_action('login_head', 'se_css_output_hide_promt');

I bet !important is no longer needed in case you are doing it this way. This css will be added right before the ending html-head, very close to your html. Meaning that your !important is propably no longer necessary.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745161348a4614403.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信