I want to enqueue my plugin for certain pages. I've tried several things but nothing worked so far:
add_action( 'init', 'my_enqueue' );
function my_enqueue() {
global $post;
if( $post->ID == 380 || is_home() || is_front_page() || is_single(380) || is_page(380)) {
wp_enqueue_script( 'lister_js', plugins_url( '/js/lister.js', __FILE__ ), array('jquery'), filemtime( '/js/lister.js', __FILE__ ));
wp_localize_script( 'lister_js', 'my_ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
}
}
The post id is 380 according to the db. I even checked the url and it is ".../wp-admin/post.php?post=380&action=edit". So I'm pretty sure the id is correct.
I want to enqueue my plugin for certain pages. I've tried several things but nothing worked so far:
add_action( 'init', 'my_enqueue' );
function my_enqueue() {
global $post;
if( $post->ID == 380 || is_home() || is_front_page() || is_single(380) || is_page(380)) {
wp_enqueue_script( 'lister_js', plugins_url( '/js/lister.js', __FILE__ ), array('jquery'), filemtime( '/js/lister.js', __FILE__ ));
wp_localize_script( 'lister_js', 'my_ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
}
}
The post id is 380 according to the db. I even checked the url and it is ".../wp-admin/post.php?post=380&action=edit". So I'm pretty sure the id is correct.
Share Improve this question asked Jun 20, 2020 at 12:18 KitiaraKitiara 1114 bronze badges1 Answer
Reset to default 0It started to work once i change
add_action( 'init', 'my_enqueue' );
to
add_action( 'wp_enqueue_scripts', 'my_enqueue' );