1

I've been adding on to a plugin I'm using that works well overall, but has several issues that I've been having to go in and fix within it's core files.

The issue I'm dealing with right now is trying to fix how certain information is stored in the database.

The plugin enables private messaging. The situation is that this plugin uses the meta_key "_participants" to store the user ID of both sender and recipient of a message.

Thus when I try to display the recipient of a message, the only way I found to get the recipient's name/ID to display on the front-end is using the _participants meta_key.

However, as you can probably guess, when I use _participants metakey it pulls the data for both the recipient and the sender. I do not need the sender/message author to display when I pull the data.

I am asssuming that I need to change the meta_key that the recipient ID is stored with to something other than _participants in order to create a degree of separation.

Where/how can I change the name of the overarching meta_key so that I may attribute a different meta_key for my message recipients?

I also think another solution (and probably the better, more robust solution) could be to use some kind of conditional statement to exclude the message author when I pull the _participants data in the particular module I'm working with, however I'm still a noob with php so I'm not sure how to successfully pull this method off.

Would be awesome if someone could give some insight and guidance on either of these two potential solutions.

Here is the code that is displaying the usernames of the _participants (both sender + recipient)

  $post = $message; //setup_postdata does not work properly if variable name is NOT $post !!!!!

  ob_start();
  setup_postdata( $post ); //setup_postdata does not work properly if variable name is NOT $post !!!!!
  //$read_class = fep_is_read() ? ' fep-hide-if-js' : '';
  $participants = get_post_meta( get_the_ID(), '_participants' );
  $par = array();
  foreach( $participants as $participant ) {
    $par[] = fep_get_userdata( $participant, 'display_name', 'id' );
  }
  fep_make_read();
  fep_make_read( true );
  ?>
  <div class="fep-message">
    <div class="fep-message-title-heading2">
<?php _e("Message Participants", 'front-end-pm'); ?>: <?php echo implode( ', ', $par ); ?></div>

It currently displays on the front-end like this:

Message Participants: joesender, johnrecipient

but need to somehow exclude the message author to get it like this:

Recipient: johnrecipient

and here is another block that I think may be important to solving this (I think this is what is storing the data for author + recipient (aka the _participants)

function fep_send_message( $message = null, $override = array() )
{
if( null === $message ) {
    $message = $_POST;
}

if( ! empty($message['fep_parent_id'] ) ) {
    $message['post_status'] = fep_get_option('reply_post_status','publish');
    $message['message_title'] = __('RE:', 'front-end-pm'). ' ' . get_the_title( $message['fep_parent_id'] );
    $message['message_to_id'] = get_post_meta( $message['fep_parent_id'], '_participants' );
    $message['post_parent'] = absint( $message['fep_parent_id'] );
} else {
    $message['post_status'] = fep_get_option('parent_post_status','publish');
    $message['post_parent'] = 0;
}

$message = apply_filters('fep_filter_message_before_send', $message );

if( empty($message['message_title']) || empty($message['message_content']) ) {
    return false;
}
// Create post array
$post = array(
    'post_title'    => $message['message_title'],
    'post_content'  => $message['message_content'],
    'post_status'   => $message['post_status'],
    'post_parent'   => $message['post_parent'],
    'post_type'     => 'fep_message'
);

if( $override && is_array( $override ) ) {
    $post = wp_parse_args( $override, $post );
}

$post = apply_filters('fep_filter_message_after_override', $post );

// Insert the message into the database
$message_id = wp_insert_post( $post );

if( ! $message_id || is_wp_error( $message_id ) ) {
    return false;
}
$inserted_message = get_post( $message_id );

if( ! empty($message['message_to_id'] ) ) { //FRONT END message_to return id of participants
    if( is_array( $message['message_to_id'] ) ) {
        foreach( $message['message_to_id'] as $participant ) {
            add_post_meta( $message_id, '_participants', $participant );
        }
    } else {
        add_post_meta( $message_id, '_participants', $message['message_to_id'] );
    }
}
add_post_meta( $message_id, '_participants', $inserted_message->post_author );

if( $inserted_message->post_parent ) {

    $participants = get_post_meta( $inserted_message->post_parent, '_participants' );

    if( $participants && is_array( $participants ) )
    {
        foreach( $participants as $participant ) 
        {
            delete_post_meta( $inserted_message->post_parent, '_fep_parent_read_by_'. $participant );
            delete_user_meta( $participant, '_fep_user_message_count' );
        }
    }
    fep_make_read( true, $inserted_message->post_parent, $inserted_message->post_author );

} else {
    $participants = get_post_meta( $message_id, '_participants' );

    if( $participants && is_array( $participants ) )
    {
        foreach( $participants as $participant ) 
        {
            delete_user_meta( $participant, '_fep_user_message_count' );
        }
    }
}

fep_make_read( true, $message_id, $inserted_message->post_author );

 do_action('fep_action_message_after_send', $message_id, $message, $inserted_message );

return $message_id;
}
  • Please edit your question with code examples. – Nathan Powell Oct 18 '16 at 4:39
  • Hi Nathan! Done. I added the code that is actually displaying this area on the front-end. Still checking around the files to see if there are any other blocks of code I should include. – kosmicbird Oct 18 '16 at 5:11
  • It would be more helpful to see the code that stores the data of the author and recipient. – Nathan Powell Oct 18 '16 at 5:23
  • Yea, that would make more sense. Lol. I searched around for add_post_meta and update_post_meta in the files (I think that is how the data is stored?...sorry, I'm learning as I go, so my knowledge is deficient in random areas). I believe I just added the code that stores the data for author + recipient, not really sure 😅 – kosmicbird Oct 18 '16 at 5:29
  • 1
    OMG. Just figured it out. I modified the line that says } else { add_post_meta( $message_id, '_participants', $message['message_to_id'] ); by changing _participants to _recipients, then also applied the new _recipients meta_key to where it displays on front-end. – kosmicbird Oct 18 '16 at 6:15
0

Solution:

I modified the line that says } else { add_post_meta( $message_id, '_participants', $message['message_to_id'] ); by changing _participants to _recipients, then also applied the new _recipients meta_key to where it displays on front-end.

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.