0

I am trying to create custom post types with wordpress 4. I followed a tutorial book called "Buildin Wordpress Themes From Scratch". I am on localhost so debug mode is on.

When I try create a new CPT, I get the form but still I get undefined index errors on source, author and date. I tried to solve by checking array index but it did not solve either. Can you help me to fix this problem?

function basin_manager_meta_options(){
      global $post;
      if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
      return $post_id;

            $custom = get_post_custom($post->ID);
            if(isset($custom["source"])){
                    $source= $custom["source"][0];
            }
            $author= $custom["author"][0];
            $date= $custom["date"][0];

      ?>
      <style type="text/css">
      <?php include('basin-manager.css'); ?>
      </style>
      <div class="basin_manager_extras">
      <?php
      $website= ($website == "") ? "http://" : $website;
      ?>
      <div><label>Website / Gazete ? :</label>
        <input name="source" value="<?php echo $source; ?>" />
      </div>
      <div><label>Yazar:</label>
        <input name="author" value="<?php echo $author; ?>" />
      </div>
      <div><label>Date:</label>
        <input name="date" value="<?php echo $date; ?>" />
      </div>
      <?php
        }
      add_action('save_post', 'basin_manager_save_extras');
      function basin_manager_save_extras(){
        global $post;
        if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ){
        //if you remove this the sky will fall on your head.
        return $post_id;
        }else{
          update_post_meta($post->ID, "source", $_POST["source"]);
          update_post_meta($post->ID, "author", $_POST["author"]);
          update_post_meta($post->ID, "date", $_POST["date"]);
        }
      }

Thanks.

1

First thing i see that needs to be changed is your assigining unchecked values to your input fields. Try wrapping isset round the variable before echoing, this will stop errors if they are not assigned or empty.

Second thing as you have passed a post id to get_post_custom() you dont need the second array.

function basin_manager_meta_options(){
    global $post;
    if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
        return $post_id;

    $custom = get_post_custom($post->ID);
    if(isset($custom["source"])){
        $source= $custom["source"];
    }
    $author= $custom["author"];
    $date= $custom["date"];**

?>
    <style type="text/css">
        <?php include('basin-manager.css'); ?>
    </style>
    <div class="basin_manager_extras">
<?php
    $website= ($website == "") ? "http://" : $website;
?>
    <div><label>Website / Gazete ? :</label>
    <input name="source" value="<?php echo isset($source) ? source : ''; ?>" />
    </div>
    <div><label>Yazar:</label>
    <input name="author" value="<?php echo isset($author) ? $author : ''; ?>" />
    </div>
    <div><label>Date:</label>
    <input name="date" value="<?php echo isset($date) ? $date : ''; ?>" />
    </div>
<?php
}
add_action('save_post', 'basin_manager_save_extras');

function basin_manager_save_extras(){
    global $post;
    if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ){
        //if you remove this the sky will fall on your head.
        return $post_id;
    } else {
        update_post_meta($post->ID, "source", $_POST["source"]);
        update_post_meta($post->ID, "author", $_POST["author"]);
        update_post_meta($post->ID, "date", $_POST["date"]);
    }
}
  • 1
    I corrected with checking if the $custom object is set. if isset($custom) and then getting values. thanks mate. – ytsejam Oct 1 '14 at 11:51
  • 2
    You might care to check out how to properly format code. I went ahead and did that in this case. – Johannes Pille Oct 1 '14 at 13:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.