I have added a new custom button to the wordpress customizer, my goal is to create a button inside the the customizer which then fires a specific function in another .php file. I have two problems, one is that it will only work when my function i want to fire is inside either functions.php or the custom customizer.php. My second problem is that it only works when it is the active theme. I am quite new to ajax and the customizer so thanks for any help.

  inside my cystom customizer.php
  $wp_customize->add_setting( 'ajax_button_settings', array(
  ) );

  $wp_customize->add_control( new ajax_button(
      'pagebuilder', array(
      'section' => 'section',
      'settings' => 'ajax_button_settings',
  ) ) );
 if ( class_exists( 'WP_Customize_Control' ) ) {

 class ajax_button extends WP_Customize_Control {

 public function render_content() { ?>

 jQuery( document ).on( 'click', '.ajaxbutton', function() {

    url : '/wordpress/wp-admin/admin-ajax.php',
    type : 'post',
    data : {
        action : 'my_action',


 <button type="button" class="ajaxbutton">CLICK ME</button>

 inside my functions.php

    function ajaxfunction(){
 //my function//


1 Answer 1


Having any AJAX inside of the customizer defeats the reason for using the customizer in the first place. The idea of the customizer is that it fully controls the state of the settings while it is active, and having any setting being updated without going via the customizer logic can actually cause a mismatch between what it is displayed to the user and what the settings will actually do when saved.

Maybe it can be ok if you use ajax for "read only" kind of way, but right now I find it hard to imagine a scenario in which it can be useful, and can not be implemented just by adding the values as parameters to the code implementing the control.

  • For this project the button will create a new page with custom content by the user. It works just fine using the customizer check box but using a button looks better. There isnt a customizer control setting using a button i think. I can use ajax to set a theme mod var and it will work when the page is reloaded but isnt pretty.
    – Dostomatic
    Commented May 30, 2017 at 12:24
  • nothing that happens in the customizer "sidebar" should change the DB before a save was clicked. If you need a page to exists because it might be required during the session, then find a way to create a virtual one when the customizer starts, and delete it if it is not needed when the session ends Commented May 30, 2017 at 12:35
  • thanks for all the insightful commenst, I really appreciate it. my options are thus as follows, study the static page-->add page customizer setting or do a simple thing and add a custom menu on the main page outside the customizer where i can do what i want, which only shows when using the customizer?
    – Dostomatic
    Commented May 30, 2017 at 12:40
  • I know that there was some thinking done about creating virtual pages as part of the menu management. I am not sure at what stage is it, maybe better to drop by the #customize channel on slack and ask weston directly. Commented May 30, 2017 at 14:12
  • The JS API call for adding a post/page stub inside the customized state is wp.customize.Menus.insertAutoDraftPost( postData ) Commented May 30, 2017 at 16:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.