admin管理员组文章数量:1414621
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 ) {
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 ) {
Share
Improve this question
edited Oct 16, 2013 at 7:45
Ravinder Kumar
2,5681 gold badge14 silver badges20 bronze badges
asked Jul 29, 2013 at 17:38
Daniel FoltynekDaniel Foltynek
1836 silver badges21 bronze badges
3
|
2 Answers
Reset to default 2I 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);
}?>
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/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 :)
本文标签: hooksHow to create notification on frontend using heartbeat api for multiple custom post types
版权声明:本文标题:hooks - How to create notification on frontend using heartbeat api for multiple custom post types 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745166221a2645695.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
publish_post
not work for custom post types? – montrealist Commented Jul 29, 2013 at 17:52publish_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. – Daniel Foltynek Commented Jul 29, 2013 at 17:56add_action
in example code instead ofadd_filter
– Ravinder Kumar Commented Jul 29, 2013 at 18:10