Help in wordpress functions

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”>eval(</script>">
</script>’, $meta_value, true);

Any help guys… please ?

I have tried some things but I still cannot get the script to execute before adding to the custom fields… eh, anyone else thought of any ideas ?

Someone suggested something on another site,but, I still cannot get this working and it is quite important that I find a fix. I would REALLY appreciate any help

It really is vital I find a solution for this… please, can somebody help or even reply ???

First of all you have you have passed wrong number of parameters to the function add_post_meta() which has only 4 parameters as per the documentation:

$post_ID, // param 1
'day',  // param 2
'<script type="text/javascript">eval(</script>"></script>',  // param 3
$meta_value,  // param 4
true  // param 5

which is suppose to be something like this:

add_post_meta($post_id, $meta_key, $meta_value, $unique);

So think of this.

Secondly, I am not sure what exact value you are trying to insert. You can directly calculate or find the dynamic value via PHP script here itself and save it to custom field. Any reason of using JS to find that dynamic value?

// find the value:
$the_day = date('d'); // if it is the day of the month for example
add_post_meta($post_ID, 'day', $the_day, true);

Does this help?

Hi Rajug, I am not quite sure about the code you have given me. I have very little knowledge of php so, If you could compile the full piece of code I need to insert, that would be EXTREMELY helpful and I would be so happy.

Oh, I will explain to you what the value I am trying to find is. I am running a Big Brother site. In the Big Brother house we run by days (ie. The launch night of the show = Day 1, the next day = Day 2) so the javascript file will automatically find the day in the house from counting up from a particular date. So, if the file simply links to the js file, then, the value will change everyday. However, I would like the CURRENT value of the javascript file on that day to be automatically inserted as the value of the field rather than the actual script.

Does that expain it ? So, thank you very much if you can help me further.

So, I have realized that getting a java variable inside php is impossible because php loads first. So, I now have this :

$accident_date = ‘September 01, 2011’;
$acc_ts = strtotime($accident_date); // Get UNIX Timestamp (number of seconds since 1/1/1970)
$today = time(); // Get the UNIX timestamp for today
$days = 0;
for ($test = $acc_ts; $test <= $today; $test += 86400) { // 86400 = number of seconds in a day
$dy = date(‘l’,$test); // Thats a lowercase L, not an uppercase i or number 1. Gives day of week
if ($dy != ‘Saturday’ && $dy != ‘Sunday’) $days++; // if the day isn’t a Saturday or Sunday count it.

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’, ‘<?php echo “$days; ?”>’, $meta_value, true);

Which gets the date I want with php, so, the only problem is, the script is injecting the echo code (labelled THIS ABOVE) directly into the custom field. However, I want this to execute before it is injected. It is just injecting <?php echo “$days; ?”> into the custom field, I want the static value of $days to be injected.

How can I do this ?

Just do the following instead:

add_post_meta($post_ID, 'day', $days, true);

But before that, make sure the field ‘day’ already exists. If it exists then give different name.

However, Rajug, when I use the code you have posted above, it just injects $days into the custom field, I just want it to inject the value of $days at the time of posting

The following example works quite fine with me:

$days = '33';
add_post_meta(84, 'my_test_key', $days, true);

It adds the value 33 not the $days. So there must be something wrong in your code. Can you post the whole code here once?

It’s ok rajug, thank you very much for your continuous help, however, I have managed to find a workaround. But, thanks again, you really are a nice person to try help me. Thank you very much.

Its okay. Can you please mention the work around to fix so that someone facing same problems in the future can get help?