user1828780 February 2016

Wordpress Customizer API: access variables

I have the following code:

function color_scheme_customizer_register($wp_customize) {

  $wp_customize->add_section('front_page', array(
    'title'    => __('Front Page'),
    'priority' => 120,
  ));
  // MAIN IMAGE
  $wp_customize->add_setting('front_page_options[image_select]', array(
    'capability'        => 'edit_theme_options',
    'type'           => 'option',
  ));
  $wp_customize->add_control( new WP_Customize_Image_Control($wp_customize, 'image_select', array(
    'label'    => __('Main Image', 'themename'),
    'section'  => 'front_page',
    'settings' => 'front_page_options[image_select]',
  )));

  // FEATURE ONE
  $wp_customize->add_setting('front_page_options[feature_one_page]', array(
    'capability'     => 'edit_theme_options',
    'type'           => 'option',
  ));
  $wp_customize->add_control('feature_one_page', array(
    'label'      => __('Featured Page One'),
    'section'    => 'front_page',
    'settings'   => 'front_page_options[feature_one_page]',
    'type'           => 'dropdown-pages',
  ));
  $wp_customize->add_setting('front_page_options[feature_one_textarea]', array(
    'capability'     => 'edit_theme_options',
    'type'           => 'option',
  ));
  $wp_customize->add_control('feature_one_textarea', array(
    'label'      => __('Featured Page One Summary'),
    'section'    => 'front_page',
    'settings'   => 'front_page_options[feature_one_textarea]',
    'type'           => 'textarea',
  ));
  ...
}

I want to access the front_page_array variables, but all I can find is documentation on simply creating a new .css spreadsheet to make changes. Is there a way I can specifically access the variables like so:

<?php get_header(); ?>

<?php get_customizer_variables('front_page_options'); ?>

<?php get_footer(); ?>

Answers


Joe Conlin February 2016

To retrieve values that you have stored, you will either use get_theme_mod() or get_option(), depending on the "type" you set. If no type is given, it defaults to theme_mod. To retrieve a field, use something like:

get_option( 'front_page_options[image_select]', '' );

Alternatively, if you have multiple options, you can retrieve all of them in an array and then access them as needed.

$front_page_options = get_option( 'front_page_options', '' );
$image_select = $front_page_options['image_select'];

This is a very simple example but should give you an idea of how to access the values you are looking for.

Post Status

Asked in February 2016
Viewed 1,748 times
Voted 11
Answered 1 times

Search




Leave an answer