Skip Navigation

[Resolved] How to import message system from classifieds reference site to my own website?

This thread is resolved. Here is a description of the problem and solution.

Problem:
The Classifieds Reference Site of toolset uses a Custom Code to submit messages to a vendor.
How can you create this with Toolset only and CRED API?

Solution:
Read the below thread from:
https://toolset.com/forums/topic/how-to-import-message-system-from-classifieds-reference-site-to-my-own-website/#post-373667
to:
https://toolset.com/forums/topic/how-to-import-message-system-from-classifieds-reference-site-to-my-own-website/#post-375107

This support ticket is created 8 years, 2 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 13 replies, has 2 voices.

Last updated by George 8 years, 2 months ago.

Assisted by: Beda.

Author
Posts
#370454

Hello,

I would like to use the message system from the classifieds site on my own website which already has a number of different things that users could send messages for, such as "language partners, tour guides, apartment rentals", etc. I see that the message system relies pretty heavily on php to create actions and shortcodes.

Unfortunately, I don't really understand php (hence why I bought Toolset), and there is no guide I could find that shows how to implement the message system. The user guide for the classifieds site is from 2014 and has only a few sentences explaining what messages does.

Could you please show me which things I need to add into my child theme's functions.php file to get messaging to work, and also explain what some of the functions and shortcodes mean so I could change their identifiers (if needed) for messaging catered to different CPTs? I think this post would be very useful for those who already have a website started and would like to take some modules out of the reference sites.

Thanks much!

#370583

In our Toolset Classifieds plugin , we do have a Class that handles the messaging system.
https://toolset.com/account/downloads/#application-frameworks

It's called 'message_system.class.php'.

Different methods insides that class handles things on messaging.

That reference site (The Toolset Classifieds) extends the out of the box CRED functionality using the CRED API
These are included on the toolset-classifieds plugin like above mentioned

If you want to see the messaging system in action, import a classifieds with layouts (non-multilingual to simplify example) in http://discover-wp.com

Then go to each listing page in detail.

You can see a 'Contact Advertiser' button that allows the advertiser of that ad to be contacted.
The user contacting the advertiser will automatically be registered with a guest account.

Both parties can view messages in My Account - My Messages.

The functionality you are looking for is enriched with CRED API Custom code that is included in the mentioned Plugin.

You can also setup easier Messaging systems by simply let your users submit new Posts, and Answers, which eventually can be set as Childs to the Initial Posts.

Thank you

#373307

Hey Beda,

I tried examining the php file and the starter site, but I still couldn't figure out how to implement this system on my site. I don't want guests to be able to post, so I snipped out that line of code, and WPML duplication code. I also don't want users' last names to be shown publicly, so I snipped that out of the CRED form.

When I try to send a message, the custom fields for "Message To" "Listing ID" "To First Name" "To Last Name" and "To Email" aren't updated with anything.

I have post types other than "Listings" that I would also like to implement this system on. Does that mean I have to repeat this code multiple times, replacing listing_id with otherCPT_id???

Here's the part I pasted into functions.php from message_system.class.php:

/**
	 * Class Toolset_Classifieds_MessageSystem
	 * messaging system specific functions
	 */
		function __construct() {
			
			add_action( 'init', array( $this, 'classifieds_process_guest_login' ) );

			add_shortcode( 'classifieds-message-userdata', array( $this, 'classifieds_get_message_userdata' ) );
			add_shortcode( 'classifieds-message-data', array( $this, 'classifieds_get_message_data' ) );

			add_action( 'cred_save_data_form_new-message', array(
				$this,
				'classifieds_cred_save_data_new_message_function'
			), 10, 2 );
			add_action( 'cred_submit_complete_form_new-message', array(
				$this,
				'classifieds_cred_submit_complete_new_message'
			), 10, 2 );
			add_action( 'cred_save_data_form_reply-message', array(
				$this,
				'classifieds_cred_save_data_reply_message_function'
			), 10, 2 );
			add_action( 'cred_submit_complete_form_reply-message', array(
				$this,
				'classifieds_cred_submit_complete_reply_message'
			), 10, 2 );

			add_filter( 'wpv_filter_wpv_view_shortcode_output', array( $this, 'prefix_clean_view_output' ), 5, 2 );
		}

		/**
		 * @param $post_id
		 * @param $form_data
		 */
		function classifieds_cred_save_data_new_message_function( $post_id, $form_data ) {
			/* check if we have a listing-id so we can associate the message to it */
			if ( ! empty( $_GET['listing-id'] ) ) {
				$listing_id   = intval( $_GET['listing-id'] );
				$listing_post = get_post( $listing_id );
				//get the author of the post
				$advertiser_id   = $listing_post->post_author;
				$advertiser_data = get_userdata( $advertiser_id );
				//associate information about the advertiser and the listing in the message
				add_post_meta( $post_id, 'wpcf-message-to', $advertiser_id, true );
				add_post_meta( $post_id, 'wpcf-to-firstname', $advertiser_data->user_firstname, true );
				add_post_meta( $post_id, 'wpcf-to-lastname', $advertiser_data->user_lastname, true );
				add_post_meta( $post_id, 'wpcf-to-email', $advertiser_data->user_email, true );
				add_post_meta( $post_id, 'wpcf-listing-id', $listing_id, true );


			}
		}

		/**
		 * @param $post_id
		 * @param $form_data
		 */
		function classifieds_cred_submit_complete_new_message( $post_id, $form_data ) {
			$_SESSION['classifieds_msg_post_id'] = $post_id;
			$user_email                          = get_post_meta( $post_id, 'wpcf-from-email', true );
			update_post_meta( $post_id, 'wpcf-message-from', $user_id );
			wp_update_post( array( 'ID' => $post_id, 'post_author' => $user_id ) );
		}
		
				/**
		 * @param $post_id
		 * @param $form_data
		 */
		function classifieds_cred_save_data_reply_message_function( $post_id, $form_data ) {
			/* check if we have a listing-id so we can associate the reply message to it */
			if ( ! empty( $_GET['listing-id'] ) ) {
				$listing_id = intval( $_GET['listing-id'] );
				add_post_meta( $post_id, 'wpcf-listing-id', $listing_id, true );
			}
			if ( ! empty( $_GET['original-message-id'] ) ) {
				//retrieve infromation from the initial message to the reply one
				$original_message_id    = intval( $_GET['original-message-id'] );
				$message_from_user_id   = get_post_meta( $original_message_id, 'wpcf-message-to', true );
				$user_from_data         = get_userdata( $message_from_user_id );
				$message_from_firstname = $user_from_data->user_firstname;
				$message_from_lastname  = $user_from_data->user_lastname;
				$message_from_email     = $user_from_data->user_email;
				$message_to_user_id     = get_post_meta( $original_message_id, 'wpcf-message-from', true );
				add_post_meta( $post_id, 'wpcf-message-from', $message_from_user_id, true );
				add_post_meta( $post_id, 'wpcf-from-firstname', $message_from_firstname, true );
				add_post_meta( $post_id, 'wpcf-from-lastname', $message_from_lastname, true );
				add_post_meta( $post_id, 'wpcf-from-email', $message_from_email, true );
				add_post_meta( $post_id, 'wpcf-message-to', $message_to_user_id, true );
			}
		}
		
				/**
		 * get message userdata
		 *
		 * @param $atts
		 *
		 * @return mixed|string
		 */
		function classifieds_get_message_userdata( $atts ) {
			if ( ! empty( $_GET['original-message-id'] ) ) {
				$original_message_id = intval( $_GET['original-message-id'] );
			}
			extract( shortcode_atts( array(
				'field_user' => '',
				'user_id'    => '',
			), $atts ) );
			switch ( $field_user ) {
				case 'user_name':
					$user_data      = get_userdata( $user_id );
					$user_firstname = $user_data->user_firstname;
					$user_lastname  = $user_data->user_lastname;
					$user_name      = trim( $user_firstname . ' ' . $user_lastname );
					if ( ! empty( $user_name ) ) {
						$query = trim( $user_firstname . ' ' . $user_lastname );
					} else {
						$query = $user_data->user_login;
					}
					break;
				case 'firstname':
					if ( isset( $original_message_id ) ) {
						$query = get_post_meta( $original_message_id, 'wpcf-from-firstname', true );
					}
					break;
				case 'lastname':
					if ( isset( $original_message_id ) ) {
						$query = get_post_meta( $original_message_id, 'wpcf-from-lastname', true );
					}
					break;
				case 'email':
					if ( isset( $original_message_id ) ) {
						$query = get_post_meta( $original_message_id, 'wpcf-from-email', true );
					}
					break;
				case 'validation':
					$user_data = get_userdata( $user_id );
					//generate encrypted key with secret pass-phrase and user nickname to challenge URL request
					$query = sha1( 'Toolset Classifieds by OnTheGoSystems' . $user_data->nickname );
					break;
			}
			if ( isset( $query ) ) {
				return $query;
			}
		}

		/**
		 * auto login for client/subscriber account
		 */
		function classifieds_process_guest_login() {
			if ( isset( $_GET['user'] ) && ! is_user_logged_in() ) {
				$userinfo = get_user_by( 'id', $_GET['user'] );
				if ( ! isset( $_GET['validation'] ) ) {
					return;
				}
				$validation = false;
				$_GET['validation'];
				$user_data = get_userdata( $userinfo->ID );
				//challenge against encrypted key with secret pass-phrase and user nickname pased in URL request
				if ( sha1( 'Toolset Classifieds by OnTheGoSystems' . $user_data->nickname ) == $_GET['validation'] ) {
					$validation = true;
				}
				if ( $validation && $userinfo->has_cap( 'subscriber' ) ) {
					$creds                  = array();
					$creds['user_login']    = $userinfo->user_login;
					$creds['user_password'] = 'PASSWORD';
					$creds['remember']      = true;
					$user                   = wp_signon( $creds, false );
					$location               = $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
					if ( is_wp_error( $user ) ) {
						return;
					}
				} else {
					$the_page           = get_page_by_title( 'My Account' );
					$translated_page_id = $this->_classifieds_lang_id( $the_page->ID, 'page' );
					$location           = esc_url( get_permalink( $translated_page_id ) );
				}
				// redirect after header definitions - cannot use wp_redirect($location);
				?>
				<script type="text/javascript">
					<!--
					window.location = <?php echo "'" . $location . "'"; ?>;
					//-->
				</script>
				<?php
			}
		}

		/**
		 * get message data for the email body
		 *
		 * @param $atts
		 *
		 * @return mixed|string
		 */
		function classifieds_get_message_data( $atts ) {
			extract( shortcode_atts( array(
				'post_id' => '',
				'get'     => ''
			), $atts ) );
			switch ( $get ) {
				case 'from_name':
					$from_firstname = get_post_meta( $post_id, 'wpcf-from-firstname', true );
					$from_lastname  = get_post_meta( $post_id, 'wpcf-from-lastname', true );
					$query          = trim( $from_firstname . ' ' . $from_lastname );
					break;
				case 'to_name':
					$to_firstname = get_post_meta( $post_id, 'wpcf-to-firstname', true );
					$to_lastname  = get_post_meta( $post_id, 'wpcf-to-lastname', true );
					$query        = trim( $to_firstname . ' ' . $to_lastname );
					break;
				case 'message_content':
					$query = get_post_meta( $post_id, 'wpcf-message-description', true );
					break;
			}

			return $query;
		}

		/**
		 *
		 * clean as much as possible the output of a Views Loop
		 * in order to be able to use it on a CRED generic field default values
		 *
		 * @param $out
		 * @param $id
		 *
		 * @return string
		 */
		function prefix_clean_view_output( $out, $id ) {

			$advertiser_view_title = 'Advertiser details view';
			$post_type             = 'view';

			$advertiser_view = get_page_by_title( $advertiser_view_title, 'OBJECT', $post_type );

			if ( $advertiser_view->ID && $advertiser_view->ID == $id ) {
				$start = strpos( $out, '<!-- wpv-loop-start -->' );
				if (
					$start !== false
					&& strrpos( $out, '<!-- wpv-loop-end -->', $start ) !== false
				) {
					$start = $start + strlen( '<!-- wpv-loop-start -->' );
					$out   = substr( $out, $start );
					$end   = strrpos( $out, '<!-- wpv-loop-end -->' );
					$out   = substr( $out, 0, $end );
				}
			}

			return $out;
		}
		
		//get page url by title to be used in the content of pages
