• Resolved dosenlunch

    (@dosenlunch)


    Hi,

    I am trying to do what should be really simple, but somehow this is driving me mad. I try to have a code snippet only run on the front page. My site is set to have a static front page so I think my key to success is the conditional tag is_front_page(). However, I cannot get it to run as no matter what I try, I always end up with an error message in the console saying “ReferenceError: Can’t find variable: is_front_page”.

    Here is my very basic code that I am using to only test if the conditional tag is working:

    <?php
    add_action('wp', function(){; ?>
    <script>
    	window.addEventListener("load", function() {
    		if( is_front_page() ){
    			console.log("front page")
    		};
    	});	
    </script>
    <?php });

    I already spend hours browsing through the internet and found that conditional tags only work after certain elements have been loaded, hence I hooked the function in ‘wp’. Also, I have read that some similar issues were resolved by restoring the original wp_query, so I added <?php wp_reset_query();?>, but again, no change. I have also tried using is_home(), but ended up with the same error message. Also, I have disabled all plugins, no change. It is really strange as I don’t even get neither is_home() nor is_front_page() to run, so I figure there must be some rather general issue.

    Any help is appreciated.
    Regards, happy easter and stay safe!
    Dominik

    • This topic was modified 4 years, 7 months ago by dosenlunch.
Viewing 2 replies - 1 through 2 (of 2 total)
  • The code doesn’t do anything:

    1. “add_action(‘wp’, function…
      ” doesn’t have a valid name. In the name of actions “wp” is prefix and needs always something after. I presume that in this case you need apparently “wp_head”.
    2. PHP-function inside JavaScript in this case doesn’t do anything. If you want PHP to create conditional code together with JavaScript, you should split JavaScript into several parts or put the PHP function outside from the JavaScript code

    The following code creates JavaScript, if the page is front page.

    <?php add_action('wp_head', function(){??if( is_front_page() ) { ?>
    
    <script> window.addEventListener("load", function() {console.log("front page") };); </script> <?php }
    
    });

    Note in the end additional ‘}’.
    You can create JavaScript in several parts, but you need more code. If you use PHP-functions, you must separate them from JavaScript.

    • This reply was modified 4 years, 7 months ago by tapiohuuhaa.
    • This reply was modified 4 years, 7 months ago by tapiohuuhaa.
    • This reply was modified 4 years, 7 months ago by tapiohuuhaa.
    • This reply was modified 4 years, 7 months ago by Yui.
    Plugin Author Shea Bunge

    (@bungeshea)

    You’re trying to use a PHP function within JavaScript code. @tapiohuuhaa provides a correct way of doing things – this isn’t anything to do with the core Code Snippets plugin, but feel free to let us know if you’d like some more assistance.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘is_front_page() error message’ is closed to new replies.