# Module : Settings - Edit

This module uses CMB2 fields to build a "General Settings" page, in order to regroup common website settings.
It includes by default 2 sections :

  • Contact (Postal address, E-mail, Phone),
  • Social (Social network urls)

Shortcode options

You can define options to be displayed easily with a shortcode.

In the field declaration, you can add 'show_shortcode_btn' => true, ; the field will have a button to copy shortcode in the General Settings page, or you will be able to select it in the editor with the special button {;}

// E-mail :
array(
  'id'   => 'email',
  'name' => __('E-mail:', 'starter-theme'),
  'type' => 'text_email',
  'show_shortcode_btn' => true,
),

Multilingual option

You can define multilingual options.
(Only works with Polylang - Never checked compatibility with WPML)

In the field declaration, you can add 'is_multilingual' => true,
In the General Setting, the field value will be only registered for the current language. (If you are on view "All the languages", this will take the default language)
So don't forget to switch language, to fill-in the option(s) in all languages.

// E-mail :
array(
  'id'   => 'test',
  'name' => __('Test:', 'starter-theme'),
  'type' => 'text',
  'is_multilingual' => true,
),

Available Filters

Some filters are available to adjust this module :

  • FILTER_ADD_SECTION : Used to add a group of fields.
    Usage example :

    
    add_filter( Hooks::FILTER_ADD_SECTION, [$this, 'define_social_settings'], 12, 1); // Add Social fields
    /**
       * Define Social Settings
       * @param  array $settings  theme global settings
       * @return array            contains cmb2_box & fields about social.
       */
      public function define_social_settings($settings)
      {
    
        $settings[] = array(
          'id'              => 'social',
          'title'           => esc_html__('Social', 'starter-theme'),
          'fields'          => array(
            // Big title :
            array(
              'id'       => 'social_global_title',
              'name'     => __('Social Networks settings', 'starter-theme'),
              'type'     => 'title',
              'classes'  => 'title-big'
            ),
            // Facebook :
            array(
              'id'   => 'facebook',
              'name' => __('Facebook:', 'starter-theme'),
              'type' => 'text_url',
            ),
            ...
          )
        );
        return $settings;
      }
    
  • FILTER_ADD_FIELD : Used to add a single field to an existing group of fields.
    Usage example :

    
    add_filter( Hooks::FILTER_ADD_FIELD, [$this, 'example_add_single_field_in_section'], 99, 2 );
    /**
       * Example : how to add single field on a specific setting section
       * (You can also remove, reorder the field array)
       *
       * @param array   $fields   current fields in box
       * @param string  $box_id   cmb2 box identifier
       */
      public function example_add_single_field_in_section($fields, $box_id)
      {
        if ($box_id == 'social') {
          $fields[] = array(
            'id'   => 'google',
            'name' => __('Google +:', 'starter-theme'),
            'type' => 'text_url',
          );
        }
        return $fields;
      }
    
  • FILTER_SANITIZE_BEFORE: Allow to edit the fields before sanitize them for usage in options-page.
    Usage example :

    
    add_filter( \Magina\StarterTheme\Settings\Hooks::FILTER_SANITIZE_BEFORE, [$this, 'example'], 10, 1 );
    
  • FILTER_SANITIZE_AFTER: Allow to edit the complete CMB2 box declaration before new_cmb2_box.
    Usage example :

    
    add_filter( \Magina\StarterTheme\Settings\Hooks::FILTER_SANITIZE_AFTER, [$this, 'example'], 10, 1 );
    
  • FILTER_ADMIN_PAGE: By default, the options-page will be displayed on Dashboard Admin-menu. You can adjust the location of this page with this filter.
    Usage example :

    
    add_filter( \Magina\StarterTheme\Settings\Hooks::FILTER_ADMIN_PAGE, function(){ return 'options-general.php'; } );
    
  • FILTER_SHORTCODE: Used to filter the value returned by a shortcode, for example to directly add links to e-mail or phone fields.
    Usage example :

    
    add_filter( Hooks::FILTER_SHORTCODE, array($this, 'add_contact_links_in_shortcodes'), 10, 3);
    /**
       * Filter Shortcode Output.
       * (Add Link mailto/tel to email/phone fields)
       *
       * @param string  $value    default-value
       * @param string  $section  field's section
       * @param string  $field    field's id
       * @return string
       */
      public function add_contact_links_in_shortcodes($value, $section, $field)
      {
    
        if ($section == 'contact') {
    
          if ($field == 'email') {
            $value = '' . $value . '';
          } elseif ($field == 'phone') {
            $value = '' . $value . '';
          }
        }
    
        return $value;
      }