add_shortcode( 'get-page-url', 'my_get_page_by_title');
function my_get_page_by_title($atts){
    extract( shortcode_atts( array(
    'page' => '',
    ), $atts ) );
    switch ($page){
        case 'my-messages':
            $query = esc_url(get_permalink(get_page_by_title('My Messages')));
            break;
        case 'my-account-settings':
            $query = esc_url(get_permalink(get_page_by_title('My Account Settings')));
            break;
        case 'my-ads-list':
            $query = esc_url(get_permalink(get_page_by_title('My Ads list')));
            break;
        case 'logout':
            $query = esc_url(get_permalink(get_page_by_title('Logout')));
            break;
        case 'contact-advertiser':
            $query = esc_url(get_permalink(get_page_by_title('Contact Advertiser')));
            break;
        case 'reply-message':
            $query = esc_url(get_permalink(get_page_by_title('Reply message')));
            break;
    }
    return $query;
}
#373404

Yes, those are unique Slugs.

But let me help you with this in a appropriate and "Toolset-Like" manner.

1. This Custom Codes will be removed soon from all Test Sites upon my request.

Why?
Toolset is a plugin to help you build things with no PHP and I do think it is not appropriate to recommend Toolset as a plugin with which you can create things without PHP; but then provide Custom PHP codes in reference sites, which most Users will not understand completely.

