Last active
August 19, 2020 13:40
-
-
Save askaaqib/4c1981e7c81cbeb60e9b122de2fa3dde to your computer and use it in GitHub Desktop.
WordPress AJAX Function Hook
This file contains 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 | |
/****** CUSTOM POST TYPE = 'SERVICES' HOOKS *******/ | |
add_action( 'wp_ajax_get_client_timezone_info', __NAMESPACE__ . '\\getTZInfo' ); | |
add_action( 'wp_ajax_nopriv_get_client_timezone_info', __NAMESPACE__ . '\\getTZInfo' ); | |
add_filter( 'the_content', __NAMESPACE__ . '\\replace_hello_with_good_morning' ); | |
function replace_hello_with_good_morning($content){ | |
session_start(); | |
if(get_post_type()=='services'){ | |
echo " | |
<script> | |
var timezone_offset_minutes = new Date().getTimezoneOffset(); | |
timezone_offset_minutes = timezone_offset_minutes == 0 ? 0 : -timezone_offset_minutes; | |
var data = { | |
'action': 'get_client_timezone_info', | |
'tom_inf': timezone_offset_minutes | |
}; | |
var ajaxurl = '".admin_url('admin-ajax.php')."'; | |
jQuery.post(ajaxurl, data, function(response) { | |
console.log(response); | |
}); | |
</script> "; | |
$utz_name = $_SESSION['utz_name']; | |
$utz_hour = $_SESSION['utz_hour']; | |
if($utz_hour <= 12){ | |
$content = str_ireplace("Hello", "Good Morning", $content); | |
} | |
return $content; | |
} | |
} | |
function getTZInfo(){ | |
session_start(); | |
global $wpdb; | |
$tom_inf = $_POST['tom_inf']; | |
$timezone_offset_minutes = $tom_inf; | |
// Convert minutes to seconds | |
$timezone_name = timezone_name_from_abbr("", $timezone_offset_minutes*60, false); | |
$_SESSION['utz_name'] = $timezone_name; | |
date_default_timezone_set($timezone_name); | |
// 24-hour format of an hour without leading zeros (0 through 23) | |
$Hour = date('G'); | |
$_SESSION['utz_hour'] = $Hour; | |
echo " ". date("d-m-Y H:i:s ")." "; | |
//echo $_SESSION['utz_name']. " | " . $timezone_name; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment