Support

Account

Home Forums Add-ons Options Page How to initiate Theme Setting on theme activate without having to click "Update"

Solved

How to initiate Theme Setting on theme activate without having to click "Update"

  • I have a theme settings page and I would like to have these settings to automatically use the default settings I have specified but it does not do this unless the user clicks on Theme Settings then clicking on Update.

    Is there a way to use these settings when the theme is activated for the first time without making the user click on Theme Settings -> Update?

  • There are two choices. What I would call easy and difficult.

    First the difficult:
    Register a theme activation hook https://codex.wordpress.org/Function_Reference/register_activation_hook. In your activation function insert the needed data into the database by way of update_field() http://www.advancedcustomfields.com/resources/update_field/

    Then the easy and the way that I prefer to do things:
    (Actually, I only use the get_post_meta() and get_option() WP functions in my themes so that they won’t crash a site should ACF get deactivated, but that’s another story)

    Set the default value in the theme file

    
    // with ACF, longhand
    $value = 'the default_value';
    $option = get_field('my_field_name', 'option');
    if ($option) {
      $value = $option.
    }
    
    // with get_option
    $value = get_option('options_'.'my_field_name', $default_value);
    
  • Ok, thank you John. I will try your “easier method” first.

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

The topic ‘How to initiate Theme Setting on theme activate without having to click "Update"’ is closed to new replies.