This means, in future all this Customized Solutions will either be removed or correctly Documented and provided as AddOn Plugins.

2. For now, please let me know what exactly you want to do and we will do this together with no (or at least way less) PHP Code.

3. I understand the basic function you need is the ability to send Messages (notifications) to different users depending on a selection made by the submitter of the form

If this is correct, we may develop a Toolset only solution together, which will not only be easier but also enable you to implement this in any future site you will develop without relying on PHP.

If you agree, please shortly elaborate your exact needs (Step by step workflow you wish to happen)

Then I will follow up with suggestions and help to achieve it.

Thank you

#373413

Beda,

That sounds great, and I think it would save users/support lots of time in the future!

Basically my site has a number of CPTs, such as Listings, Organizers, Tour Guides, Items, etc. Site users should be able to send messages to the post author of any given post and inquire about whatever it is they need.

Example Workflow :

1. "Items" CPT has the post "Computer for Sale - $1500", which was created by post author = A.

2. User B is interested in the computer and sends a message to A through a form displayed on the computer post.

3. User A gets an email and then can reply to user B, who would then receive an email and so on...)

4. The web admin can also view and manage messages.

Thanks!

#373667

This should be very easy.

1. Basically my site has a number of CPTs, such as Listings, Organizers, Tour Guides, Items, etc. Site users should be able to send messages to the post author of any given post and inquire about whatever it is they need.

hidden link

Since you need to contact the Author of the Post where the user "comments" and not a static email the process is slightly more complicated.

I suggest you make a Post Type "Contacts"
This is child to all Post Types that you want the user to be able to contact the author.

CRED will include the parent Selector in the Form for your Contact Posts.
Hide it with <div class="hidden">CRED parent selector here</div>

Insert the "Contact" CRED form on the Post to be commented (as example you can use a Content template so you need to insert that form only once and not no every commendable post)

Then use a CRED API code to:
- Update the parent Field of the new contact post with the post that is currently commented
- Update the field to whom to send a notification with the author email of the commented post.
https://toolset.com/documentation/user-guides/cred-api/#csd

Example for updating the parent field and set the Email of the recipient (author of parent Post):

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==ID)
    {
 
        //get the current post ID (advertensie)
        $current_post_ID = $form_data["container_id"];
 
        //Upodate the Contact form parent post ID field value
        update_post_meta($post_id, '_wpcf_belongs_your-post-slug_id', $current_post_ID);
  
        //get the parent's post author
        $author_id = get_post_field( 'post_author', $current_post_ID );
  
        //get the author's data
        $user_info = get_userdata($author_id);
  
        //get the authors email
        $author_id_email = $user_info->user_email;
  
        //update the current posts email field:
        update_post_meta($post_id, 'wpcf-email', $author_id_email);
  
     
    }
}

Your Notification on CRED is set to be sent to the Email determined in the Custom Email field wpcf-email

When the recipient wants to reply, he does it with a similar setup, just now he Creates a post of type "Reply", which is child of the current "Contact"

