admin管理员组

文章数量:1434380

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 6 years ago.

Improve this question

I have a custom post type called X,

Post type X has some custom fields, one of these custom fields is date,

<input type="date" name="date_added" id="date_added" class="regular-text" value="<?php echo get_post_meta($post->ID,'date_added',true ); ?>">

When I want to fill this field, WordPress gives me a calendar (which is great),

I want to change this calendar's time zone (Persian calendar),

I would be thankful if anybody helps me.

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 6 years ago.

Improve this question

I have a custom post type called X,

Post type X has some custom fields, one of these custom fields is date,

<input type="date" name="date_added" id="date_added" class="regular-text" value="<?php echo get_post_meta($post->ID,'date_added',true ); ?>">

When I want to fill this field, WordPress gives me a calendar (which is great),

I want to change this calendar's time zone (Persian calendar),

I would be thankful if anybody helps me.

Share Improve this question edited Apr 6, 2019 at 12:04 butlerblog 5,1313 gold badges28 silver badges44 bronze badges asked Apr 6, 2019 at 6:53 hessam hosseinipourhessam hosseinipour 191 silver badge8 bronze badges 5
  • 1 What kind of custom field? How did you add that field? Do you use ACF or some other plugin? – Krzysiek Dróżdż Commented Apr 6, 2019 at 9:15
  • without plugin, i wrote it. – hessam hosseinipour Commented Apr 6, 2019 at 9:50
  • Well, you did not ;) And again... Could you show how do you add this CF? It's a little bit hard to guess how to change something, we have no knowledge about... ;) – Krzysiek Dróżdż Commented Apr 6, 2019 at 9:55
  • 2 WordPress does not add a calendar to date inputs. If a calendar is appearing then it’s either the one provided by the browser, or one you added yourself. Either way, it’s not really related to WordPress. – Jacob Peattie Commented Apr 6, 2019 at 10:06
  • you r right, its related to the browser, isn't there any way to use a persian clendar instead of this? – hessam hosseinipour Commented Apr 6, 2019 at 10:26
Add a comment  | 

2 Answers 2

Reset to default 2

The calendar you are referring to is not provided by WordPress. You are using a "date" input field type, which is an HTML5 input specification. The calendar is from your browser based on that.

If your question is about timezone offset for the input itself (so what displays with the input), then you should note that there is no timezone offset for HTML5 "date" inputs. It will always be GMT.

Option 1: HTML5 "datetime-local"

One possibility would be to use the input type "datetime-local" (do not use "datetime" as that is a deprecated field type), but keep in mind that may not actually give you what you want either because the timezone will be the user's timezone. If all your users are in the same timezone, then this might be a solution:

<input type="datetime-local" name="date_added" id="date_added" class="regular-text" value="<?php echo get_post_meta($post->ID,'date_added',true ); ?>">

Option 2: jQuery UI datepicker

A better solution might be to use something like the jQuery UI datepicker (which is included in WordPress). However, that is also GMT by default and is tricky to adjust for timezone offset. Unfortunately, I can't give you an example of timezone offset, but to get you started so you can use it, add the following to your functions.php:

add_action('wp_enqueue_scripts', 'my_add_jquery_datepicker');
function my_add_jquery_datepicker() {
    wp_enqueue_script( 'jquery' );
    wp_enqueue_script( 'jquery-ui-datepicker' );
    $css = wp_register_style( 'jquery-ui', '//code.jquery/ui/1.12.1/themes/base/jquery-ui.css' );
    wp_enqueue_style(  'jquery-ui', $css );
}

Then to use it, add some jQuery to attach your field's ID as a datepicker field. Just to give you an easy working example, here's a quick-and-dirty way to add it via wp_head (if used, you should implement this in a better way):

add_action( 'wp_head', function() {
    ?>
    jQuery(document).ready(function($) {
        $( "#date_added" ).datepicker();
    });
    <?php 
});

Once you're able to implement the jQuery datepicker, there are examples out there on how to implement a timezone offset for this date. Google is your friend (as is stackoverflow).

Or... leave the input as GMT and store as timezone offset

However, all of that being noted, if you want to adjust what the user inputs based on a specific timezone offset, you can take the default that you get as GMT and adjust it for the offset. Keep in mind that is not a "best practice" as dates/times should really be stored as GMT and adjusted when displayed.

Not sure its the complete answer, but try

        date_default_timezone_set('Asia/Jerusalem');

Replace Jerusalem with exact persian zone word.

本文标签: Change WordPress custom field default calendar