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(
$wp_customize,
'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() { ?>
<script>
jQuery( document ).on( 'click', '.ajaxbutton', function() {
jQuery.ajax({
url : '/wordpress/wp-admin/admin-ajax.php',
type : 'post',
data : {
action : 'my_action',
},
});
})
</script>
<button type="button" class="ajaxbutton">CLICK ME</button>
<?php
}
}
}
?>
////////////////////////////////////////////////////////
inside my functions.php
////////////////////////////////////////////////////////
add_action('wp_ajax_my_action','ajaxfuntion');
add_action('wp_ajax_nopriv_my_action','ajaxfunction');
function ajaxfunction(){
//my function//
die();
}