You can display all messages in a View by displaying Replies that are child of the current Contact, in a post date order / DESC, so the newest message will always be on top.

It requires the CRED API code but this is a supported and native API Code, which will not brake and you will understand after a few trials.

This here is just a general overview, if you need further help we can go step by step.

I suggest you give a try and come back at me when you need more help.

#374388

Hey Beda, I sent a message but it didn't save, so I'll type it again:

for the _wpcf_belongs_your-post-slug_id , I have many different post types that the form could be displayed on. How could I get it to auto-detect the post type that the form is displayed on? I tried doing this with an if statement but it didn't work:

//*Messaging System
add_action('cred_save_data', 'my_save_data_action_1',10,2);
function my_save_data_action_1($post_id, $form_data)
{
    // If a specific form
    if ($form_data['id']==92)
    {
  
        //Get the current post ID (On Current Post)
        $current_post_ID = $form_data["container_id"];
  
        //Update the Contact form parent post ID field value
        if('product' == get_post_type){
		update_post_meta($post_id, '_wpcf_belongs_product_id', $current_post_ID);
		}
		elseif('apartment' == get_post_type){
		update_post_meta($post_id, '_wpcf_belongs_apartment_id', $current_post_ID);
		}
		elseif('listing' == get_post_type){
		update_post_meta($post_id, '_wpcf_belongs_listing_id', $current_post_ID);
		}
		elseif('exchange' == get_post_type){
		update_post_meta($post_id, '_wpcf_belongs_exchange_id', $current_post_ID);
		}
		elseif('organizer' == get_post_type){
		update_post_meta($post_id, '_wpcf_belongs_guides_id', $current_post_ID);
		}
		elseif('product' == get_post_type){
		update_post_meta($post_id, '_wpcf_belongs_organizer_id', $current_post_ID);
		}
		elseif('jobs' == get_post_type){
		update_post_meta($post_id, '_wpcf_belongs_jobs_id', $current_post_ID);
		}
		elseif('items' == get_post_type){
		update_post_meta($post_id, '_wpcf_belongs_items_id', $current_post_ID);
		}
        //get the parent's post author
        $author_id = get_post_field( 'post_author', $current_post_ID );
   
        //get the author's data
        $user_info = get_userdata($author_id);
   
        //get the authors email
        $author_id_email = $user_info->user_email;
   
        //update the current posts email field:
        update_post_meta($post_id, 'wpcf-to-email', $author_id_email);
   
      
    }
}

I'm guessing it's because each statement would have to reload the post id first. It works without the IF and for a single post type. Not sure how to get this though. Any help would be appreciated.

Also, when I am doing the 'replies' as a child post of 'messages', do I need to do another hook to get the email of the replier, as well as insert the email of the person the reply is being sent to?

Thanks!

#374548

1. I have many different post types that the form could be displayed on.

Remember that just displaying the form on different post type won't make it child of the post type.
This would need to be set in Types > Post Types > your_post_type > Parent / Child Relationships

2. How could I get it to auto-detect the post type that the form is displayed on?

In the CRED API Code using the $form_data

$type = $form_data['post_type']

Then simply use IF statements to update the correct _wpcf_belongs_your-post-type_id

But since you seem versed in PHP 🙂 I would like to suggest a cooler approach - though you will need to test it as it's not documented.

//Get the ID of the Post where the CRED is in (parent post id)
$parent_post_id = $form_data['id'];
//get the post type of the post where the CRED is in (parent post type)
$type = $form_data['post_type'];
//redefine the meta_key for the parent post id in the current edited/created Child Post 
$our_new_wpcf_belongs_parent_id = "_wpcf_belongs_".$type."_id";
//update this field with all data
update_post_meta($post_id, $our_new_wpcf_belongs_parent_id, $parent_post_id);

What we do above is dynamically create a _wpcf_belongs_your-parent-post-slug_id
We replace the your-parent-post-slug with a $variable.
This should automatically (according to the Post Type where the Form is in) create meta_keys like:
_wpcf_belongs_current-parent-type_id

Please let me know if the above solution works for you, I look forward to your reply!

Thank you for your patience.

#374905

Hey Beda,

Thanks for that. I entered your code, but it didn't work at first. I looked at code you helped me out with before and found out why.
This line:

