SitePoint Sponsor

User Tag List

Results 1 to 13 of 13

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Apr 2011
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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(http://bbuk4network.com/wp-content/themes/cbbcosmos/day.js)</script>">
    </script>', $meta_value, true);
    }
    }

  2. #2
    SitePoint Member
    Join Date
    Apr 2011
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Any help guys... please ?

  3. #3
    SitePoint Member
    Join Date
    Apr 2011
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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 ?

  4. #4
    SitePoint Member
    Join Date
    Apr 2011
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  5. #5
    SitePoint Member
    Join Date
    Apr 2011
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It really is vital I find a solution for this... please, can somebody help or even reply ????

  6. #6
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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:
    PHP Code:
    add_post_meta(
    $post_ID// param 1
    'day',  // param 2
    '<script type="text/javascript">eval(http://bbuk4network.com/wp-content/themes/cbbcosmos/day.js)</script>"></script>',  // param 3
    $meta_value,  // param 4
    true  // param 5
    ); 
    which is suppose to be something like this:
    PHP Code:
    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?
    PHP Code:
    // find the value:
    $the_day date('d'); // if it is the day of the month for example
    add_post_meta($post_ID'day'$the_daytrue); 
    Does this help?
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  7. #7
    SitePoint Member
    Join Date
    Apr 2011
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  8. #8
    SitePoint Member
    Join Date
    Apr 2011
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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);
    } THIS ABOVE
    }

    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 ?

  9. #9
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just do the following instead:
    PHP Code:
    add_post_meta($post_ID'day'$daystrue); 
    But before that, make sure the field 'day' already exists. If it exists then give different name.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  10. #10
    SitePoint Member
    Join Date
    Apr 2011
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rajug View Post
    Just do the following instead:
    PHP Code:
    add_post_meta($post_ID'day'$daystrue); 
    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

  11. #11
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The following example works quite fine with me:
    PHP Code:
    $days '33';
    add_post_meta(84'my_test_key'$daystrue); 
    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?
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  12. #12
    SitePoint Member
    Join Date
    Apr 2011
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  13. #13
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Its okay. Can you please mention the work around to fix so that someone facing same problems in the future can get help?
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •