This is the technical support forum for WPML - the multilingual WordPress plugin.

Everyone can read, but only WPML clients can post here. WPML team is replying on the forum 6 days per week, 22 hours per day.

This topic contains 10 replies, has 3 voices.

Last updated by sarah.n 5 years, 2 months ago.

Assigned support staff: sarah.n.

Author Posts
July 7, 2014 at 12:04 pm #431454

mohammedT

Dear All,

Before creating a guest form with CRED I used the following code to automatically duplicate posts to all languages when the user submit his/her post also with another CRED form (but the user in this case was registered and in the form setting, I set the status to “Published”.):

//duplicate posts created with CRED
function classifieds_cred_commerce_after_send_notifications($post_id, $form_data){
    if ($form_data['id']==313 || $form_data['id']==474) {
        classifieds_duplicate_on_publish($post_id);
    }
}

add_action('cred_save_data', 'classifieds_cred_commerce_after_send_notifications',10,2);

//duplicate a post/page/custom post on publish or update
function classifieds_duplicate_on_publish ($post_id) {
    if(function_exists('icl_makes_duplicates')){    
        icl_makes_duplicates($post_id);
    }
}

From here: https://toolset.com/forums/topic/automatically-duplicate-posts-to-all-languages-with-cred/#post-222308

The gust form (In the form setting, I set the status to “Pending Review”.) I use the following code in the functions.php of the current active theme to allow non-registered users to submit their posts without registration

function classifieds_cred_submit_complete_guest_post($post_id, $form_data){

                $user_email = $_POST['guest-email'];
                 
        if (email_exists($user_email) != false) {
            $user_id = email_exists($user_email);
        }
                 
        if ( !$user_id && email_exists($user_email) == false ) {
            $guest_password = '^Z%7g$rrpoYAl#Y6$e9HYNg$i3N7N760%c^T7Tch';
            $user_id = wp_create_user($user_email, $guest_password, $user_email);
            wp_update_user( array ( 'ID' => $user_id, 'role' => 'author' ) ) ;
            wp_set_auth_cookie( $user_id, false, is_ssl() );
        }
        wp_update_post( array ('ID' => $post_id, 'post_author' => $user_id ) );

    }
add_action('cred_submit_complete_474', 'classifieds_cred_submit_complete_guest_post',10,2);

From: https://toolset.com/forums/topic/specifying-author-for-guest-posts/#post-228448

And the CRED guest form code is the following:

[credform class="cred-form"]
[cred_field field="form_messages" value=""]

<div class="cred-label">[wpml-string context="Guest Form" name="Guest Name"]Guest Name[/wpml-string]</div>
<div id="mos-guest-name">
[cred_generic_field field="guest-name" type="textfield" class="" urlparam=""]
{
"required":1,
"validate_format":1,
"persist":1,
"default":""
}
[/cred_generic_field]
</div>
   
 
<div class="cred-label">[wpml-string context="Guest Form" name="Guest Email"]Guest Email[/wpml-string]</div>
<div id="mos-guest-email">
[cred_generic_field field="guest-email" type="email" class="" urlparam=""]
{
"required":1,
"validate_format":1,
"persist":1,
"default":""
}
[/cred_generic_field]
</div>


 
 
<div class="cred-field cred-field-post_title">
<div class="cred-label">[wpml-string context="Guest Form" name="Title"]Title[/wpml-string]</div>

[cred_field field="post_title" post="post" value="" urlparam="" placeholder="Ad Title"]
</div>


 
<div class="cred-field cred-field-post_content">  
<div class="cred-label">[wpml-string context="Guest Form" name="Description"]Description[/wpml-string]</div>

[cred_field field="post_content" post="post" value="" urlparam="" placeholder="Ad Description"]
</div>


   
<div class="cred-label">[wpml-string context="Guest Form" name="Email"]Email[/wpml-string]</div>
<div id="mos-email">
[cred_generic_field field="user_email" type="email" class="" urlparam=""]
{
"required":0,
"validate_format":1,
"persist":1,
"default":""
}
[/cred_generic_field]
</div>
<p>[wpml-string context="Guest Form" name="Email Description"]This email is hidden from public but its allow the user to communicate with you directly from JYDAR[/wpml-string]</p>

<div class="cred-label">[wpml-string context="Guest Form" name="Country"]Country[/wpml-string]</div>
<div id="mos-country">[cred_field field="country" display="select" single_select="true"]</div>


<div class="cred-label">[wpml-string context="Guest Form" name="Category"]Category[/wpml-string]</div>
<div id="mos-main-cat">[cred_field field="category" display="select" single_select="true"]</div>

