简体   繁体   中英

javascript inside php if specific user

Im new to PHP, so maybe this is a stupid question. I have some code that should show a livechat for all Wordpress users, in access level 7, and show nothing to people who have another access level.

  //Checks if current_user_on_level($level_id) and returns true or false
if ( current_user_on_level(7) ) {
    <!--Start of Zopim Live Chat Script-->
    <script type="text/javascript">
     window.$zopim||(function(d,s){var z=$zopim=function(c){z._.push(c)},$=z.s=
     d.createElement(s),e=d.getElementsByTagName(s)[0];z.set=function(o){z.set.
     _.push(o)};z._=[];z.set._=[];$.async=!0;$.setAttribute('charset','utf-8');
     $.src='//v2.zopim.com/?0xObfhlxyhOFFN07eRruAdGFbEDphAiO';z.t=+new Date;$.
     type='text/javascript';e.parentNode.insertBefore($,e)})(document,'script');
     </script>
     <!--End of Zopim Live Chat Script-->
          } else {
            // Do nothing
          }

I get an error, that say there is an error on the first line where the JS begins.

Since the code is JS and not PHP you must close the PHP tag and add it as HTML so add a ?> and

if ( current_user_on_level(7) ) {
?>
    <!--Start of Zopim Live Chat Script-->
    <script type="text/javascript">
     window.$zopim||(function(d,s){var z=$zopim=function(c){z._.push(c)},$=z.s=
     d.createElement(s),e=d.getElementsByTagName(s)[0];z.set=function(o){z.set.
     _.push(o)};z._=[];z.set._=[];$.async=!0;$.setAttribute('charset','utf-8');
     $.src='//v2.zopim.com/?0xObfhlxyhOFFN07eRruAdGFbEDphAiO';z.t=+new Date;$.
     type='text/javascript';e.parentNode.insertBefore($,e)})(document,'script');
     </script>
     <!--End of Zopim Live Chat Script-->
<?PHP
          } else {
            // Do nothing
          }

Your code tries to execute the javascript code as if it was PHP code. You need to include the javascript code in your server's response. You can do that by using echo :

if ( current_user_on_level(7) ) {
echo <<<EOT
<!--Start of Zopim Live Chat Script-->
<script type="text/javascript">
 window.$zopim||(function(d,s){var z=$zopim=function(c){z._.push(c)},$=z.s=
 d.createElement(s),e=d.getElementsByTagName(s)[0];z.set=function(o){z.set.
 _.push(o)};z._=[];z.set._=[];$.async=!0;$.setAttribute('charset','utf-8');
 $.src='//v2.zopim.com/?0xObfhlxyhOFFN07eRruAdGFbEDphAiO';z.t=+new Date;$.
 type='text/javascript';e.parentNode.insertBefore($,e)})(document,'script');
 </script>
 <!--End of Zopim Live Chat Script-->
EOT;
      } else {
        // Do nothing
      }

Or by closing the PHP tag:

if ( current_user_on_level(7) ) { ?>
<!--Start of Zopim Live Chat Script-->
<script type="text/javascript">
 window.$zopim||(function(d,s){var z=$zopim=function(c){z._.push(c)},$=z.s=
 d.createElement(s),e=d.getElementsByTagName(s)[0];z.set=function(o){z.set.
 _.push(o)};z._=[];z.set._=[];$.async=!0;$.setAttribute('charset','utf-8');
 $.src='//v2.zopim.com/?0xObfhlxyhOFFN07eRruAdGFbEDphAiO';z.t=+new Date;$.
 type='text/javascript';e.parentNode.insertBefore($,e)})(document,'script');
 </script>
 <!--End of Zopim Live Chat Script-->
 <?php
      } else {
        // Do nothing
      }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM