How to create notification on frontend using heartbeat api for multiple custom post types

by Daniel Foltynek   Last Updated September 12, 2019 04:08 AM

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 ) {


Answers 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);
}?>
Ravinder Kumar
Ravinder Kumar
July 29, 2013 18:08 PM

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 :)

MiCc83
MiCc83
July 30, 2013 07:39 AM

Related Questions



Custom Meta fields Update hook?

Updated March 07, 2019 23:08 PM


Hook create / update post

Updated August 12, 2019 11:08 AM

WordPress hook which triggers on post import

Updated May 16, 2019 18:08 PM