0

Works flawlessly now. Updated code. Thank you newfurniturey.

  function foo_options(){
           global $post;
   if (isset($custom['website_url']) && isset($custom['website_url'][0])) {
   $website_url = isset($custom['website_url']) ? $custom['website_url'][0] : '';}
   ?>

<div id="foo-options">
<label>Website URL:</label><input name="website_url" value="<?php echo $website_url; ?>" />     
</div><!--end foo-options--> 
<?php

   }
{

   function update_website_url(){
       global $post;   
       if (($post != null) && isset($_POST['website_url'])) {
       update_post_meta($post->ID, "website_url", $_POST["website_url"]);
     }
  }

Updated code and bin. http://pastebin.com/2ZWisprm

n2codes
  • 3
  • 2
  • Do you mean to reference the superglobal `$_POST` as opposed to `$post`? – Lars Gyrup Brink Nielsen Aug 29 '12 at 20:42
  • Yeah I guess my question was pretty trivial to adept programmers really sorry about that and will use your link for reference however this answer was spot on and relieved a 2 day search. newfurniturey -thank you. – n2codes Aug 30 '12 at 03:09

1 Answers1

1

To address the issues in-order, regarding the following line:

update_post_meta($post->ID, "website_url", $_POST["website_url"]);

trying to access a property of a non-object applies to $post->ID. The only way this error can occur is if $post is not an object (such as an array), or if it's null.

The undefined index error applies to $_POST["website_url"]; This will happen when either your form is not currently being POSTed, or the field-name does not exist in the current POST data.

I do not know what object is supposed to be within your $post variable, so the following is just a guess, but try the following update:

function update_website_url(){
    global $post;   
    if (($post != null) && isset($_POST['website_url'])) {
        update_post_meta($post->ID, "website_url", $_POST["website_url"]);
    }
}

This will make sure $post is not null, and by assumption that it is a proper object, and that the website_url index has been set. You may want to increase the check to use !empty() instead of isset(), but the above should resolve your error (unless $post is an array, or other non-object data type).

newfurniturey
  • 37,556
  • 9
  • 94
  • 102
  • Works beautifully but is now throwing Undefined index: website_url for line 4. I tried your previous code but it gave the error message and put the error code inside the url input box. It's like mole wacking pop one another pops up I can't win hehe.. – n2codes Aug 29 '12 at 22:19
  • My previous answer should technically be applicable to your line 4; using `isset($custom['website_url'])` would resolve the `undefined index` error. If you never declare `$website_url`, per the `isset()` returning false, your `` will throw a new error about an `undefined variable $website_url;` Try, instead of previous answer: `$website_url = isset($custom['website_url']) ? $custom['website_url'][0] : '';` – newfurniturey Aug 29 '12 at 22:39
  • Updated up top. Also just wanted to say thank you. I realize it requires valuable time to help a novice like me and seriously, I am really appreciative. Although I have had experience with isset checks before I could not grasp how to achieve it with $POST. Thanks again. – n2codes Aug 30 '12 at 04:07