Support

Account

Home Forums ACF PRO Custom Field in Head – Javascript Link

Solving

Custom Field in Head – Javascript Link

  • What is the best way for me to insert a script href link in the header?

    Here’s what I’ve got setup and tried to no avail…

    – I have an options page with a text field where the user can insert a script tage such as this:
    <script src="//nexus.ensighten.com/Bootstrap.js"></script>

    – I’ve tried just calling the field directly in the head like this. Nothing shows up in this scenario:
    <?php the_field('ensighten_tag', 'option'); ?>

    – I’ve also tried calling it using acf_admin_head like this:

    <?php function my_acf_admin_head() {
    
        <?php the_field('ensighten_tag', 'option'); ?>
    
    <?php } add_action('wp_head', 'my_acf_admin_head'); ?>

    I’m certainly doing something very wrong. This is a final launch piece on a NOW deadline. Would be so grateful for some direction as soon as anyone is able.

    Thanks a ton!

  • You need to supply the correct $post_id when calling the_field(). The problem is that you are calling the function outside of “The Loop” so ACF does not know what post to use.

    An example for a post

    
    $object = get_queried_object();
    $post_id = $object->ID;
    the_field('ensighten_tag', $post_id);
    

    The above will only work on single post pages and will cause errors if called on an archive page.

  • Thanks for the response!

    But I want this to appear on all pages so it needs to populate somewhere globally.

  • NM, I misread your post.

    You can use your code in any template because it’s an options field.

    if you have the field on an options page and you put this in between <head> and </head> then the content of the field should be output

    
    <?php the_field('ensighten_tag', 'option'); ?>
    
  • That’s what I thought. I’m still not getting anything. I think it might be that I’m running multisite.

    So…

    I have 1 site that I’ve created the ensighten tag field for and placed it on an options page for that site. Its called in the global header from there. Its NOT showing up in the site that I have the options page setup for and NOT on any of the child sites.

    Could that be the source of the problem?

    I’ve double checked the slug name again and again. Its solid.

    <?php the_field ('ensighten_tag', 'option'); ?>

  • Since the field is defined on the “Admin Site” you will need to use switch_to_blog(), to switch to that site before getting the value and then restore_current_blog() https://codex.wordpress.org/Function_Reference/switch_to_blog

Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘Custom Field in Head – Javascript Link’ is closed to new replies.