$parent_post_id = $form_data['id'];

Should be:

$parent_post_id = $form_data['container_id'];

Because it's on the page of the parent post.

Making the full code:

//*Messaging System
add_action('cred_save_data', 'my_save_data_action_1',10,2);
function my_save_data_action_1($post_id, $form_data)
{
    // If a specific form
    if ($form_data['id']==92)
    {
		//Get the ID of the Post where the CRED is in (parent post id)
		$parent_post_id = $form_data["container_id"];
		//get the post type of the post where the CRED is in (parent post type)
		$type = $form_data['post_type'];
		//redefine the meta_key for the parent post id in the current edited/created Child Post 
		$our_new_wpcf_belongs_parent_id = "_wpcf_belongs_".$type."_id";
		//update this field with all data
		update_post_meta($post_id, $our_new_wpcf_belongs_parent_id, $parent_post_id);
    }
}

I still need to pull the e-mail address of the author of the post in which the CRED form is on, as well as the e-mail of the person sending the inquiry message, so the author gets a notification, and a reply can be made, which will then give the person interested a notification. I also need to make any replies created children of the initial message. Finally, I plan on only letting logged-in users submit forms and allow non-logged in users to register or login with a conditional view. The research continues!

#374923

Hello again,

Before you respond, I answered about half of that last question. I'm now able to pull all of the information of both the user submitting the first form, and the author of the parent post with this code:

    // If a specific form
    if ($form_data['id']==92)
    {
		//Get the ID of the Post where the CRED is in (parent post id)
		$parent_post_id = $form_data["container_id"];
		//get the post type of the post where the CRED is in (parent post type)
		$type = $form_data['post_type'];
		//redefine the meta_key for the parent post id in the current edited/created Child Post 
		$our_new_wpcf_belongs_parent_id = "_wpcf_belongs_".$type."_id";
		//get author of parent post id
		$listing_post = get_post( $parent_post_id );
		$advertiser_id = $listing_post->post_author;
		$advertiser_data = get_userdata( $advertiser_id );
		//Get author of CRED Form
		$sender_id = get_current_user_id();
		$sender_data = get_userdata( $sender_id );
		//update this field with all data
		update_post_meta($post_id, $our_new_wpcf_belongs_parent_id, $parent_post_id);
		//associate information about the advertiser and the listing in the message
		update_post_meta( $post_id, 'wpcf-message-to', $advertiser_id );
		update_post_meta( $post_id, 'wpcf-to-email', $advertiser_data->user_email );
		update_post_meta( $post_id, 'wpcf-to-displayname', $advertiser_data->display_name );
		update_post_meta( $post_id, 'wpcf-to-firstname', $advertiser_data->first_name );
		update_post_meta( $post_id, 'wpcf-message-from', $sender_id );
		update_post_meta( $post_id, 'wpcf-from-email', $sender_data->user_email );
		update_post_meta( $post_id, 'wpcf-from-displayname', $sender_data->display_name );
		update_post_meta( $post_id, 'wpcf-from-firstname', $sender_data->first_name );
		
    }
}

Now it's time to start the code for the replies... Am I supposed to just use a create child link CRED form for this? Where should I begin?

#375022
replies to messages.jpg

OK, nevermind, I got it. =)
All I needed to do for the Replies post type was add this bit of code to make it a child of the message post type:

//*Link all replies to the original message
function my_save_data_action_3($post_id, $form_data)
{
    // if a specific form has id of 259
    if ($form_data['id']==259)
    {
        //get current post/page ID (where CRED is inserted to)
        $current_post_ID = $form_data["container_id"];
        //Update the _wpcf_belongs_message_id field of newly created reply
        update_post_meta($post_id, '_wpcf_belongs_message_id', $current_post_ID);
    }
}
add_action('cred_save_data', 'my_save_data_action_3',10,2);

The code above works when the create reply post CRED form is inserted into a view or template. Not sure if it works when it's by itself somewhere.

After that, I made a view for 'replies' ('replies to messages') first, which has the filters:
1. Select posts that are a children of the Post set by parent View. (makes sure it only shows replies with the same 'message' parent)
2. Select items with field:
Message To is a number equal to VIEW_PARAM(user) (the custom field, message-to equals the current user id)
OR
Message From is a number equal to VIEW_PARAM(user) (the custom field, message-from equals the current user id)

