Created
August 27, 2022 16:09
-
-
Save kimcoleman/428bae3bcdb4e0ad8baff43a51a69b06 to your computer and use it in GitHub Desktop.
Script to load a datepicker that targets the custom user field with class name 'my-datepicker'
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Load datepicker script for specific pages. | |
*/ | |
function load_my_jquery_datepicker_script_for_pmpro() { | |
global $pmpro_pages; | |
if ( is_page( array( $pmpro_pages['checkout'], $pmpro_pages['member_profile_edit'] ) ) || | |
defined( 'IS_PROFILE_PAGE' ) || | |
( is_admin() && | |
isset( $_REQUEST['page'] ) && | |
'pmpro-addmember' === $_REQUEST['page'] | |
&& function_exists( 'pmpro_addmember' ) ) | |
) { | |
?> | |
<script> | |
jQuery(document).ready(function($) { | |
$( '.my-datepicker' ).datepicker( { | |
dateFormat: 'yy-mm-dd', | |
showButtonPanel: true, | |
changeMonth: true, | |
changeYear: true, | |
yearRange: "1920:2040", // set your start and end years for the datepicker here | |
}); | |
}); | |
</script> | |
<?php if ( ! is_admin() ) { ?> | |
<style> | |
/* Customize the style and layout for the Jquery datepicker here */ | |
#ui-datepicker-div { | |
-webkit-box-shadow: 10px 10px 25px 0px rgba(0,0,0,0.35); | |
-moz-box-shadow: 10px 10px 25px 0px rgba(0,0,0,0.35); | |
box-shadow: 10px 10px 25px 0px rgba(0,0,0,0.35); | |
} | |
</style> | |
<?php | |
} | |
} | |
} | |
add_action( 'wp_head', 'load_my_jquery_datepicker_script_for_pmpro' ); | |
add_action( 'admin_head', 'load_my_jquery_datepicker_script_for_pmpro' ); | |
function my_pmpro_datepicker_enqueue_scripts() { | |
global $pmpro_pages; | |
if ( is_page( array( $pmpro_pages['checkout'], $pmpro_pages['member_profile_edit'] ) ) || | |
defined( 'IS_PROFILE_PAGE' ) || | |
( is_admin() && | |
isset( $_REQUEST['page'] ) && | |
'pmpro-addmember' === $_REQUEST['page'] | |
&& function_exists( 'pmpro_addmember' ) ) | |
) { | |
// tell WordPress to load the JQuery Datepicker, the ui-core depency will automatically be loaded, | |
wp_enqueue_script( 'jquery-ui-datepicker' ); | |
// load CSS to style datepicker. | |
wp_enqueue_style( 'jquery-ui-css', '//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css' ); | |
} | |
} | |
add_action( 'wp_enqueue_scripts', 'my_pmpro_datepicker_enqueue_scripts' ); | |
add_action( 'admin_enqueue_scripts', 'my_pmpro_datepicker_enqueue_scripts' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment