Support

Account

Home Forums Gutenberg Get option page fields from block (not a AFC block)

Solved

Get option page fields from block (not a AFC block)

  • Is it possible to get ACF option page field values from the JS code for a custom block?

  • I do not use blocks, so this is just a guess and based on getting options pages and adding them to JS in a page.

    There are a couple of choices.

    If the script that needs values is “Enqueued” then you need to localize the script https://developer.wordpress.org/reference/functions/wp_localize_script/

    If the script is output into the page then you need to output the values to JS variables on the php side.

    
    <script>
      var some_varialble_name = '<?php the_field('some_field_name', 'options'); ?>';
    </script>
    

    Or some combination of these things.

    The only other choice is to have an AJAX request in your script that gets the option values.

  • Thank you for your suggestion.

    I actually used wp_add_inline_script() in the end.

    I thought maybe there was a clever way of getting the options value via wp.data.select() but I can’t find one.

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

You must be logged in to reply to this topic.