Support

Account

Home Forums Add-ons Options Page using options page fields in functions.php

Solved

using options page fields in functions.php

  • I’m trying to use an options page to do some conditional loading of some php files using the the true/false field. For example:

    
    $include_albums = get_field('include_albums', 'option');
    if($include_albums == 1) {
    	include_once('lib/cpt_albums.php');
    }
    

    But it’s not working. Is this possible?

  • Hi @thesrpr

    Currently, ACF will work only AFTER the init action. Before this (when the functions.php file is run), the get_field function will not work.

    In the next version of ACF, this will be fixed! Until then, you need to move your logic into a function which is run on or after the init action.

    Thanks
    E

  • This reply has been marked as private.
  • Hey Elliot. I tried that and it still didn’t work. I pulled my include out of the function just to test and it works.

    
    function m1_core_feature_selection() {
    	$include_albums = get_field('include_albums', 'option');
    	if($include_albums == 1) {
    		include_once('lib/cpt_albums.php');
    	}
    }
    add_action('wp_loaded', 'm1_core_feature_selection');
    

    I also tried ‘init’ versus ‘wp_loaded’ and that didn’t work either

  • Hi @thesrpr

    is the wp_loaded action fired after or before the ‘init’ action?

  • after. I tried hooking into “init” as well and that didn’t work either.

  • Strange.. can you try ‘init’ again, but add a priority above 10. Try 20?

    Thanks
    E

  • 
    function m1_core_feature_selection() {
    	$include_albums = get_field('include_albums', 'option');
    	if($include_albums == 1) {
    		include_once('lib/cpt_albums.php');
    	}
    }
    add_action('init', 'm1_core_feature_selection', 20);
    

    NO dice.

    I’ve also included a couple of screen shots of my admin area
    http://cl.ly/07142Z0r282F

  • Hey Elliot. I wrote a function to go get some posts and their acf fields and the get_field worked just fine but for some reason it appears to not work with option pages

  • Hi @thesrpr

    Thanks for the info. I’m not sure why it isn’t working with the options page.

    Are you sure it isn’t returning the value? Can you debug the value (as in use var_dump) to test the returned value?

    Cheers
    E

  • Sorry to jump in on this thread; I am also trying to do the same with no luck.
    Will be keeping an eye on this thread – hopefully we can solve the problem.

    Thanks

  • after_setup_theme cant get option values too..
    I am using wp_head for now, but its not a good solution for every option.

    Interesting section is, it works in my local test but it wont work in my server @ after_setup_theme. Maybe its problem with latest version of php? Using 5.4.19 on my server.

  • hey @unsalkorkmaz can you post some code that you have working w/ an options page in a functions.php file? I tried wp_head and that isn’t working for me either.

  • Just wanted to add another update. Here’s my function:`function m1_core_feature_selection() {
    $include_albums = get_field('include_albums', 'option');
    if ( $include_albums === true ){
    include_once('lib/cpt_albums.php' );
    }
    }
    add_action('after_setup_theme', 'm1_core_feature_selection');
    `
    When I do a var_dump($include_albums) I get bool(true). I can uncheck the box and see the change to bool(false). If I remove the if ( $include_albums === true ) piece from the function it works.

  • Hi there! I think I’m having this problem too. I’m trying to set up a select field that’s dynamically populated from a repeater field on the Options page, pretty much exactly like this example:

    http://www.advancedcustomfields.com/resources/tutorials/dynamically-populate-a-select-fields-choices/#example-2

    Manually typing in select values, like so, works:

    $field['choices'][ 'test' ] = 'Testing 1';
    $field['choices'][ 'test2' ] = 'Testing 2';

    But attempting to reference the option field using get_field() doesn’t work.

    Edit: I originally tried calling add_filter() in functions.php, but, after some reading some posts here, then tried calling add_filter() inside an action on admin_init. I think the above code (manual choices) worked, but the option field values did not.

  • Hi @mazil

    Can you please create a new thread with more detail about the issue and also the full code you are using to modify the select field’s value?

    Thanks
    E

  • I think to get get_field and get_sub_field functions in functions.php is impossible now. But you may use get_field_objects($post_id) function to use in functions.php. Just print returned array and use (parse it) as you like!

    By the way you can use get_post_meta and get_user_meta functions against get_field, but I couldn’t use them to get sub_field of repeater in functions.php file.

    Good luck!

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

The topic ‘using options page fields in functions.php’ is closed to new replies.