0

I want to create alert system for users. If user get message I want to show info andout it near user image in header. So I create table for message

id_message | id_user | sender_name | sender_email | subject | sender_text | datatime

also I want to add meta key and meta value for each user - when user check messages.

So I add this code to header.php. I want to check if user has last_seen meta value - if not I want to create it

if ( metadata_exists( 'user', $user_id, $meta_key ) ) 
{
    $user_last_seen = get_user_meta( $user_id, $meta_key, true );
    $last_message_time = $wpdb->get_results( "SELECT * FROM ej_usermeta WHERE meta_key = 'last_seen' AND user_id = ".$user_id );
    if ( $last_message_time > $user_last_seen ){
        <a href="/messages"><p>New message</p></a>
    }
}
else 
{
    $meta_value = date('Y-m-d H:i:s');
    add_user_meta( $user_id, 'last_seen', $meta_value, true );
}

Update meta key last_seen when user click on messages

<input type="hidden" name="user_id" class="current-user-id" value="<?php get_current_user_id();?>">

Send user id to function update_last_seen_value

jQuery(".current-user-id").on('click', function () {
  var id = $(".current-user-id").attr("id");
  jQuery.ajax({
      type: 'POST',
      url: ajaxurl,
      data: {"action": "update_last_seen_value", "id": id},
      success: function (data) {
         
      }
  });
});

Code in function.php for updating last_seen value

function update_last_seen_value() {
    global $wpdb;
    $table = 'ej_users';
    $id = $_POST['id'];
    $meta_value = date('Y-m-d H:i:s');
    update_user_meta( $id , 'last_seen', $meta_value );
}

But it doesnt work

1 Answer 1

0

You need to use wp_ajax action to callback your function.

add_action( 'wp_ajax_update_last_seen_value', 'update_last_seen_value' );
function update_last_seen_value() {
    global $wpdb;
    $table = 'ej_users';
    $id = $_POST['id'];
    $meta_value = date('Y-m-d H:i:s');
    update_user_meta( $id , 'last_seen', $meta_value );
        $data = array(
            "status"     => 'success'
        );
    echo json_encode($data);
    die();
}

I have used your code and made correction to render scenario to my machine.

header.php file should be as per my understanding. You can make change as per your requirement.

<?php 
if(is_user_logged_in())
{
    $user_id = get_current_user_id();
    $meta_key = 'last_seen';
    if ( metadata_exists( 'user', $user_id, $meta_key ) ) 
    {
        $user_last_seen = get_user_meta( $user_id, $meta_key, true );
        if ( !empty($user_last_seen ))
        {  ?>
            <a href="javascript:void(0);" class="message-link"><p>New message</p></a>
            <input type="hidden" name="user_id" class="current-user-id" value="<?php echo get_current_user_id();?>">
            <?php
        }
    }
    else 
    {
        $meta_value = date('Y-m-d H:i:s');
        add_user_meta( $user_id, 'last_seen', $meta_value, true );
    }
}

jQuery snippet

   jQuery(".message-link").on('click', function () {
        var id = jQuery(".current-user-id").val();
        jQuery.ajax({
            type: 'POST',
            url: ajaxurl,
            data: {"action": "update_last_seen_value", "id": id},
            success: function (data) {
                console.log(data);
            }
        });
    });

Please check above code and let me know if you have any questions related this. I have tested and it is working fine for me.

4
  • Also I have table with messages and message table has datatime fiel. And I want to show message alert only if I have new message. So I try $last_message_time = $wpdb->get_results( "SELECT datetime FROM ej_user_messages WHERE id_user = ".$user_id."ORDER BY datetime DESC LIMIT 1" ); if ( $last_message_time > $user_last_seen ) { ?> <a href="javascript:void(0);" class="message-link"><p>New message</p></a> <input type="hidden" name="user_id" class="current-user-id" value="<?php echo get_current_user_id();?>"> <?php } Jul 18 at 12:19
  • when I try to see results of query I get Array ( ) - I get empty resuts. So I can't use if else Jul 18 at 12:27
  • It seems problem with Query $last_message_time = $wpdb->get_results( "SELECT 'datetime' FROM ej_user_messages WHERE id_user = ".$user_id."ORDER BY datetime DESC LIMIT 1" ); Jul 18 at 12:39
  • Yes, you need to check last seen and last message time using strtotime function. the mail problem was you did not use wp_ajax. Jul 18 at 17:54

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.