0

I have been attempting different methods of adding jquery to my site for a while now - and found that the best practice is to enqueue it in functions.php. Specifically i am trying to add a 'sticky' nav bar that snaps to the top of the page as the user scrolls down. However when i do this my site get a 500 error and I have to remove it to get the site back up and running.

It is obviously the functions.php code to enqueue the script or the script itself causing the problem - however i have not been able to identify which and of course why. (could there be an issue with how my server is set up? lack of resources/permissions ect?)

Here is my Functions.php extract:

    function verdigris_script() {
// register your script location, dependencies and version
   wp_register_script('custom_js',
   get_template_directory_uri() . '/js/scripts.js',
   array('jquery'),
   '1.0', true );

function scripts_enqueue() {
    //css
    wp_enqueue_style('bootstrap', get_template_directory_uri() . '/css/bootstrap.min.css', array(), 'all');
    wp_enqueue_style('customstyle', get_template_directory_uri() . '/css/verdigris-style.css', array(), 'all');
    //js
    wp_enqueue_script('jquery');
    wp_enqueue_script('bootstrapjs', get_template_directory_uri() . '/js/bootstrap.min.js', array(), true);
    wp_enqueue_script('custom_js');

}
add_action( 'wp_enqueue_scripts', 'verdigris_script');

and this is my script:

    jquery(function() {
  var $container = $('.container');
  var $b = $('body');
  $.waypoints.settings.scrollThrottle = 0;
  $container.waypoint({
    handler: function(e, d) {
      $b.toggleClass('sticky', d === 'down');
      e.preventDefault();
    }
  });
});

am asking if anyone can identify the issue.

Many thanks in advance.

MoxieProxy
  • 65
  • 7
  • 3
    Always check the server error logs to find out the reason behind a 500 error message. The error itself is too broad and covers a multitude of issues. – aynber Apr 06 '17 at 13:29
  • You can usually find the error message if you check error logs or turn on debugging in your wordpress settings, here is a question with a good answer in regards to debugging http://stackoverflow.com/questions/1080679/php-and-wordpress-debugging – Uberswe Apr 06 '17 at 13:30
  • In my log files i get : thrown in wp-content/themes/Verdigris/functions.php on line 20" while reading response header from upstream, client: 86.170.108.202, server: , request: "POST /wp-admin/admin-ajax.php HTTP/2.0", upstream: "fastcgi://unix:$ 2017/04/06 10:56:24 [error] 17097#17097: *3412 FastCGI sent in stderr: "PHP message: PHP Fatal error: Uncaught Error: Call to undefined function wp_register_scripts() in /wp-content/themes/Verdigris/functions.php:20 Stack trace: – MoxieProxy Apr 06 '17 at 13:34
  • you dont have to do this wp_enqueue_script('jquery'); wordpress ships with its own version of jquery – victor Apr 06 '17 at 14:24
  • Thanks, ill remove that. do you have any insight in to the error message i get? – MoxieProxy Apr 06 '17 at 14:28

1 Answers1

0

You have an error in your PHP code.

You're unintentionally declaring a function inside another function and never closing it.

Remove:

function scripts_enqueue() {
Nathan Dawson
  • 18,138
  • 3
  • 52
  • 58
  • Thank you. ^ this worked. I knew itd be something trivial i had missed. – MoxieProxy Apr 06 '17 at 14:32
  • Does this mean i don't need to add a new function when enqueueing after registering? – MoxieProxy Apr 06 '17 at 14:39
  • when writing PHP code, it is a good idea to check that it's valid PHP syntax before trying to run it. Any good IDE or editor will tell you this automatically and highlight the errors, which is why I always suggest using a decent editor, but you can also run `php -l ` to get PHP to check for syntax errors without running the code. – Simba Apr 06 '17 at 14:43
  • @MoxieProxy You can register and enqueue a script within the same function. It's equally fine to register in one function and enqueue in another. Your error was down to a mistake in PHP. If you open a function you need to close it. – Nathan Dawson Apr 06 '17 at 14:54