5

I'm using the Advanced Custom Fields plugin and I have created some options pages with:

if (function_exists('acf_add_options_page')) {
    website_settings_init();
}

function website_settings_init() {
    $website_settings = acf_add_options_page(array(
        'page_title' => 'Website Settings',
        'menu_title' => 'Website Settings',
        'menu-slug' => 'website-settings',
        'post_id' => 'website-settings',
        'redirect' => false
    ));

    $form_settings = acf_add_options_sub_page(array(
        'page_title' => 'Form Settings',
        'menu_title' => 'Form Settings',
        'menu-slug' => 'form-settings',
        'post_id' => 'form-settings',
        'parent_slug' => $website_settings['menu_slug']
    ));

    $contact_details = acf_add_options_sub_page(array(
        'page_title' => 'Contact Details',
        'menu_title' => 'Contact Details',
        'menu-slug' => 'contact-details',
        'post_id' => 'contact-details',
        'parent_slug' => $website_settings['menu_slug']
    ));
}

I have a field group that is visible if Options Page = Form Settings.

One of these fields is 'thank_you_page' which returns a Page ID.

I read https://www.advancedcustomfields.com/resources/get-values-from-an-options-page/ which describes how you can get values from an options page field using get_field('field_name', 'option');

I have tried:

get_field('thank_you_page', 'option');

This returns null. I've tried both 'option' and 'options'. However, if I try:

get_field('thank_you_page', 'form-settings');

I get the saved Page ID. What am I misunderstanding/doing wrong? Is it because it is an options sub page?

I'm using Wordpress 4.9.8 and ACF Pro 5.7.7

Thanks

0

2 Answers 2

5

I just had the same problem. It's insanely poorly documented on ACF's pages (as in, not there at all).

The core of your problem is, that you have spaces in your options-page names. And when you access it, using the 'ID', then you have to use the 'page_title', but where all spaces are replaced by underscores.

Please note, that it's not all lowercase. It's written exactly as you've done it, but just replacing the spaces with underscores.


Working example:

In functions.php:

if( function_exists( 'acf_add_options_page' ) ){

  acf_add_options_page( array(
    'page_title' => 'Some settings',
    'menu_title' => 'Some settings',
    'menu_slug'  => 'some_settings',
    'icon_url'   => 'dashicons-admin-generic',
    'capability' => 'edit_posts',
    'redirect'   => true,
    'position'   => '7.4',
  ) );

  acf_add_options_sub_page( array(
    'page_title'  => 'Acf page with spaces',
    'menu_title'  => 'Acf page with spaces',
    'parent_slug' => 'acf_page_with_spaces',
  ) );

} // endif

Where you want to access the fields (like page.php or single.php).

<?php if( have_rows( 'Acf_page_with_spaces', 'option' ) ): ?>
  <?php
  while( have_rows( 'Acf_page_with_spaces', 'option' ) ):
  the_row();
    $foo = get_sub_field( 'foo' );
    $bar = get_sub_field( 'bar' );
    ?>

    <h1><?php echo $foo; ?></h1>
    <p><?php echo $bar; ?></p>

  <?php endwhile; ?>
<?php endif; ?>

There's something about this, that doesn't make any sense, - but that's how it works.


Extra note

This snippet helped me debugging this issue:

<?php
echo '<pre>';
print_r(acf_get_options_pages());
echo '</pre>';
?>

It prints all the registered options-pages.

1
  • What does the post_id key in the registration args actually do then…? Commented Aug 4, 2023 at 5:55
-1

Note that after struggling with a related issue for a long time, if you remove the post_id array parameter from the acf_add_options_page that will solve it by using default ACF options keys in the wp_options table. (You will need to resave your options on the admin edit options page for this to work, since the database keys will have changed.)

2
  • This isn't really a solution, as it ignores the multiple examples in the original post where they demonstrate that they do in fact want to use something other than the default. Commented Aug 4, 2023 at 5:51
  • @ChrisMontgomery I disagree. They are following the documentation, which isn't good, and if they remove that field and use the default ACF value each page will still have its own ID based on the title, which fulfills their need. Just because the ACF documentation is poor in this example doesn't mean this isn't a solution. You aren't required to pass post_id here and even in your comment above on Zeth's post you express that you don't understand what the post_id registration arg does. Commented Aug 11, 2023 at 20:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.