Skip to content

Instantly share code, notes, and snippets.

@michaelnie
Last active October 9, 2017 20:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save michaelnie/20e0962238da9cd82bffa75b491abf0a to your computer and use it in GitHub Desktop.
Save michaelnie/20e0962238da9cd82bffa75b491abf0a to your computer and use it in GitHub Desktop.
<?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