0

I am writing my first function in the functions.php file in my child theme and am running into problems getting it to work. I keep getting an error that seems to be syntax related, like WP doesn't want to see "var". I am also not clear how to run the function either, being a very basic programmer.

Here is the code I got working in JSfiddle that I am trying to use in WP:

function adjustLogoHeight() {

var divHeight = $('#et-top-navigation').height();

 $('.logo_container').css('height', divHeight+'px');

};

adjustLogoHeight();

If you have a solution that I can try, I would really appreciate it. Thanks in advance.

1
  • Hi there! Would it be possible to see the exact error message? Thanks!
    – nibnut
    Commented Jan 30, 2017 at 20:49

1 Answer 1

3

Your code is based on jQuery and you are including it to a PHP file. So you need to call it between <script></script> tag and obviously either you must end and start PHP tags before <script> and after </script> or echo the full script.

Now come to how you are gonna execute the code. You can hook the adjustLogoHeight() function to any hook which fires on where you need to put the function-

add_action( 'wp_head', 'adjustLogoHeight' );

Or you can direct call the function where you want to put the function by calling the function like adjustLogoHeight().

Now a little update on your jQuery code-

function adjustLogoHeight() { ?>
    <script>
        (function( $ ) {
            'use strict';
            var divHeight = $('#et-top-navigation').height();
            $('.logo_container').css('height', divHeight+'px');
        })( jQuery );
    </script>
<?php };

Write your function like above. It's more appropriate.

Must call the adjustLogoHeight function after jQuery is loaded to the DOM. Otherwise you'll get error whether you're calling it directly or hooking it to any hook.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.