0

I am battling with a problem for months now, that my code will not execute or show the result, when I enqueue it. I have been told, that my code is fine, and that it should work. The clock is showing, so the css and html is correct, but not doing anything. Here it is:

<?php

function childtheme_parent_styles() {
 wp_enqueue_style( 'parent', get_template_directory_uri().'css/style.css' );
function mytheme_files() {
 wp_enqueue_style('mytheme_main_style', get_stylesheet_uri());
wp_register_script('main-js', get_stylesheet_directory_url() . '/js/main.js');
}

add_action( 'wp_enqueue_scripts', 'childtheme_parent_styles');

wp_register_script('main-js',get_stylesheet_directory_url().'/js/main.js');

}

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

function pr_scripts_styles() {
if( is_page(507) ) {
wp_enqueue_script( 'main-js');      


}

The js code is in a folder named js and in a file named main.js

setInterval(() => {
    d = new Date(); //object of date()
    hr = d.getHours();
    min = d.getMinutes();
    sec = d.getSeconds();
    hr_rotation = 30 * hr + min / 2; //converting current time
    min_rotation = 6 * min;
    sec_rotation = 6 * sec;
  
    hour.style.transform = `rotate(${hr_rotation}deg)`;
    minute.style.transform = `rotate(${min_rotation}deg)`;
    second.style.transform = `rotate(${sec_rotation}deg)`;
}, 1000);
<?php

function childtheme_parent_styles() {
    wp_enqueue_style( 'parent', get_template_directory_urI().'/css/style.css' );
    function mytheme_files() {
        wp_enqueue_style('mytheme_main_style', get_stylesheet_uri());
        wp_register_script('main-js', get_stylesheet_directory_uri() . '/js/main.js');
    }

    add_action( 'wp_enqueue_scripts', 'childtheme_parent_styles');

    wp_register_script('main-js',get_stylesheet_directory_uri(). '/js/main.js');

}

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

function pr_scripts_styles() {
    if( is_page(507) ) {
        wp_enqueue_script( 'main-js');
    }

Will check the dev tools.

7
  • Oh, thank you so much, I will let you know tommorow if it works. Sep 16, 2021 at 17:23
  • 1
    Is the PHP code the entire code? There's a missing } at the end, also where is the code? Which file is it in. If there is an error it will show in your PHP error log, have you checked? And when you say the code doesn't run, do you mean it fails? Or the page crashes? Or there's an error? Can you be more specific?
    – Tom J Nowell
    Sep 16, 2021 at 18:11
  • 2
    Use the edit link underneath the tags in your question to update it, rather than posting updated code as the solution to your question. This isn't a discussion forum so people will see you responded to your own question and assume you were posting the solution
    – Tom J Nowell
    Sep 16, 2021 at 18:13
  • 1
    I also strongly recommend indenting the code correctly to avoid problems, there are a number of problems that are hidden by the lack of indentation
    – Tom J Nowell
    Sep 16, 2021 at 18:14
  • 1
    I updated the last snippet you posted and added indentation and it's clear there are major PHP syntax issues, such as nested functions, brackets that don't match up ( { and } ), and hooks that add themselves inside themselves so they would never run, and functions that are started but never finished
    – Tom J Nowell
    Sep 16, 2021 at 18:17

1 Answer 1

0

There are two errors I see in your code. First is that you've written get_stylesheet_directory_url() instead of get_stylesheet_directory_uri(). Switch the L for an I and that will probably fix it.

Second, for the JS file you're adding a slash before the folder, and for the CSS you're not. Once you fix the URL/I issue, you may still need to remove the /

Update I've reviewed and updated your enqueue styles. There were a number of issues, but I've tightened it up, and this will load your styles and scripts:

function childtheme_parent_styles() {
    wp_enqueue_style('parent', get_template_directory_uri().'/css/style.css' );
    wp_enqueue_style('mytheme_main_style', get_stylesheet_uri());
    wp_register_script('main-js', get_stylesheet_directory_uri() . '/js/main.js');
    if( is_page(507) ) {
        wp_enqueue_script( 'main-js');
    }
}

add_action( 'wp_enqueue_scripts', 'childtheme_parent_styles');

Good luck!

5
  • No, it still does not execute. I put the i in and tried it with a small i and capital i. I also put a slash on the css and js, and tried them without the slashes. Sep 16, 2021 at 17:54
  • Have you checked dev tools to confirm that the file itself is loading? That's the first step. And being able to see the code in place will help a lot for solving the problem.
    – Pixelsmith
    Sep 16, 2021 at 17:58
  • 1
    @Pixelsmith check the PHP code, it's got a lot of basic syntax errors and mistakes
    – Tom J Nowell
    Sep 16, 2021 at 19:31
  • @TomJNowell thanks! I thought I'd start with the obvious stuff! :D
    – Pixelsmith
    Sep 16, 2021 at 21:32
  • Where would I find the php error log. Must I look for a file called php_ini configuration file. Sep 17, 2021 at 5:56

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.