Skip Navigation

[Resolved] Comments and Custom Type

This support ticket is created 6 years, 5 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 7 replies, has 3 voices.

Last updated by Christian Cox 6 years, 5 months ago.

Assisted by: Christian Cox.

Author
Posts
#957177

Hello,
I have activated comments on a custom type.

I want to add an action when a user add a comment.

It will be something like the code below :
add_action('comment_post', 'my_function',10,2);

My question is comment_post is used for post but what do i have to use for comments attached to a custom type ?

I have tried comment_<name_of_my_custom_type> but it doesn't work.

Best regards,

Olivier

#957220

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Olivier,

Thank you for contacting our support forum.

This hook should also work for your CPT because it triggers after a comment is saved to the database.

Thanks,
Shane

#957221

Ok but what name does I have to user ?

comment_<name_of_my_custom_type>

or

comment_post ?

If it comment_<name_of_my_custom_type> I use the slug name ?

#957226

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Olivier,

It should be the same comment_post as per the wordpress documentation.

Thanks,
Shane

#957229

It doesn't work with comment_post
i have tested the hook add_action('publish_<my_custom_type_name>' and it works but comment_< my_custom_type_name > doesn't work.
Can you tell me what I have to use ?

#957402

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Olivier,

What I can do is to check with my teammates to see if they are familiar with this. However based on the wordpress Doc the comment_post should work.

Could you share with me the code that you are using so that I can test it locally to see the results as well?

Thanks,
Shane

#1069377

My Custom Post Type is called fichier

When i insert this code :
add_action('publish_fichier', 'call_the_endpoint',10,2);

function call_the_endpoint($post_id, $post){
Content of my function it works
}

Then I have tried with the comment I have tried whith
add_action('comment_fichier', 'test_function',10,2);
and
add_action('comment_post', 'test_function',10,2);
It doesn't work

Can you help me ?

#1070314

Hi, Shane is on public holiday today so I'll try to help. The comment_post hook should work with custom post types, so I suspect something else is going on. Please try these troubleshooting steps first:
- Temporarily deactivate all plugins except Types and Views
- Submit the comment form on the CPT post. If the problem was resolved, activate your other plugins one by one until the problem returns.
- If the problem does not seem to be resolved, please turn on server logs and add error logging to this callback like this:

function test_function( $post_id, $post ) {
  error_log('comment posted for post ' . $post_id);
  // your code continues below this line...
  // ...
}
add_action('comment_post', 'test_function',10,2);

If you're not familiar with server logs, I can show you how to activate them temporarily. Go in your wp-config.php file and look for define(‘WP_DEBUG’, false);. Change it to:

define('WP_DEBUG', true);

Then add these lines, just before it says 'stop editing here':

ini_set('log_errors',TRUE);
ini_set('error_reporting', E_ALL);
ini_set('error_log', dirname(__FILE__) . '/error_log.txt');

Submit a Comment, and you should find an error_log.txt file in your site's root directory. Please send me its contents. Once that is done, you can revert the changes you made to wp-config.php.