I work with wordpress. I would like to auto-enqueue jQuery script CodXXX.js on page id XXX if the relative file exists.
I wrote:
/* Enqueue CondXXX.js on page XXX if file CondXXX.js exists */
function carica_scripts() {
$pageId = get_the_ID();
$myBaseURL = get_stylesheet_directory_uri() . '/js/';
$hookName = 'Cond' . $pageId;
$condFile = $myBaseURL . $hookName . '.js';
if (file_exists($condFile)) {
wp_enqueue_script($hookName, $condFile, array('jquery'));
}
}
add_action( 'wp_enqueue_scripts', 'carica_scripts' );
But I can't manage to have the scripts loaded.
Any idea on how to make it?
Aucun commentaire:
Enregistrer un commentaire