最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

customization - Customizer JS API get value of customizer field

matteradmin8PV0评论

I want to use a javascript plugin on my website that should be initialized with values set in the wordpress customizer.

Kirki::add_field( 'artist_theme_config', array(
  'type'        => 'select',
  'settings'    => 'gallery_lightgallery_transition',
  'label'       => __( 'Select the transition type of your lightgalleries', 'site-name' ),
  'section'     => 'artist_theme_gallery_style',
  'default'     => 'lg-slide',
  'priority'    => 10,
  'choices'     => array(
    'lg-slide' => esc_attr__( 'Slide', 'site-name' ),
    'lg-fade' => esc_attr__( 'Fade', 'site-name' ),
    'lg-zoom-in' => esc_attr__( 'Zoom In', 'site-name' ),
  ),
) );

My goal is to run the initialization logic with the options set in the customizer. Therfore I need to get the value of the setting in a custom javascript. I found that one way (is it correct?) to get the value is:

wp.customize.instance( 'gallery_lightgallery_transition' ).get();

However that just works if the customizer is open. What is the correct way to get the value of a customizer field in javascript? Do I need to enqueue the script with the correct dependency, like in

wp_enqueue_script('sage/customizer_controls.js', asset_path('scripts/customizer_controls.js'), [ 'customize-controls' ], null, true);

? I do not want to put the logic in the customizer_controls.js however, but in a file called plugins.js. There is a long discussion on how to pass variables from php to javascript in wordpress, however there should be a way with the new customizer api I guess, just can't find any examples or documentation.

I want to use a javascript plugin on my website that should be initialized with values set in the wordpress customizer.

Kirki::add_field( 'artist_theme_config', array(
  'type'        => 'select',
  'settings'    => 'gallery_lightgallery_transition',
  'label'       => __( 'Select the transition type of your lightgalleries', 'site-name' ),
  'section'     => 'artist_theme_gallery_style',
  'default'     => 'lg-slide',
  'priority'    => 10,
  'choices'     => array(
    'lg-slide' => esc_attr__( 'Slide', 'site-name' ),
    'lg-fade' => esc_attr__( 'Fade', 'site-name' ),
    'lg-zoom-in' => esc_attr__( 'Zoom In', 'site-name' ),
  ),
) );

My goal is to run the initialization logic with the options set in the customizer. Therfore I need to get the value of the setting in a custom javascript. I found that one way (is it correct?) to get the value is:

wp.customize.instance( 'gallery_lightgallery_transition' ).get();

However that just works if the customizer is open. What is the correct way to get the value of a customizer field in javascript? Do I need to enqueue the script with the correct dependency, like in

wp_enqueue_script('sage/customizer_controls.js', asset_path('scripts/customizer_controls.js'), [ 'customize-controls' ], null, true);

? I do not want to put the logic in the customizer_controls.js however, but in a file called plugins.js. There is a long discussion on how to pass variables from php to javascript in wordpress, however there should be a way with the new customizer api I guess, just can't find any examples or documentation.

Share Improve this question edited Mar 9, 2019 at 15:03 user9 asked Mar 9, 2019 at 13:16 user9user9 1644 silver badges17 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

You need to do it the same way you'd get options, shortcodes, or any other WordPress value into JavaScript: You need to get them with PHP and pass them to the script with wp_localize_script():

wp_enqueue_script( 'my-script', asset_path( 'scripts/plugins.js' ), [], null, true );
wp_localize_script(
    'my-script',
    'myScript',
    [
        'galleryTransition' => get_theme_option( 'gallery_lightgallery_transition' ),
    ]
);

Just change my-script and myScript to something specific to your project.

Now you can access the value in your script via myScript.galleryTransition.

Post a comment

comment list (0)

  1. No comments so far