I'm using WP as the framework for my site and I have a bunch of custom tables.
Now I need to allow users to write comments to various entities. Let's say one entity is image and I want users to comment my images.
How would I go about using wp_insert_comment()
when I only have an image ID and not an Post ID? (The image is not related to a post).
Example:
Using jQuery, I execute this function:
public function addComment(){
$data = array(
'comment_post_ID' => 123,
'comment_author' => 'Steven',
'comment_author_email' => '[email protected]',
'comment_author_url' => 'http://www.storelocator.com',
'comment_content' => 'Lorem ipsum dolor sit amet...',
'comment_author_IP' => '127.0.0.1',
'comment_agent' => 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; fr; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3',
'comment_date' => date('d-m-Y H:i:s'),
'comment_date_gmt' => date('d-m-Y H:i:s'),
'comment_approved' => 0,
);
$comment_id = wp_insert_comment($data);
echo 'Comment ID: '.$comment_id;
}
This works. The problem is that there is no post with the ID 123. So when I go to my admin interface I get error messages like:
Notice: Trying to get property of non-object in /var/www/storelocator/wp-includes/capabilities.php on line 1178
Notice: Trying to get property of non-object in /var/www/storelocator/wp-admin/includes/class-wp-comments-list-table.php on line 488
Is there any way I can resolve this issue?
Update
I'm trying to add some meta data, but the function is not triggered in functions.php
.
This is the code I'm using so far:
// Comment submit on a car
http://mysite.com/wp-content/themes/mytheme/include/jquery.php?action=addComment&entity=car&entity_id=247
// My web service
- More or less as above. I've added two fields:
'entity' => $_REQUEST['entity_id'],
'entity_id' => $_REQUEST['entity']
I've also created a post, and using this post ID for comment_post_ID.
// functions.php - This is not triggered
add_action('comment_post', 'pre_handle_comment');
function pre_handle_comment($comment_id){
add_comment_meta($comment_id, 'entity', $_POST['comment_type']);
// Also tried
add_comment_meta($comment_id, 'entity', 'car');
}