最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - Send Notification after post published was working but stopped after the last WP update

matteradmin8PV0评论

I'm just wondering why this snippet stopped working! It was working before the last WordPress Update!

I'm using DW Notifications plugin and work without any problem in another functions.

There are any explanation, Thanks.

function to_followers_only_notify( $new_status, $old_status, $post ) {

    global $post;

    if ( 'publish' !== $new_status or 'publish' === $old_status )
        return;

        $post = get_post($post->ID);
        $author = $post->post_author;
        $author_name = get_the_author_meta( 'display_name', $author );

        $followers = get_user_meta($author, '_pwuf_followers', true);
        $followers = array_unique($followers);

        $title = '<strong>'.$author_name.'</strong> ' . __('Published new post', 'dw-notifications'). ' <strong>'.$post->post_title.'</strong>';
        $link =  get_permalink($post->ID);
        if(has_post_thumbnail($post->ID)){
            $image = get_the_post_thumbnail_url($post->ID, 'hitmag-list') ;
        }

        $notif = array('title'=>$title, 'link' => $link, 'image' => $image);

        if ( !empty( $followers ) && is_array( $followers ) ) {

            $follower_ids = array();
            foreach( $followers as $follower ) {
                if ( is_numeric( $follower ) ) {

                    if(!$post->post_author || $post->post_author == $follower) continue;


                    $follower_ids[] = $follower;

                }
            }
            dwnotif_add_notification($follower_ids, $notif);
        }
}
add_action( 'transition_post_status', 'to_followers_only_notify', 10, 3 );

Update 1

When I used publish_post action back to work again! But I need to avoid from everytime posts updated :(, any suggestions please, Thanks.

function to_followers_only_notify( $ID, $post ) {

    global $post;

        $post = get_post($post->ID);
        $author = $post->post_author;
        $author_name = get_the_author_meta( 'display_name', $author );

        $followers = get_user_meta($author, '_pwuf_followers', true);
        $followers = array_unique($followers);

        $title = '<strong>'.$author_name.'</strong> ' . __('Published new post', 'dw-notifications'). ' <strong>'.$post->post_title.'</strong>';
        $link =  get_permalink($post->ID);
        if(has_post_thumbnail($post->ID)){
            $image = get_the_post_thumbnail_url($post->ID, 'hitmag-list') ;
        }

        $notif = array('title'=>$title, 'link' => $link, 'image' => $image);

        if ( !empty( $followers ) && is_array( $followers ) ) {

            $follower_ids = array();
            foreach( $followers as $follower ) {
                if ( is_numeric( $follower ) ) {

                    if(!$post->post_author || $post->post_author == $follower) continue;

                    $follower_ids[] = $follower;

                }
            }
            dwnotif_add_notification($follower_ids, $notif);
        }
}
add_action( 'publish_post', 'to_followers_only_notify', 10, 2 );

I'm just wondering why this snippet stopped working! It was working before the last WordPress Update!

I'm using DW Notifications plugin and work without any problem in another functions.

There are any explanation, Thanks.

function to_followers_only_notify( $new_status, $old_status, $post ) {

    global $post;

    if ( 'publish' !== $new_status or 'publish' === $old_status )
        return;

        $post = get_post($post->ID);
        $author = $post->post_author;
        $author_name = get_the_author_meta( 'display_name', $author );

        $followers = get_user_meta($author, '_pwuf_followers', true);
        $followers = array_unique($followers);

        $title = '<strong>'.$author_name.'</strong> ' . __('Published new post', 'dw-notifications'). ' <strong>'.$post->post_title.'</strong>';
        $link =  get_permalink($post->ID);
        if(has_post_thumbnail($post->ID)){
            $image = get_the_post_thumbnail_url($post->ID, 'hitmag-list') ;
        }

        $notif = array('title'=>$title, 'link' => $link, 'image' => $image);

        if ( !empty( $followers ) && is_array( $followers ) ) {

            $follower_ids = array();
            foreach( $followers as $follower ) {
                if ( is_numeric( $follower ) ) {

                    if(!$post->post_author || $post->post_author == $follower) continue;


                    $follower_ids[] = $follower;

                }
            }
            dwnotif_add_notification($follower_ids, $notif);
        }
}
add_action( 'transition_post_status', 'to_followers_only_notify', 10, 3 );

Update 1

When I used publish_post action back to work again! But I need to avoid from everytime posts updated :(, any suggestions please, Thanks.

function to_followers_only_notify( $ID, $post ) {

    global $post;

        $post = get_post($post->ID);
        $author = $post->post_author;
        $author_name = get_the_author_meta( 'display_name', $author );

        $followers = get_user_meta($author, '_pwuf_followers', true);
        $followers = array_unique($followers);

        $title = '<strong>'.$author_name.'</strong> ' . __('Published new post', 'dw-notifications'). ' <strong>'.$post->post_title.'</strong>';
        $link =  get_permalink($post->ID);
        if(has_post_thumbnail($post->ID)){
            $image = get_the_post_thumbnail_url($post->ID, 'hitmag-list') ;
        }

        $notif = array('title'=>$title, 'link' => $link, 'image' => $image);

        if ( !empty( $followers ) && is_array( $followers ) ) {

            $follower_ids = array();
            foreach( $followers as $follower ) {
                if ( is_numeric( $follower ) ) {

                    if(!$post->post_author || $post->post_author == $follower) continue;

                    $follower_ids[] = $follower;

                }
            }
            dwnotif_add_notification($follower_ids, $notif);
        }
}
add_action( 'publish_post', 'to_followers_only_notify', 10, 2 );
Share Improve this question edited Mar 24, 2019 at 19:42 Adham Mohamed asked Mar 23, 2019 at 0:56 Adham MohamedAdham Mohamed 1853 silver badges16 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Regarding Update 1. Perhaps you could compare the publish and modified dates and run your code only when they match, i.e. when the post is first published. Something along these lines,

function to_followers_only_notify( $ID, $post ) {

  $posted = strtotime( $post->post_date );
  $modified = strtotime( $post->post_modified );

  if ( $posted !== $modified ) {
    return;
  }

  // rest of the code
}
Post a comment

comment list (0)

  1. No comments so far