Fields in the Customizer : appear and disappear


Hello Thierry, hello everybody,

I want to add panels and sections to the customizer with the Beans fields API.

In order to test it, I have added in my functions.php the code snippet shown as an example in this page of the Beans documentation.

add_action( 'admin_init', 'my_function_name' );

function my_function_name() {

  $fields = array(
     array(
     'id' => 'field_id',
     'label' => 'Field label',
     'type' => 'text',
     'default' => ''
   ),
  );

  beans_register_wp_customize_options( $fields, 'section_id', array( 'title' => 'Controler title' ) );

}

This code effectively adds a new section in the customizer but this new section immediately disappear once the customizer is loaded. Very strange. I can see it for one or two second, and then, nothing.

Am I the only one to experience this issue ?

Thanks !

Mat


Hey Mat,

Change the action from admin_init to customize_register.

I have updated the docs too 🙂

Happy coding,


Thanks a lot Thierry,

It works now, the section appears in the customizer. Unfortunately, I have a new problem : the customizer doesn't refresh when I add/change something in the section that I have added in the customizer.

I have this in my functions.php :

add_action( 'customize_register', 'mytheme_add_section_customizer' );

function mytheme_add_section_customizer() {

  $fields = array(
     array(
     'id' => 'myid',
     'label' => 'Field label',
     'type' => 'textarea',
     'default' => 'test'
   ),
  );

  beans_register_wp_customize_options( $fields, 'section_id', array( 'title' => 'Presentation' ) );

}

And, in my page template, I have this :

<p><?php get_theme_mod( 'myid' );?></p>

But interacting with the customizer, as I said, changes nothing.

Do you see where is my mistake ?

Mathieu


Hey Matt,

Add 'transport' => 'refresh' to your field if you want the page to automatically refresh when you make a change. So your $fields looks like this:

$fields = array(
    array(
        'id' => 'myid',
        'label' => 'Field label',
        'type' => 'textarea',
        'default' => 'test',
        'transport' => 'refresh'
    ),
);

By default Beans set the transport to postMessage which is a mistake because WP default is refresh. I have made a note of that and it will be fixed and marked as Important since it will affect backwards compatibility.

Beans extends the WP core Customizer and make it more accessible. If you want to have a look at other transport methods, you can look at this section of the WP doc. Beans core uses that for the responsive preview settions for example.

Happy coding,


Hello Thierry,

Thanks a lot !

I have added the transport method and now the page refreshes in the customizer.

But the text that I add in the textarea, in the left panel of the customizer, doesn't appear in my website after the refresh...

Mathieu


My bad, it works with :

<?php echo get_theme_mod( 'myid' ); ?>

Thanks again Thierry, Mat

Write a reply

Login or register to write a reply, it's free!