Skip to content

Instantly share code, notes, and snippets.

@CFranc111
Last active August 23, 2019 00:28
Show Gist options
  • Save CFranc111/d7e4c1c2b08baf7371f9da13c23dd69b to your computer and use it in GitHub Desktop.
Save CFranc111/d7e4c1c2b08baf7371f9da13c23dd69b to your computer and use it in GitHub Desktop.
Wordpress jQuery cookie example
function custom_main_scripts() {
// Enqueue plugin here, if needed, including array('jquery') as the 3rd argument as a dependency (see below)
// Enqueue custom javascript file
wp_register_script( 'my-scripts', get_stylesheet_directory_uri() . '/myfile.js', array('jquery'), filemtime( get_template_directory() . '/myfile.js' ), true );
}
add_action( 'wp_enqueue_scripts', 'custom_main_scripts' );
( function( $ ) {
$(document).ready( function() {
if (!!$.cookie('textfield1')) {
// existing cookie found - get cookie
$(".textfield1").val( $.cookie("textfield1") );
} else {
// no cookie found - set cookie
$(".textfield1").change(function() {
$.cookie("textfield1", $(".textfield1").val(), {expires: 7});
});
}
});
} )( jQuery );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment