Support

Account

Home Forums ACF PRO Problems with Sub Options pages

Solved

Problems with Sub Options pages

  • I have created some sub options pages inside my custom post types and I am showing one field group on each of them.

    This is my code in functions.php

    function add_default_menus() {
    
    	if(function_exists('acf_add_options_page')) { 
    
    		acf_add_options_page();
    
    	}
    
    	$args = array(
    	   'public'   => true
    	);
    
    	$output = 'objects';
    
    	$operator = 'and';
    
    	$post_types = get_post_types( $args, $output, $operator ); 
    
    	foreach ( $post_types  as $post_type ) {
    
    		if ($post_type->name != "post" && $post_type->name != "attachment") {
    
    			acf_add_options_sub_page(array(
    				'title' => $post_type->label . ' Defaults',
    			    'page_title' => $post_type->label . ' Defaults',
    			    'menu_title' => $post_type->label . ' Defaults',
    			    'menu_slug' => 'page-defaults-' . $post_type->name,
    			    'slug' => 'page-defaults-' . $post_type->name,
    			    'parent' => 'edit.php?post_type=' . $post_type->name,
    			    'capability' => 'edit_posts',
    			    'position' => false,
    			    'icon_url' => false
    			));
    
    		}
    
        }
    
    }
    
    add_action( 'init', 'add_default_menus', 999 );

    The problem is that, although the field group shows on all of them new sub pages which is great. If I edit one of the fields and save it, it then shows on all the other sub pages?

    I thought that maybe it had something to do with the file on the ‘parent’ being all set to ‘edit.php…’ and that maybe ACF wasn’t counting the post_type variable when saving?

  • Options pages save the values to the wp_options table using your field name. In essence you’ve created a single option field that all of your options pages are using.

    When working with options pages your field names need to be unique for each options page. You will need to create a separate field group for each of your options pages and prefix the field name with something unique for each.

  • Thanks for the quick reply.

    The problem is, I will need to be updating and changing the one field group on a regular basis and it will soon get messy if I have to replicate the same changes 6 times (one for each custom post type). I put a sub option page in the custom post type’s menu in order to provide a place to set the options as a fall back if the same settings weren’t set on the custom post itself.

    Why can’t it save to the wp_options table using my slug i’m sending it. That is unique…?@!

    Is there a way to extend the core functionality do get what I need?

  • So you have an options page for each post type that basically holds default values for when someone does not add a value to a post I’m guessing. I do something similar. Unfortunately, ACF does not alter the field names based on the options page they are added to. Each field needs a unique key and field name.

    You’re going to be modifying the field group and not just the values on a regular basis?

    Maybe there is a better solution, can I get more detail on what it is you’re trying to achieve?

  • What I ended up doing was creating a new “Page Defaults” custom post type. If a the template can’t find a value on it’s own page then it checks for a slug which matches it’s post type and get’s the ID. Then checks to see if the field exists on that page.

    So in functions.php:

    function the_slug_exists($post_name, $post_type = "page") {
    
        global $wpdb;
    
        if($slug_id = $wpdb->get_var("SELECT ID FROM wp_posts WHERE post_name = '" . $post_name . "' AND post_type = '" . $post_type . "'")) {
    
            return $slug_id;
    
        } else {
    
            return false;
    
        }
    
    }

    Then in my template:

    $pt = get_post_type();
    $default_id = the_slug_exists($pt, 'default_layouts');
    
    if ( have_rows('body_content') ) {
    
    	$body_id = null;
    
    } else {
    
    	$body_id = $default_id;
    
    }
    
    if( have_rows('body_content', $body_id ) ) {
    
    	while ( have_rows('body_content', $body_id ) ) {
    
    //As normal...

    It’s working, but it’s less than graceful. I’ve effectively made an options page with sub pages that works how I needed it.

    Elliot… can something like this be built in?

  • If you’re interested, I’ve finished adding field group duplicators to my options page plugin. I would be interested in any feedback or bug reports before I merge it with the master repo. The duplicator basically does all the work of renaming all the fields and other necessary changes so that you can use the same field group multiple times and all of them will be automatically updated if there are changes in your master copy of the group.

    https://github.com/Hube2/acf-options-page-adder/tree/Fieldset-Duplicator

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

The topic ‘Problems with Sub Options pages’ is closed to new replies.