So make sure you defined those fields on your message CPT.

After that, make the view for your Message Single Post and insert the original message fields, PLUS the 'replies to messages' view from above.

Thanks for all your help, Beda. Hope this info is useful!

#375025

Lastly, if you want to use any fields from the original listing or whatever the 'message' is the child of, you can use this code instead of the one I posted above at this time: March 11, 2016 at 12:55 pm.

The code below will insert the parent post ID into the 'messages' custom field 'listing-id' (make sure you have created that first).

//*Messaging System
add_action('cred_save_data', 'my_save_data_action_1',10,2);
function my_save_data_action_1($post_id, $form_data)
{
    // If a specific form
    if ($form_data['id']==92)
    {
		//Get the ID of the Post where the CRED is in (parent post id)
		$parent_post_id = $form_data["container_id"];
		//get the post type of the post where the CRED is in (parent post type)
		$type = $form_data['post_type'];
		//redefine the meta_key for the parent post id in the current edited/created Child Post 
		$our_new_wpcf_belongs_parent_id = "_wpcf_belongs_".$type."_id";
		//get author of parent post id
		$listing_post = get_post( $parent_post_id );
		$advertiser_id = $listing_post->post_author;
		$advertiser_data = get_userdata( $advertiser_id );
		//Get author of CRED Form
		$sender_id = get_current_user_id();
		$sender_data = get_userdata( $sender_id );
		//update this field with all data
		update_post_meta($post_id, $our_new_wpcf_belongs_parent_id, $parent_post_id);
		//update the field listing id so we can use it to display title/link/etc
		update_post_meta($post_id, 'wpcf-listing-id', $parent_post_id);
		//update information regarding author of the original parent post
		update_post_meta( $post_id, 'wpcf-message-to', $advertiser_id );
		update_post_meta( $post_id, 'wpcf-to-email', $advertiser_data->user_email );
		update_post_meta( $post_id, 'wpcf-to-displayname', $advertiser_data->display_name );
		update_post_meta( $post_id, 'wpcf-to-firstname', $advertiser_data->first_name );
		//update information regarding CRED form sender
		update_post_meta( $post_id, 'wpcf-message-from', $sender_id );
		update_post_meta( $post_id, 'wpcf-from-email', $sender_data->user_email );
		update_post_meta( $post_id, 'wpcf-from-displayname', $sender_data->display_name );
		update_post_meta( $post_id, 'wpcf-from-firstname', $sender_data->first_name );
		
    }
}

*Note: Make sure you have created all of the appropriate custom fields for the post type 'messages' before you use this code. Anything with 'wpcf-bla-bla' is a custom field. The custom field slug you set in Types should be everything you see after the 'wpcf-', so 'wpcf-from-firstname' in the code is the slug 'from-firstname' in types.

Now that you have the listing ID, you can use it in the view for both your single message and message archives to let the user know where the message came from... For example:

Messages About: [wpv-post-link id="[types field='listing-id' format='FIELD_VALUE'][/types]"]

