Support

Account

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

Solved

using options page fields in functions.php

    • thesrpr

    • September 6, 2013 at 6:26 am

    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?

    • Elliot

    • September 6, 2013 at 10:25 am

    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

    • thesrpr

    • September 6, 2013 at 3:14 pm
    This reply has been marked as private.
    • thesrpr

    • September 7, 2013 at 1:05 am

    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

    • Elliot

    • September 7, 2013 at 1:39 pm

    Hi @thesrpr

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

    • thesrpr

    • September 8, 2013 at 1:15 am

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

    • Elliot

    • September 8, 2013 at 6:05 pm

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

    Thanks
    E

    • thesrpr

    • September 9, 2013 at 4:22 am
    
    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

    • thesrpr

    • September 14, 2013 at 12:34 am

    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

    • Elliot

    • September 14, 2013 at 1:54 pm

    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.

    • thesrpr

    • October 1, 2013 at 7:33 am

    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.

    • thesrpr

    • October 1, 2013 at 10:05 am

    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.

    • mazil

    • February 10, 2014 at 5:18 pm

    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.

    • Elliot

    • February 11, 2014 at 9:46 am

    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

    • Gamelot

    • March 20, 2015 at 11:36 pm

    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)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.