I'm trying to get user time zone with a script from JS. The result of this JS is passing successfully to the backend, but as I know, when the JS result have passed to the backend the shortcode is already loaded. So, I'm looking for a solution how can I get the user time zone and get this result in a shortcode. Thanks to this thread (Get returned variable from a function to add_shortcode function), I have this:

add_action( 'wp_ajax_my_action', 'my_action_callback' );
add_action( 'wp_ajax_nopriv_my_action', 'my_action_callback' );
add_action( 'wp_enqueue_scripts', 'theme_name_scripts' );
add_shortcode('tagtest', 'timezone');

function theme_name_scripts() {
    wp_enqueue_script( 'script-name', plugins_url( '/assets/js/timezone.js', __FILE__ ), array('jquery'), '1.0.0', true );
    wp_localize_script( 'script-name', 'MyAjax', array(
        'ajaxurl' => admin_url( 'admin-ajax.php' ),
        'security' => wp_create_nonce( 'my-special-string' ),

function my_action_callback() {
    $whatever = strval( $_POST['whatever'] );
    echo wpse_301294_whatever($whatever);

function wpse_301294_whatever($whatever) {
    return $whatever;

function timezone(){
    $tz = wpse_301294_whatever(); // here I'd need the $whatever parameter but how can I get if the shortcode has already rendered?
    $timestamp = time();
    $dt = new DateTime("now", new DateTimeZone($tz));
    $hour = $dt->format('G');
    // echo '<div class="timezone">' . $tz . '</div>';
    return '<div class="timezone">' . $hour . '</div>';;

If you could guide me with some code would be very nice, but I'd like also understand the workflow that I should follow because I don't understand if how could this be possible if when the JS pass to the backend, the shortcode is already rendered.

Thanks in advance.

  • 1
    with this first question and this second question this is the third time that you ask how to do that and we will answer a third time that it's not possible. you need to explain what you want to do. if e.g. the aim is to show a date with the timezone of the reader, you have to get the timezone in a first step, save it in a second step and on the last step you can use.
    – mmm
    Apr 20, 2018 at 20:21
  • @mmmThanks. Actually, I made that question but unfortunately, nobody has given a response. That's why I refer to other thread in this question that solves in part my issue.
    – dfrojas
    Apr 20, 2018 at 20:21
  • @mmm Yes, sorry about that. This is my third question but the reason is I have made the bad question, this time I have explained my real issue. I thought that jus passing the object from JS have solved but I was wrong.
    – dfrojas
    Apr 20, 2018 at 20:24
  • my_action_callback is where you render your Shortcode. The results get added to the page by your ajax script that called my_action_callback.
    – Milo
    Apr 20, 2018 at 20:52


Browse other questions tagged or ask your own question.