Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
add_action( 'wp_ajax_my_request', function () {
ob_start();
echo get_theme_mod( 'position', 'top' );
wp_send_json_success( ob_get_clean() );
wp_die();
} );
add_action( 'wp_enqueue_scripts', function() {
wp_enqueue_script(
'my-request',
get_theme_file_uri( 'request.js' ),
array( 'jquery' ),
null,
true
);
wp_localize_script(
'my-request',
'myArgs',
array(
'url' => admin_url( 'admin-ajax.php' ),
'action' => 'my_request',
)
);
} );
add_action( 'customize_register', function( $wp_customize ) {
$wp_customize->add_section( 'position', array(
'title' => 'Position',
'priority' => 1,
) );
$wp_customize->add_setting( 'position', array(
'default' => 'top',
) );
$wp_customize->add_control( 'position', array(
'label' => 'position',
'section' => 'position',
'type' => 'select',
'choices' => array(
'top' => 'Top',
'right' => 'Right',
'bottom' => 'Bottom',
'left' => 'Left',
),
) );
} );
(function( jQuery ) {
'use strict';
var data = {
action: myArgs.action,
}
function request() {
jQuery.post( myArgs.url, data, function( response ) {
console.log( response );
} );
}
document.addEventListener( 'DOMContentLoaded', function() {
request();
} );
})( jQuery );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment