0

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' => 'admin@admin.com',
        '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');
}
  • Are you using a custom table to store your images? Images are "posts" in vanilla WordPress. – Andrew Bartel May 6 '13 at 18:48
  • I don't know anything about vanilla WP, but Image => jpg, gif and png. – Steven May 6 '13 at 19:26
  • If you use the wp media uploader, a row in the posts table is created that stores info about the image, where are you uploading them? Are you creating a database entry for them? – Andrew Bartel May 6 '13 at 19:34
  • Image was just an example. Pretend it's about potatoes. – Steven May 6 '13 at 21:31
0

You could create a hidden dummy post and use always its post ID as comment_post_ID. Then use a comment meta field to store the related ID from your custom table.

The other, and probably better option: use custom post types, not tables if you need something that acts like a post. Register that post type with …

'supports' => array(
    'comments',
),

… and the problem will not exist anymore.

To add custom data, hook into wp_insert_comment:

add_action( 'wp_insert_comment', 'prefix_comment_meta', 10, 2 );

function prefix_comment_meta( $id, $comment )
{
    // evaluate POST request and create comment meta
}
  • Yea but we haven't even established how he's storing the images yet toscho. – Andrew Bartel May 6 '13 at 19:50
  • @AndrewBartel I read that as one example for the bunch of custom tables. – fuxia May 6 '13 at 19:53
  • @toscho Yes, I thought about that. Do you know how I can get my custom properties when I create a custom function in functions.php and use add_action()? See example at codex.wordpress.org/Function_Reference/add_comment_meta – Steven May 6 '13 at 21:36
  • 1
    @Steven Not sure what you mean. You can use meta_query with WP_Comment_Query, so that should not be a problem. Please do not register custom content (post types, taxonomies, meta keys, tables) in a theme. That isn’t even loaded when SHORTINIT is TRUE. Use a plugin for that. – fuxia May 7 '13 at 3:32
  • When I submit a comment, I also need to submit Entity name and Entity ID and store this info in the comment meta. How can this be registered in the comment_meta table when I do a wp_insert_comment()? – Steven May 7 '13 at 15:39

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.