I am very new to javascript and php.
I am trying to write script in the Code Snippets plugin for a wordpress site to make anchor links to accordions open the accordion.
For example, the URL https://challengingbehavior.org/pyramid-model/overview/resources/#generalResources when clicked, should open the page, then scroll down to and open the accordion 'General Resources' with ID #generalResources.
Here is the code I created
function add_this_script_footer(){ ?>
<script>
$(document).ready(function(){
var hash = window.location.hash;
if(hash){
var element = $(hash);
if(element.length){
element.trigger('click');
}
}
});
</script>
<?php }
add_action('wp_footer', 'add_this_script_footer'); ?>
When I try to activate the snippet, it gives me the error "Could not create snippet. Request failed with status code 418."
I found this online... https://wordpress.org/support/topic/could-not-create-snippet-request-failed-with-status-code-418/ But not knowing enough about code, I don't know if this is my problem or something else.
If it matters...Dreamhost, Wordpress 6.4.2, PHP 8.3.
Any help would be appreciated.
Update: Here is the code I put in the functions.php file instead. I am getting syntax errors and it causes an critical error on the site.
//anchor link open accordion
function anchor_open_accordion() {
$(document).ready(function() {
var hash = window.location.hash;
if (hash){
var element = $(hash);
if (element.length){
element.trigger('click');
}
}
});
}
add_action('wp_footer', 'anchor_open_accordion');
Can someone tell me what change I need to make to my code to get it to work?