0

I have some javascript in my footer that are only being dropped in if they pass a php check. Due to some page optimisations I would like to enqueue these scripts. I tried to enqueue the php scripts like so:

function add_base_scripts() {
    wp_enqueue_script(get_template_directory_uri() . '/base-scripts.php');
}
add_action('wp_footer', 'add_base_scripts');

But I'm not seeing any of the scripts from base-scripts.php being put into the page. It's definately the correct directory which makes me think its a problem with the scripts being in a php file. I haven't found anything online as to if you can use a php file or not. Can someone confirm/debunk this?

2 Answers 2

0
function add_base_scripts() {
   wp_enqueue_script('script-handle',get_template_directory_uri() . '/base-scripts.php');
}
add_action('wp_enqueue_scripts', 'add_base_scripts');

You got to add a handle to the enqueue_scripts more info

Sign up to request clarification or add additional context in comments.

3 Comments

Just added a handle - same problem. I can't see the scripts from base-scripts.php in the page.
Oh sorry, i read over the .php part, can't you just change it to include(FILEPATH) instead of enqueuescript?
We're using enqueue script because of the way the plugin autoptimize picks up scripts. If we just include the scripts there are errors because they get loaded before jQuery.
0

After fiddling with this, we came to the conclusion that you cannot enqueue a php script. We had to use a workaround to solve our problem.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.