Support

Account

Home Forums Add-ons Options Page Save Options (default values) on theme activation (LITE mode)

Solved

Save Options (default values) on theme activation (LITE mode)

  • Hello!

    I run ACF in LITE mode as I’m developing a premium theme. (I have mentioned this in the old forum earlier.)

    I use the options add-on for Theme Settings.

    Everything works great, except that when I activate the theme the first time the Theme options default values have not been saved. Obviously this breaks the theme and disrupts the user experience for the one who uses the theme. The only way to get those values saved is to click save on all the options tab that I have set up… Something a theme user does not expect.

    Elliot, what is your recommended approach to get those values saved upon theme activation (maybe even on theme preview)? Any ideas?

  • Current approach:
    (in functions.php)

    // Update option keys on theme activation
    function updateoptionkeys() {
      // BASE
      if (!get_field("field_515d9fcb5bee9", "options")) {update_field("field_515d9fcb5bee9", 0, "options");}
      if (!get_field("field_515d933e4347c", "options")) {update_field("field_515d933e4347c", 50, "options");}
      //...etc
    }
    
    // When switching Theme Hook
    function myactivationfunction($oldname, $oldtheme=false) {
      // IF Theme options is not set, do set/update them when activating theme
      updateoptionkeys();
    }
    add_action("after_switch_theme", "myactivationfunction");

    For some reason this approach doesn’t seem to work with the latest v. of WP & ACF…

  • Update. I have now improved this to be an associate array instead:

    function updateoptionkeys() {
      $fields = array (
        "field_515d9fcb5bee9"=>0,
        "field_515d933e4347c"=>50,
        // etc...
      ); 
      
      foreach ($fields as $key=>$value) { 
        update_field($key, $value, "options");
      }
    }
    
    function myactivationfunction($oldname, $oldtheme=false) {
      updateoptionkeys();
    }
    add_action("after_switch_theme", "myactivationfunction");

    But this means it will revert back to default values everytime the user switch on/off the theme…

    Doesn’t get_field() work with field keyvalues instead of id/name anymore?

  • Hi @emilolsson

    Your second comment contains the code I would use (if statement), however, I would also use the loop you have developed in your 3rd comment.

    It should work.

    Perhaps you tested the code when you had a value in the options table? This would cause the if statement to do it’s job and fail the update.

    Does that help?

    Can you debug the function? ie: print_r and die? This will show you where it gets up to / what the issue is – if any

  • Thanks for the input Elliot.
    I ended up doing it like this:

    function updateoptionkeys() {
      $fields = array (
        "field_515d9fcb5bee9"=>1,
        "field_515d933e4347c"=>'value',
        // etc etc.
      ); 
    
      foreach ($fields as $key=>$value) { 
        $existence = get_field_object($key, "options", false);
        
        if (!get_field($existence[name], "options")) {
          update_field($key, $value, "options");
        }
      } 
    }
    
    function myactivationfunction($oldname, $oldtheme=false) {
      updateoptionkeys();
    }
    add_action("after_switch_theme", "myactivationfunction");
  • Hi,

    How can this be rewritten for:
    – ACF5 option fields
    – using the field name instead of the field key

    Looking forward to the reply.

    Kind regards,
    Filip

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

The topic ‘Save Options (default values) on theme activation (LITE mode)’ is closed to new replies.