3

How can I get correct action hook on publish_posts for multiple custom types ?

I have a following problem, I can use this hook for posts or single custom post_type, but I don't know correct hook for multiple custom post types:

// Post publication for posts
add_filter ( 'publish_post', 'notify_published_post' );
function notify_published_post( $post_id ) {

I would like to publish to all even future custom post_types, so I would like to get something like this:

// Post publication hook for all custom posts
add_filter ( 'publish_anypost', 'notify_published_post' );
function notify_published_post( $post_id ) {
3
  • Does publish_post not work for custom post types? Jul 29, 2013 at 17:52
  • It works but for only one, I can make it work for publish_portfolio for example, but I would like to make it work for all custom post types I have. Or did you mean if it doesn't work for all of them in default? No, I have tried it and it didn't work for custom post types. Jul 29, 2013 at 17:56
  • i think question will contain add_action in example code instead of add_filter Jul 29, 2013 at 18:10

2 Answers 2

2

I think you can achieve this by:

1.determine the custom post types by using get_post_types() fx

It returns a list of post names or objects on basis of parameter you pass

<?php
$args = array(
  'public'   => true,
  '_builtin' => false
); //pass parameter to array according to get all custom post types( parameter pass for demo only.modify it to get desire result)
$post_types=get_post_types($args,'names');

?>

2.add action to custom post type when publish

<?php
foreach($post_types as $post_type){
  add_action( 'publish_'.$post_type, 'ravs_notify_published_post' );
}
function ravs_notify_published_post( $post_id ) {
 $post = get_post( $post_id );
 // Here's the magic
 Wp_Heartbeat_Notify::notify( array(
    'title'     =>      'New Post by ' . $post->post_author,
    'content'   =>      'There\'s a new post publish, why don\'t you <a href="' .get_permalink($post_id). '">give it</a> a look?',
    'type'      =>      'info'
  ));
}
?>

Updated

Create a plugin to show realtime notifications WP-Realtime-Notify

Edit

Paste this code in functions.php.It'll print array of name of custom post types.goto get_post_types and see all parameters.pass correct parameters which gives you required output( change $args ).if you get correct array of name of custom post types for required $args,change it with my $args in plugin.

<?php
add_action('the_content','ravs_customPostList');
function ravs_customPostList(){
$args = array(
  'public'   => true,
);
$post_types = get_post_types($args,'names'); //get names of post types
print_r($post_types);
}?>
12
  • Ravs thank you for answer where should I put the code above? I would like to make work this simple plugin below which uses new WP 3.6 heartbeat API plugin link is here Jul 29, 2013 at 18:15
  • create a plugin, paste this code in plugin file.Activate it.Make sure wp-heartbeat-notify activated.hope this will work Jul 29, 2013 at 18:26
  • thanks for your effort!, really appreciate it, the plugin is already created here all plugin files , but I can't make it work , I have tried to delete the action hook part in wp-heartbeat-notify.php and insert your code but it gives me the error :( , Jul 29, 2013 at 18:43
  • don't insert my code in wp-heartbeat-notify plugin, make a new plugin.Newly created plugin using wp-heartbeat-notify plugin functionality. Jul 29, 2013 at 18:50
  • I have let the original plugin untouched and I have created the new plugin with your 2 codes as you told me and I can't activate it, I will try to play with it all the night and I will let you know, thanks again Ravs, I thought that it will be more simple issue than I see it now. Fatal error: Cannot redeclare notify_published_post() (previously declared in /home6/dancersr/public_html/4ella/wp-content/plugins/wp-heartbeat-notify/wp-heartbeat-notify.php:35) in /home6/dancersr/public_html/4ella/wp-content/plugins/wp-heartbeat-multiple-notify.php on line 33 Jul 29, 2013 at 19:26
1

As i wrote on the doc this plugin has been full tested on WordPress 3.6-beta3 both on localhost and online but i'm not sure how it would work on later RC.

As soon as 3.6 release is coming out i'll check it out. For testing purpose i would suggest to download beta 3 here: http://wordpress.org/wordpress-3.6-beta3.zip

Remember that to see realtime notification you have to open two browser cause events are obviously not shown to the user that triggered them.

Hope this can help :)

1
  • 1
    Your plugin works perfect on RC2, you don't need to check it out, it works well for one custom post type or wp posts, I will wait for solution to make it work with all custom post types. Jul 30, 2013 at 13: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.