I want to add multiple HTML attributes to a button which is present across all blog posts on my website. The buttons all have links in them, to which I want to add like 3-4 'rel' attribute values. I coded a basic function that applies the attributes to all buttons of the same class on single blog posts. I am just an amateur at coding so I don't know if the below code is correct. I added this to my functions.php file and it did not throw any syntax errors but it also doesn't work.
I would like to do this without using any additional plugins. Any help is much appreciated.
add_action('wp_head', 'buttonRel'); function buttonRel() { if (is_single ('^\d+$')) { ?> <script type="text/javascript"> document.getElementByClassName("button-class").setAttribute("rel", "abc1"); document.getElementByClassName("button-class").setAttribute("rel", "abc2"); document.getElementByClassName("button-class").setAttribute("rel", "abc3"); document.getElementByClassName("button-class").setAttribute("rel", "abc4"); </script> <?php } } Edit: So I added the below JS function to the 'js' folder in my child theme. Lets call it vsr.js
document.addEventListener( 'DOMContentLoaded', function() { if (is_single ()) { document.getElementByClassName("button-class").setAttribute("rel", "abc1"); document.getElementByClassName("button-class").setAttribute("rel", "abc2"); document.getElementByClassName("button-class").setAttribute("rel", "abc3"); document.getElementByClassName("button-class").setAttribute("rel", "abc3"); } ); Then I added the below function in my child theme's functions.php file to enqueue the script.
function SRel() { wp_enqueue_script('sprel', get_stylesheet_directory_uri() . './js/vsr.js'); } add_action( 'wp_enqueue_scripts', 'SRel' );