Support

Account

Home Forums General Issues putting a snippet in the functions.php

Helping

putting a snippet in the functions.php

  • The following snippet to allow javascript with ACF needed to add in header.php before the </head> tag.
    <?php the_field(‘header_script'); ?>

    But I would like to add to functions.php instead of header.php.
    How can I add the snippet to the functions.php?

  • This should do the trick. Simply add it to your functions document. the \n is a line break to make your code look nicer when it spits it out.

    
    add_action('wp_enqueue_scripts', 'customJsFunction'); 
    function customJsFunction(){
    	global $post;
    	$script= get_field('header_script',$post->ID);
    	if($script){
    		echo '<script>'."\n".$script."\n".'</script>'."\n";
    	}
    }
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘putting a snippet in the functions.php’ is closed to new replies.