<div class="cred-label">[wpml-string context="Guest Form" name="Featured Image"]Featured Image[/wpml-string]</div>
<div id="mos-featured">
[cred_field field="_featured_image" value="" urlparam=""]
</div>
   
<div class="cred-label">[wpml-string context="Guest Form" name="Another Image"]Another Image[/wpml-string]</div>
<div id="mos-first_img_att">  
[cred_generic_field field="mos_first_img_att" type="image" class="" urlparam=""]
{
"required":0,
"validate_format":1,
"persist":1,
"default":""
}
[/cred_generic_field]
</div>
   
<div class="cred-label">[wpml-string context="Guest Form" name="Another Image"]Another Image[/wpml-string]</div>
<div id="mos-second_img_att">  
[cred_generic_field field="mos_second_img_att" type="image" class="" urlparam=""]
{
"required":0,
"validate_format":1,
"persist":1,
"default":""
}
[/cred_generic_field]
</div>
   
<div class="cred-label">[wpml-string context="Guest Form" name="Another Image"]Another Image[/wpml-string]</div>
<div id="mos-third_img_att">  
[cred_generic_field field="mos_third_img_att" type="image" class="" urlparam=""]
{
"required":0,
"validate_format":1,
"persist":1,
"default":""
}
[/cred_generic_field]
</div>
   
<div class="cred-label">[wpml-string context="Guest Form" name="Another Image"]Another Image[/wpml-string]</div>
<div id="mos-fourth_img_att">  
[cred_generic_field field="mos_fourth_img_att" type="image" class="" urlparam=""]
{
"required":0,
"validate_format":1,
"persist":1,
"default":""
}
[/cred_generic_field]
</div>

[cred_field field="recaptcha" value="" urlparam=""]

[cred_field field="form_submit" value="Submit" urlparam=""]
[/credform]

Now the guest form doesn’t work correctly (some cases that did not work correctly: the post is saved without Author and no matter what category he/she selected when he/she created the post, the post assigned to Uncategorized category !!) unless commented the previous duplication code.

Please how can I solve that issue. I need duplication code work in all cases(in case of guest form or another CRED forms) without any problems.

I have a staging site so in case you needed an admin access or ftp account just let me know.

Best Regards,
Mohammed

July 7, 2014 at 2:00 pm #431575

Andrey
Supporter

Languages: English (English ) Russian (Русский )

Timezone: Europe/Kiev (GMT+03:00)

Dear Mohammed,

Thank you for contacting to us !

Please upload a screenshot of your WPML > Support page so we can confirm that the latest version of WPML plugins you are using.

>> Now the guest form doesn’t work correctly...

Does it work correctly before something ?

_

With Regards

Andrey

July 7, 2014 at 3:06 pm #431643

mohammedT

Dear Andrey,

This is the link to the gust form: hidden link

Regards,
Mohammed

July 8, 2014 at 8:05 am #432041

Andrey
Supporter

Languages: English (English ) Russian (Русский )

Timezone: Europe/Kiev (GMT+03:00)

Dear Mohammed,

Please fill in your access details below (under comment window), I'll take a look closer on your issue.

Provide me steps in which cases the form doesn't work correctly. How to reproduce issue on your site ?

_

With Regards

Andrey

July 8, 2014 at 9:50 am #432150

mohammedT

Dear Andrey,

Simply the problem is I need the post to be automatically duplicated to all languages when the user publishes or updates it in all CRED forms(guests and other forms) in all cases (in case of the form setting was “Published” or “Pending Review”) without problems.

Some problems that I faced were when I tried many automatically duplication codes are:

*The post duplicated but it was saved without Author.

*No matter what category he/she selected when he/she submitted the post, the post assigned to Uncategorized category!

Thank you in advance.

Best Regards,
Mohammed

July 8, 2014 at 2:27 pm #432434

Andrey
Supporter

Languages: English (English ) Russian (Русский )

Timezone: Europe/Kiev (GMT+03:00)

Dear Mohammed,

I got the access details. I will take a look and update you here as soon as I know more

_

With Regards

Andrey

July 9, 2014 at 8:47 am #432893

Andrey
Supporter

Languages: English (English ) Russian (Русский )

Timezone: Europe/Kiev (GMT+03:00)

Dear Mohammed,

Could you provide the FTP access to root folder of your test installation ? I need to activate WP_DEBUG in wp-config.php

_

With Regards

Andrey

July 9, 2014 at 9:40 am #432968

mohammedT

Dear Andrey,

Sure.

