Support

Account

Home Forums ACF PRO Certain fields not working when imported with PHP

Solved

Certain fields not working when imported with PHP

  • I am trying to create fields that are imported into a theme using PHP, but when I do this there are certain fields which don’t work properly.

    When they are saved, they lose their value and look like an empty <input> field with no label.

    Here is the code (snippet from the full PHP export) that just does not work:

    		array (
    			'key' => 'field_53bbffb33e384',
    			'label' => 'Comments',
    			'name' => 'comments',
    			'prefix' => '',
    			'type' => 'true_false',
    			'instructions' => 'If you untick here, comments will be disabled throughout the whole site.',
    			'required' => 0,
    			'conditional_logic' => 0,
    			'message' => 'Enable Comments?',
    			'default_value' => 0,
    		),
    

    The strange thing is if I rename it to something like “comments_on” then it works. This also happens to some other fields, and renaming them fixes them, but there seems to be no common reason.

    I’m trying to add it to an Options page.

    Regards
    Shaun

  • Actually, it doesn’t seem to matter what they are called. The fields are breaking no matter what I call them. Must be something to do with how many there are or something?

    Help! :-/

  • I’ve looked into this more and the problem is nothing to do with the names.

    It’s if I attach functions to the options.

    For example, I have an option called “hide_tools” and then a function like so:

    	if(get_field('hide_tools', 'options')) {
    		
    		function remove_admin_menu_items() {
    			if(!current_user_can('activate_plugins')) {
    				$remove_menu_items = array(('Tools'));
    				global $menu;
    				end ($menu);
    				while (prev($menu)){
    					$item = explode(' ',$menu[key($menu)][0]);
    					if(in_array($item[0] != NULL?$item[0]:"" , $remove_menu_items)){
    					unset($menu[key($menu)]);}
    				}
    			}
    		}
    		add_action('admin_menu', 'remove_admin_menu_items');
    	
    	}
    

    As long as that function exists in my plugin, the “Hide Tools” checkbox doesn’t appear.

    This must mean ACF is choking on that function, but this only happens when it is imported using PHP.

    What can I do?

  • I’ve fixed this now, it was the order of the functions – the functions were above the code that created the options page.

  • Hi @shauny

    Thanks for the info. Glad you got it working!

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

The topic ‘Certain fields not working when imported with PHP’ is closed to new replies.