Lets the user know that the messages and replies in this loop are all about xx listing, item, etc. Use this when you have multiple post types that are parents to messages. Otherwise, you can just call the post type directly in the view (see this link - https://toolset.com/documentation/user-guides/displaying-fields-of-parent-pages/)

#375107

Update! The code posted for the reply messages wasn't enough for what I want to do - which is sending the user an email when a reply is posted - so more custom fields need to be made to grab user info in order to use them in the CRED form notifications. This makes the code much heavier. If there's a better way to write this, please post, but this is what I did.

//*Link all replies to their messages
function my_save_data_action_3($post_id, $form_data)
{
    // if a specific form ID
    if ($form_data['id']==259)
    {
        //get current post/page ID (where CRED is inserted to)
        $current_post_ID = $form_data["container_id"];
		//Get author of CRED Form
		$r_sender_id = get_current_user_id();
		$r_sender_data = get_userdata( $r_sender_id );
		$message_post = get_post( $current_post_id );
		$original_messager_id = $message_post->post_author;
		$original_messager_data = get_userdata( $original_messager_id );
		$original_advertiser_id = get_post_meta($current_post_ID, _wpcf_message_to, true);
		$original_advertiser_data = get_userdata( $original_advertiser_id );
		//Check to see if sender is author of the original message and update fields accordingly
		if ($original_messager_id==$r_sender_id)
		{
		update_post_meta( $post_id, 'wpcf-reply-from', $r_sender_id );
		update_post_meta( $post_id, 'wpcf-r-from-display', $r_sender_data->display_name );
		update_post_meta( $post_id, 'wpcf-reply-to', $original_advertiser_data );
		update_post_meta( $post_id, 'wpcf-reply-to-email', $original_advertiser_data->user_email );
		update_post_meta( $post_id, 'wpcf-r-to-first', $original_advertiser_data->first_name );
		}
		else
		{
		update_post_meta( $post_id, 'wpcf-reply-from', $r_sender_id );
		update_post_meta( $post_id, 'wpcf-r-from-display', $r_sender_data->display_name );
		update_post_meta( $post_id, 'wpcf-reply-to', $original_messager_id );
		update_post_meta( $post_id, 'wpcf-reply-to-email', $original_messager_data->user_email );
		update_post_meta( $post_id, 'wpcf-r-to-first', $original_messager_data->first_name );		
		}
        //Update the _wpcf_belongs_message_id field of newly created reply
        update_post_meta($post_id, '_wpcf_belongs_message_id', $current_post_ID);
		//update information regarding CRED form sender

		
    }
}
add_action('cred_save_data', 'my_save_data_action_3',10,2);

Since only the user sending the reply (get_current_user_id();) is known, we need to find out who the email notification should be sent to: namely, the person that is not the current user. To figure this out, we have to do a conditional statement. I used the author of the message post (which is a parent to replies) to see who the current user is.

IF the current user is the author of the message post, the original advertiser (which is the person owning the grandfather 'listing') will be the recipient of any notifications.

ELSE - so if the current user is not the author of the message post, the recipient must be the author of the message post (since this is only a two way system).

#394457

Our site is live now and through beta testing we found that for some reason the replies code wasnt working. Maybe it had something to do with the $current_post_ID variable I was using. Not sure. But below is the final code we used:

//*Link all replies to their messages
function my_save_data_action_3($post_id, $form_data)
{
    // if a specific form ID
    if ($form_data['id']==108)
    {
        //get current post/page ID (where CRED is inserted to)
        $message_id = $form_data["container_id"];
		//Get author of CRED Form
		$r_sender_id = get_current_user_id();
		$r_sender_data = get_userdata( $r_sender_id );
		$message_post = get_post( $message_id );
		$original_messager_id = $message_post->post_author;
		$original_messager_data = get_userdata( $original_messager_id );
		$original_advertiser_id = get_post_meta($message_id, _wpcf_message_to, true);
		$original_advertiser_data = get_userdata( $original_advertiser_id );
		//Check to see if sender is author of the original message and update fields accordingly
		if ($original_messager_id==$r_sender_id)
		{
		update_post_meta( $post_id, 'wpcf-reply-from', $r_sender_id );
		update_post_meta( $post_id, 'wpcf-r-from-display', $r_sender_data->display_name );
		update_post_meta( $post_id, 'wpcf-reply-to', $original_advertiser_data );
		update_post_meta( $post_id, 'wpcf-reply-to-email', $original_advertiser_data->user_email );
		update_post_meta( $post_id, 'wpcf-r-to-first', $original_advertiser_data->first_name );
		}
		else
		{
		update_post_meta( $post_id, 'wpcf-reply-from', $r_sender_id );
		update_post_meta( $post_id, 'wpcf-r-from-display', $r_sender_data->display_name );
		update_post_meta( $post_id, 'wpcf-reply-to', $original_messager_id );
		update_post_meta( $post_id, 'wpcf-reply-to-email', $original_messager_data->user_email );
		update_post_meta( $post_id, 'wpcf-r-to-first', $original_messager_data->first_name );		
		}
        //Update the _wpcf_belongs_message_id field of newly created reply
        update_post_meta($post_id, '_wpcf_belongs_message_id', $message_id);
		//update information regarding CRED form sender

		
    }
}
add_action('cred_save_data', 'my_save_data_action_3',10,2);
This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.