Use the same credentials and check it now.

Cheers,
Mohammed

July 10, 2014 at 6:40 am #433548

sarah.n
Supporter

Languages: English (English ) German (Deutsch )

Hello Mohammed,

This is Sarah here. Andrey forwarded your forum thread to me so I will do my best to reply and offer further assistance.

I consulted one of our Toolset developers regarding your ticket. What followed is her reply:

// **begin of quote**

I think I've managed to create a workaround for the client. Essentially it relies on CRED API function 'cred_submit_complete' to apply a custom function to duplicate the created post. This custom function will only be applied for specific CRED form IDs. And also calls upon the icl_makes_duplicates() function from WPML API functions to actually create the duplicated posts.
Some adjustments needed to be done in the my_icl_duplicate_on_publish() so that the categories selected in the CRED form could also be duplicated as this was not the case if the post created had the 'pending' status.

The custom code added to the functions.php file of the current theme in use:

/* start: WPML Support forum */
//duplicate posts created with CRED
add_action('cred_submit_complete', 'duplicate_posts_on_cred_submit_complete',11,2);
function duplicate_posts_on_cred_submit_complete($post_id, $form_data){
    if ($form_data['id']==313 || $form_data['id']==474) {
        my_icl_duplicate_on_publish($post_id);
    }
}
//duplicate a post/page/custom post on publish or update
function my_icl_duplicate_on_publish ( $post_id ) {
    if(function_exists( 'icl_makes_duplicates' )){
        global $sitepress, $iclTranslationManagement;
        if (is_object($sitepress)) {
            $sitepress->icl_translations_cache->clear();
        }
		$post_status = get_post_status( $post_id );
		if ( 'pending' == $post_status && is_object( $iclTranslationManagement )) {
			wp_update_post( array( 'ID' => $post_id, 'post_status' => 'private' ) );
		}
        icl_makes_duplicates( $post_id );
		if ( 'pending' == $post_status && is_object( $iclTranslationManagement )) {
			wp_update_post( array( 'ID' => $post_id, 'post_status' => $post_status ) );
			$duplicates = $iclTranslationManagement->get_duplicates( $post_id );
			foreach ( $duplicates as $lang => $_pid ) {
				wp_update_post( array( 'ID' => $_pid, 'post_status' => $post_status ) );
			}
		}
    }
}
/* end: WPML Support forum */

The following function classifieds_cred_submit_complete_guest_post() was also corrected/updated to resolve a PHP warning for variable not defined that it was giving and also added a wp_set_current_user() function call that was needed to WPML duplicate API function. So the corrected function would be:

add_action('cred_submit_complete_474', 'classifieds_cred_submit_complete_guest_post',10,2);

function classifieds_cred_submit_complete_guest_post($post_id, $form_data){
		//$_SESSION['classifieds_msg_post_id'] = $post_id;
		//$user_email = get_post_meta($post_id, 'guest-email', true);
                $user_email = $_POST['guest-email'];
                //$user_name = $_POST['guest-name'];
		if (email_exists($user_email) != false) {
			$user_id = email_exists($user_email);
		}
                
		if ( empty($user_id) && email_exists($user_email) == false ) {
			$guest_password = '^Z%7g$rrpoYAl#Y6$e9HYNg$i3N7N760%c^T7Tch';
			$user_id = wp_create_user($user_email, $guest_password, $user_email);
			wp_update_user( array ( 'ID' => $user_id, 'role' => 'author' ) ) ;
			wp_set_current_user( $user_id );
			wp_set_auth_cookie( $user_id, false, is_ssl() );
		}
		//update_post_meta($post_id, 'wpcf-message-from', $user_id);
		wp_update_post( array ('ID' => $post_id, 'post_author' => $user_id ) );
		//duplicate post to WPML translation
		//classifieds_duplicate_on_publish($post_id);
	}

I've already tested the above solution in the client's staging site, worked ok to me.

//\ **end of quote**

Could you please run a test on your end and let us know if all is working as you expect them to work now?

Looking forward to your feedback. Thanks!

July 10, 2014 at 11:15 am #433775

mohammedT

Hello Sarah,

Thank you very very much.

I love your plugins and your great support.

Everything worked just great.

I think great company is the one that offers great support to there clients, like you.

I thought my issue will never be solved, but you are really an amazing team.

Thank you Andrey,

Thank you Sarah,

And deliver my thanks to your developers.

Best Wishes,

Mohammed

July 10, 2014 at 11:25 am #433784

sarah.n
Supporter

Languages: English (English ) German (Deutsch )

You are most welcome Mohammed 🙂