• Skip to main content
  • Skip to forum navigation

StudioPress

  • Shop for Themes
  • My StudioPress

Forum navigation

  • Home
  • General Genesis Discussions
  • StudioPress Themes
  • Genesis Blocks
    • Genesis Blocks
    • Genesis Custom Blocks
  • Retired Themes
  • FAQs
  • Forum Rules
  • Internationalization and Translations
  • Forum Bugs and Suggestions
  • Forum Log In

Are You Using The WordPress Block Editor?

Genesis now offers plugins that help you build better sites faster with the WordPress block editor (Gutenberg). Try the feature-rich free versions of each plugin for yourself!

Genesis Blocks Genesis Custom Blocks

Load Script If Not Logged In

Welcome!

These forums are for general discussion on WordPress and Genesis. Official support for StudioPress themes is offered exclusively at My StudioPress. Responses in this forum are not guaranteed. Please note that this forum will require a new username, separate from the one used for My.StudioPress.

Log In
Register Lost Password

Community Forums › Forums › Archived Forums › General Discussion › Load Script If Not Logged In

This topic is: not resolved
  • This topic has 5 replies, 2 voices, and was last updated 10 years, 7 months ago by webnonsense.
Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • April 22, 2015 at 2:31 pm #148837
    webnonsense
    Member

    I am trying to load a third party script into the <head>. It MUST be loaded at the very top like this.

    <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <script src="//cdn.optimizely.com/js/2801570498.js"></script>

    So I can't use the Script box in the them settings.

    In addition to that, I don't want it to load when someone is logged in.

    I have done some searching but can't quite piece it all together.

    Thank you.

    April 22, 2015 at 3:57 pm #148841
    Badlywired
    Member

    This will do it (add to your functions.php)

    add_action ('genesis_doctype','myscript',20);
    function myscript() {
    	?>
    	<script src=”//cdn.optimizely.com/js/2801570498.js”></script>
    	<?php
    }

    Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=ZEWW5LKK5995J


    My techy blog WordPress and stuff badlywired.com

    April 22, 2015 at 4:06 pm #148843
    webnonsense
    Member

    That does the trick as far as getting it at the top, but I don't want it to load when I am logged in.

    Also, I have never seen php executed with that syntax before. It looks strange with the question mark first and then the <?php afterwards. How come it is like that?

    Thank you for your help.

    April 22, 2015 at 4:08 pm #148844
    Badlywired
    Member

    I forgot that hang on a minute


    My techy blog WordPress and stuff badlywired.com

    April 22, 2015 at 4:13 pm #148845
    Badlywired
    Member

    OK (untested but should work unless I have made a typo)

     add_action ('genesis_doctype','myscript',20);
         function myscript() {
    	if ( !is_user_logged_in() ) {      ?>
    	<script src=”//cdn.optimizely.com/js/2801570498.js”></script>
    	<?php   }
    }

    can also be written

    add_action ('genesis_doctype','myscript',20);
         function myscript() {
    	if ( !is_user_logged_in() ) {     
    	echo '<script src=”//cdn.optimizely.com/js/2801570498.js”></script>';
    	  }
    }

    The ?> ends php and switches to just html output and <?php restarts the php
    the second version just uses php and echos the html - it does the same thing


    My techy blog WordPress and stuff badlywired.com

    April 22, 2015 at 4:23 pm #148846
    webnonsense
    Member

    Oh yeah, closing the tag makes sense. PHP is not my forte.

    That did the trick. I was playing around with !is_user_logged_in yesterday but for some reason it wasn't working.

    Thanks for your help.

  • Author
    Posts
Viewing 6 posts - 1 through 6 (of 6 total)
  • The forum ‘General Discussion’ is closed to new topics and replies.

CTA

Ready to get started? Create a site or shop for themes.

Create a site with WP EngineShop for Themes

Footer

StudioPress

© 2025 WPEngine, Inc.

Products
  • Create a Site with WP Engine
  • Shop for Themes
  • Theme Features
  • Get Started
  • Showcase
Company
  • Brand Assets
  • Terms of Service
  • Accptable Usse Policy
  • Privacy Policy
  • Refund Policy
  • Contact Us
Community
  • Find Developers
  • Forums
  • Facebook Group
  • #GenesisWP
  • Showcase
Resources
  • StudioPress Blog
  • Help & Documentation
  • FAQs
  • Code Snippets
  • Affiliates
Connect
  • StudioPress Live
  • StudioPress FM
  • Facebook
  • Twitter
  • Dribbble