0
votes

I'm attempting to register and enqueue a script from a javascript file in a child theme. The script appears to register just fine, returning true, however nothing happens when the script is enqueued. Are there any problems with the way I am registering or enqueueing?

In functions.php, I'm checking the user's role, and registering and enqueuing scripts from custom_functions.js if that user has the role of 'ad_greensboro'.

function tutor_portal_scripts() {
    $user = wp_get_current_user();
    if (in_array( 'ad_greensboro', (array) $user->roles ) ) {
        debug_to_console("User is AD Greensboro");
        wp_register_script('set_rollup_links_greensboro', get_stylesheet_directory_uri() . '/js/custom_functions.js', array(jquery), '', true);
        wp_enqueue_script('set_rollup_links_greensboro');
    } 
}
add_action( 'wp_enqueue_scripts', 'tutor_portal_scripts' );

Below is the entire contents of my custom JS file. Its purpose is to change the href property for three links on a specific page.

function set_rollup_links_greensboro() {
	console.log("Set rollup links for Greensboro AD");
	
	var at_link = document.getElementById('at-view-link');
	var bas_link = document.getElementById('bas-view-link');
	var tutor_link = document.getElementById('tutors-view-link');

 	at_link.setAttribute('href', 'https://devts.techstartutors.com/appointment-tracker-form-view-greensboro-nc/');
 	bas_link.setAttribute('href', 'https://devts.techstartutors.com/book-a-service-form-view-greensboro-nc/');
 	tutor_link.setAttribute('href', 'https://devts.techstartutors.com/');
}

Thanks in advance.

1
What does error_log( in_array( 'ad_greensboro', (array) $user->roles ) ); return? (make sure you have debugging enabled first)cabrerahector
array(jquery) is a typo, hopefully not in your code. Must be array("jquery") or array('jquery')random_user_name

1 Answers

1
votes

In your javascript file, you should call that function. So add the following code to the end of your javascript file:

set_rollup_links_greensboro();