Support » Plugins » Hacks » Headers and Saving Post Data

  • Timothy Jacobs


    I have been staring at this code for the past hour trying to figure out what is wrong. I keep getting thrown headers already sent errors, but I can’t seem to find what is triggering them. If I comment out this section of code everything works, so I am pretty sure it is something here.

    Any help would be greatly appreciated.

    add_action('save_post', 'tj_testimonials_cats_box_save');
    function tj_testimonials_cats_box_save($post_id)
        $args = array(
            'post_type' => 'testimonials',
            'post_status' => 'publish'
        $testimonials = get_posts($args);
        $testimonials_display = array(); 
        foreach ($testimonials as $testimonial) {
            $id = $testimonial->ID;
            if (isset($_POST[$id])) {
                $testimonials_display[$id] = $_POST[$id];
            else {
                $testimonials_display[$id] = 0;
        update_post_meta($post_id, 'tj_testimonials_on_page', $testimonials_display);

    Here is the error

    Warning: array_map() [function.array-map]: Argument #2 should be an array in /home/content/14/10762214/html/wp-includes/post.php on line 1918
    Warning: Cannot modify header information - headers already sent by (output started at /home/content/14/10762214/html/wp-includes/post.php:1918) in /home/content/14/10762214/html/wp-includes/pluggable.php on line 877

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz


    I suspect the error messages are a knock on effect from the actual error so are useless in locating the true fault. Line 1918 in post.php does not even remotely match up with the error messages.

    Just looking at your action function, I would question assigning $id an integer ($testimonial->ID is an integer). It is illogical to me to expect $_POST to be indexed by integers in any meaningful way. I believe you end up with $testimonials_display being an indexed array (I would expect an associative array) of 0 values, which is causing an error when the array is referenced elsewhere.

    This can easily be verified by doing a var_dump() of any of the involved arrays. I admit this can all be appropriate and I misunderstand the intent, but it’s the only thing I see unusual. But I’ve been know to miss some obvious errors…

    Timothy Jacobs


    Thanks bcworkz,

    I should probably elaborate slightly. I have a list of checkboxes which correspond to the name of a testimonial, the id of the input is the id of post that the testimonial corresponds to. So I am looping through the testimonials to get a list of those ids — this list could obviously be stored as an option and refresh on the creation or deletion of a post, but for now I’d like to just get this part working 🙂 — and then checking the $_POST variable for each id, recording the value and then updating it.

    It is important to note that the values do save, i.e., everything is sent to the database correctly, and everything works just fine in that regard. WP just throws me this error in-between.

    Here is the dump of $testimonials_display

    array(9) { [2854]=> string(2) "on" [2851]=> string(2) "on" [2844]=> int(0) [2839]=> int(0) [2508]=> int(0) [2503]=> int(0) [2501]=> int(0) [2496]=> int(0) [2495]=> int(0) }

    It looks correct to me and it functions properly.

    Thanks for the help!

    Moderator bcworkz


    Thanks to PHP being a loosely typed language, that sort of array will work fine most of the time… until it doesn’t. And then its difficult to locate where it failed. I still believe this sort of array is confusing some WP function that is expecting a more conventional data structure, which through some convoluted chain reaction, throws the errors you see.

    I see two things a bit unorthodox that could be an issue, or I could be completely wrong and the problem lies elsewhere. The more likely cause is easy to test. There may be a problem with the mixed data types in the array values, which are either a string "on" or an integer 0. Simply assigning the string "0" instead of the integer 0 could solve the problem. Just ensure whatever is processing the data knows how to deal with "0" instead of 0. Being loosely typed, it should, but you can’t always be sure.

    The other possibility is similar, but applies to the array index keys. Such arrays are usually associative, as in $testimonials_display['2584'] (string key) instead of indexed, as you currently have in $testimonials_display[2584] (integer index). You can easily convert an ID integer to a string with strval(), but the ramifications can run deeper. Or not, being loosely typed.

    Again, this may not really be the problem, but it is the only thing I see unusual. Not wrong mind you, just unusual.

    Timothy Jacobs


    The second possibility I think is more likely because of how I fixed my problem. After crazy amounts of brainstorming as to what could be going on, I looked back at the error that WP threw me, and noticed that it had to do with the post object. So my idea was that wordpress might have been seeing all of these post ids and it got confused by them. So I just appended “testimonial” to the end of the ID and removed “testimonial” when I saved it to the database. This works! But the reason it works doesn’t make so much logical sense. So what I actually could have done was convert the ID to a string when I concatenated it with the “testimonial” key word.

    Thanks so much for the help bcworkz!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Headers and Saving Post Data’ is closed to new replies.