【问题标题】:Loop notification to every user循环通知给每个用户
【发布时间】:2018-01-05 08:23:37
【问题描述】:

我正在尝试向分配给项目的每个用户发送通知。 我可以通过这样做获得所有用户ID

$ids = array_column($users, 'id');
    $userid = implode(', ', $ids);
    echo $userid;

但是当我试图把它放在我的函数中时,什么也没发生。它只是空白。 我也尝试在我的函数中移动“获取用户脚本”,但仍然没有运气。

编辑

我也尝试手动编写$ids = array("14","1"); 但我得到了同样的错误。但是,如果我将它移到函数内部,它就可以工作!如果我对$ids = array_column($users, 'id'); 做同样的事情,错误又回来了

如果我这样做,它会起作用。但我想自动获得用户。

$ids = array(
  array(
    'id' => 1,
    'first_name' => 'Peter',
    'last_name' => 'Griffin',
  ),
  array(
    'id' => 14,
    'first_name' => 'Ben',
    'last_name' => 'Smith',
  ),
  array(
    'id' => 3,
    'first_name' => 'Joe',
    'last_name' => 'Doe',
  )
);


    foreach ($ids as $key => $user) {
        $um_notifications->api->store_notification( $user['id'], 'new_action', $vars );
   }

这是完整的脚本,希望我已经解释清楚了。

<?php


    /* #### TEST OUTSIDE THE FUNCTION 'trigger_new_notification' #### 
        THIS WORKS AND PRINTING EX: '1, 8, 13'
    */ 
    $ids = array_column($users, 'id');
    $userid = implode(', ', $ids);
    echo $userid;



    /* #### NOTIFICATION 'trigger_new_notification' START #### */

    add_action('um_before_profile_fields', 'trigger_new_notification', 100);
    function trigger_new_notification( $args ) {
        global $um_notifications;


    /* Get information about the project */
    $vars = array(
        "post_title" => get_post_field( 'post_title', $project_id ),
        "photo" => um_get_avatar_url( get_avatar( get_current_user_id(), 40 ) ),
        "member" => um_user('display_name'),
        "notification_uri" => cpm_url_project_details( $project_id )
    );  

    /* Send notification to every assaigned user */
    foreach($ids as $row)
        {
            $um_notifications->api->store_notification( $row, 'new_action', $vars );
        }


     /* How it should print, where '14' is the user id */
    /* $um_notifications->api->store_notification( 14, 'new_action', $vars ); */


    }

    do_action( 'um_before_profile_fields');

    ?>

【问题讨论】:

  • 试试foreach($ids as $row)
  • 做到了,同样的结果.. :-(
  • $userid 是一个字符串,你不能foreach一个字符串。
  • 但我也试过 foreach($ids as $row)
  • 您现在拥有的代码应该可以工作。它应该打印一行类似于您的循环注释下方的行。只是为了确定,你可以 var_dump $ids 并将其粘贴到这里吗?

标签: php arrays function loops


【解决方案1】:

通过反复试验,找到了解决方案。

不确定使它起作用的单点是什么,但所做的更改是:
添加了函数调用
在函数调用中添加了 $ids
在函数声明中添加了 $ids

仍然不确定 $args 是什么,但我把它留在那里了。

$ids = array_column($users, 'id'); // get IDs of users
add_action('um_before_profile_fields', 'trigger_new_notification', 100); 

function trigger_new_notification( $args , $ids ) {
    global $um_notifications;
    $vars = array(
        "post_title" => get_post_field( 'post_title', $project_id ),
        "photo" => um_get_avatar_url( get_avatar( get_current_user_id(), 40 ) ),
        "member" => um_user('display_name'),
        "notification_uri" => cpm_url_project_details( $project_id )
    );
    foreach($ids as $row){ // iterate the userIDs
            $um_notifications->api->store_notification( $row, 'new_action', $vars );
    }
}

do_action( 'um_before_profile_fields'); 


trigger_new_notification( $args , $ids ); // Call function and pass $args and $ids as variables

【讨论】:

    【解决方案2】:

    如果你使用一个 $id 作为函数的参数,你可以试试这个:

    function trigger_new_notification( $id ) {
            global $um_notifications;
            $vars = array(
            "post_title" => get_post_field( 'post_title', $project_id ),
            "photo" => um_get_avatar_url( get_avatar( get_current_user_id(), 40 ) ),
            "member" => um_user('display_name'),
            "notification_uri" => cpm_url_project_details( $project_id )
        );  
      $um_notifications->api->store_notification( $id, 'new_action', $vars );
    
       }
    

    如果您尝试使用 ids av argumt 数组,您可以尝试以下代码:

    function trigger_new_notification( $ids ) {
            global $um_notifications;
    
    /* Get information about the project */
    $vars = array(
        "post_title" => get_post_field( 'post_title', $project_id ),
        "photo" => um_get_avatar_url( get_avatar( get_current_user_id(), 40 ) ),
        "member" => um_user('display_name'),
        "notification_uri" => cpm_url_project_details( $project_id )
    );  
    
    /* Send notification to every assaigned user */
    foreach($ids as $row)
        {
            $um_notifications->api->store_notification( $row, 'new_action', $vars );
        }
    
    
     /* How it should print, where '14' is the user id */
    /* $um_notifications->api->store_notification( 14, 'new_action', $vars ); */
    
    
    }
    

    【讨论】:

    • 试过function trigger_new_notification( $ids )但没有工作:-(
    • add_action('um_before_profile_fields', 'trigger_new_notification', 100); 100 是id?
    【解决方案3】:

    @Andreas 我同意你关于 $args 的看法。我从http://docs.ultimatemember.com/article/53-using-notifications-api-to-add-custom-notifications得到脚本

    这是正确的吗?同样的问题,但 var_dump 现在说 string(0) "".

    $ids = array_column($users, 'id');
    
    
    /* #### NOTIFICATION ACTIONS #### */            
    add_action('um_before_profile_fields', 'trigger_new_notification', 100);
    function trigger_new_notification( $ids ) {
            global $um_notifications;
    
                    um_fetch_user( get_current_user_id() );
    
    
    /* Get information about the project */
    $vars = array(
        "post_title" => get_post_field( 'post_title', GetLastPostId() ),
        "photo" => um_get_avatar_url( get_avatar( get_current_user_id(), 40 ) ),
        "member" => um_user('display_name'),
        "notification_uri" => '/projects/?project_id='.GetLastPostId().'&tab=project&action=index'
    );  
    
    $um_notifications->api->store_notification( $ids, 'new_action', $vars );
    
    
    }
    
    do_action( 'um_before_profile_fields');
    

    【讨论】:

    • 见这里:3v4l.org/jE5QC 我不得不评论这些功能,因为它们不能在 3v4l 上运行。但正如您所看到的,我调用了该函数,该函数可以使用 $ids 并输出用户 ID。这就是我认为应该的方式,但是没有注释功能。
    • @Andreas 它工作!谢谢! ☺️
    【解决方案4】:

    对于仍在寻求帮助的任何人

    $um_notifications->api->store_notification(
    

    已更改为:

    UM()->Notifications_API()->api()->store_notification(
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-13
      • 2022-01-13
      • 2018-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-06
      • 1970-01-01
      相关资源
      最近更新 更多