1

I am got using following code in a javascript file(custom.js):

gapinvite = new Date(2014, 06 - 1, 2);
$('.days').countdown({
    until: gapinvite,
    layout: '{dn} {dl}',
    timezone: +7
});

I am using theme options(theme-options.php) to get the input from a user and then need to use it in the above javascript file.If it was a php file I would've used:

$date = get_option('director_date');
if( $date) {
echo $date;
}

. Since it is a javascript file I do not know how that could be possible. Could you please help?

12
  • There are a lot of introductions to AJAX which you can find in Google. And that's how to use AJAX in WP codex.wordpress.org/AJAX_in_Plugins. Commented Mar 14, 2014 at 5:41
  • I'm not good at ajax.An answer would be appreciated Commented Mar 14, 2014 at 5:44
  • you want to add php code to javascript file ? Commented Mar 14, 2014 at 5:47
  • Yes.Or an alternative would be appreciated Commented Mar 14, 2014 at 5:48
  • you can add javascript to php file and the include the php file where you want Commented Mar 14, 2014 at 5:50

1 Answer 1

1

Output the value into a javascript variable during rendering.

<script>var directorDate = new Date(<?php echo date('Y, m, d', $date); ?>);</script>

Then in the javascript, reference directorDate variable.

The script tag above needs to be inserted before your custom.js, or custom.js needs to wait before looking for the variable (it will be undefined otherwise).

Sign up to request clarification or add additional context in comments.

8 Comments

during rendering? Do you mean i need to add the above code to theme-options.php"
While the page is being created on the server. For example, add the code above to your header.php template file in the <head> section if it should be available on all pages.
And how do i reference it. Once added?
In JavaScript it will be "directorDate".
you mean console.log("directorDate")?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.