For a while now I’ve been rather confused as to the location of comments against some of my posts and pages. People’s comments were being added to the attached images rather than the original post or page. Additionally, pages or posts could display a comment form even when the checkbox to allow comments was not ticked. I tracked the problem down yesterday evening, and have now developed a fix. This will be in oik version 1.17
- Reader writes a comment against a post/page
- The comment is misplaced; it’s associated to a different post or page, or sometimes an attached image
- Replies to the comment appear against the misplaced comment
In cases where a shortcode (such as [bw_list] was being used to list attached or related items, the comment form being displayed was associated with the most recently loaded post id, not the original post.
This is mostly due to default processing. In the WordPress comment_form() function, if the $post_id is not specified then the routine uses the global $id variable instead. The global $id variable gets set by the setup_postdata() function but doesn’t always get reset when the global $post is changed. So the values of $post-ID and $id can get out of sync.
A couple of the dynamic shortcodes load multiple posts and invoke the routines that set the global $id variable.
When time the comment_form() function is called in the theme’s template processing, the value that is set in $id no longer matches the post ID of the main content being displayed.
So any comment that is created using the generated form is associated with the wrong post.
The fix is to save any values that can be corrupted during shortcode expansion and reset them after the expansion. The function that oik v1.17 uses to do this is bw_global_post(). An alternative fix would be to avoid calling setup_postdata(). These changes are currently being tested.