1

I've got a realy hair pulling problem here.

I've started cleaning up a plugin i've made and sort some of the functions in diffrent files.

but i've got a problem with wp_enqueue_scripts.

when i place the function where i register and enqueue in the main php file and try to enqueue my script from there it works as it should

however if i move the function to a diffrent php file and try to enqueue it from that file and include this php file from the main php file nothing happens however all other funtions from this php file works as they should

do i need to call a wordpress function from my other php file to be able to unqueue it from there or?

EDIT:

If if place and call the function in my main php file like below it works like a charm and and the js file is shown in the head when i look for it with firefox

function scripts() { 
wp_register_script( 'load_table', plugins_url( 'path/js/load_table.js', __FILE__ ), array( 'jquery' ) );

wp_enqueue_script( 'load_table' );
}

add_action( 'wp_enqueue_scripts', 'scripts' );

However if i try to write it like below nothing happens and it cant be found in the head either

main.php:

include'path/to/other.php';

other.php:

function scripts() { 
wp_register_script( 'load_table', plugins_url( 'vagt_oversigt/js/load_table.js', __FILE__ ), array( 'jquery' ) );

wp_enqueue_script( 'load_table' );
}


add_action( 'wp_enqueue_scripts', 'scripts' );

The problems seems to be happening when i use hooks with functions on other.php

just echo "hello world"; in other.php it works like a charm.

But if i try calling echo with in a function by init nothing happens either.

other.php:

function test() {

echo "hello world";
}
add_action('init', 'test');

BTW! yes i did try changing the path without sucsess. any ideas? :(

  • 1
    And please post your relevant code. "I did something in a file that I included somehow" isn't much of a trouble ticket. – s_ha_dum Jul 23 '15 at 14:39
  • added the code to my question – Alexander Jul 23 '15 at 15:57
  • 1
    see plugins_url in codex, particularly the 2nd argument. – Milo Jul 23 '15 at 16:19
  • okay tried changing the path as following: wp_register_script( 'load_table', plugins_url( 'vagt_oversigt/js/load_table.js', dirname(FILE) ), array( 'jquery' ) ); and then i tried echoing the path to make sure its the right path and it is but the js file still dossent get loaded :/ – Alexander Jul 23 '15 at 16:38
1

I found the problem....

I ran main.php like:

function run_this(){

include'path/to/other.php';
}
add_shortcode('shortcode', 'run_this');

And most of the content of other.php came up exept the hooks did'nt work then i tried using add_action('init', 'run_this'); and then it worked.... exept that i get a fatal error that i cant redeclare some functions twice :D

0

You have to place the enqueue code with in a function and use add_action to call the function where you register.

Like below

add_action( 'wp_enqueue_scripts', 'your_function_name', 10 );
  • I know how to enqueue scripts ;) – Alexander Jul 23 '15 at 16:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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