-
Help with custom fields and javascript
Hi, I am trying to set a code in my functions.php in wordpress that auto adds a custom field on a new post. Now, I have that working. However, the value I want it to insert is from a javascript file. The js file's value will change each day. All I can manage to get the custom field to do is add the code for the js file, therefore, what is displayed in posts will change every day. I want it to get the current value on the day of posting and to insert that value to the custom field.
Eg. if today the value of the file was "3" I want the number 3 to be inserted as a custom field. However, if the js code is just inserted, the next day, the post will display 4. I want it to insert the constant value the file has that day and it to stay like that.
Here is the code I have at the moment :
add_action('publish_page', 'add_custom_field_automatically');
add_action('publish_post', 'add_custom_field_automatically');
function add_custom_field_automatically($post_ID) {
global $wpdb;
if(!wp_is_post_revision($post_ID)) {
add_post_meta($post_ID, 'day', '<script type="text/javascript" src="http://bbuk4network.com/wp-content/themes/cbbcosmos/day.js">
</script>', $meta_value, true);
}
}
Any help would be appreciated. Thanks very much guys
-
Wouldn't it be easier to do whatever the JavaScript does with php instead. Does it have to be executed client side?
-
After looking at your JavaScript i do think it would be advised to do the same with php. I'm not proficient with JS but it looks like it's a function calculating the difference with the post date and a future date, which shouldn't be too hard to do with php.
-
Like magnus wrote use PHP instead. There is a bunch of premade date functions you can use. Just google php date difference
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules