Skip Navigation

[Resolved] AJAX not working correctly with WPML

This support ticket is created 3 years, 8 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
9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 - - 9:00 – 13:00
14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 - - 14:00 – 18:00

Supporter timezone: Africa/Casablanca (GMT+00:00)

This topic contains 60 replies, has 3 voices.

Last updated by simonM-5 3 years, 6 months ago.

Assisted by: Jamal.

Author
Posts
#1717461

Hi Jamal

I created you a two new Ads using the logins I sent you - a Nanny Ad, created in English, and a Job Ad created in German, so you can see how they behave differently. But please create/edit/delete as many as you like.

Kind regards
Simon

#1717505

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+00:00)

Thank you Simon,

I can see now, the Ads icon on the top navigation and I am able to edit an Ad.
I'll work on it and get back to you as soon as possible.

Best regards,
Jamal

#1718153

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+00:00)

Hello Simon,

I can see now how your workflow is.
Nannies and Families can :
- Create their ads using Toolset Forms.
- Display their ads using views.
- Views loops use content templates that display an edit link tight to a specific content template.
- A post can then be edited on that content template.

Because of the way WPML works, we'll have to make sure that a post is always edited on the original language where it was created. And that any of its duplicates do not get updated from the frontend.
I am thinking that we can use two custom functions/shortcodes to make sure we'll always be updating the original post. That way WPML will sync the update with its duplicates safely.
We'll use these functions for:
- Check if the current post is a duplicate. If yes, display a message saying that we must edit the original post with a link for it
- Get the original post to create an edit link and allow the user to edit the original post/ad

I still prefer to get this working on a simple setup before implementing this on your dev server.
Unfortunately, I am having several issues on the online site that I shared before. And on a clean setup locally. And it took me too much time without any progress. I will need to approach our 2nd Tier for assistance before getting back to you.

#1718199

HI Jamal

Thank you for all your effort. You have no idea how appreciative we are of your assistance as this fundamental issue (which is critical for the success of our site) has been bogging us down for a while. We even considered giving up using Toolset and WPML because of it, but I get the strong feeling you will manage to find us a solution.

I agree - it is better to get it working in a scaled down scenario first before applying the code to our site.

Just for my understanding - Are all originals EN and all duplicates DE because our "primary" language is set to EN and our secondary DE? Or is the original the language in which it was created and the duplicate in the other language?

We also noticed that the view results seem to order first by the specified criteria then by the language. So the system seems to know which Ads are "German" and which are "English" or maybe it is "primary" and "non-primary". I don't want to throw a spanner in the works now or add issues to an existing ticket, but it might be useful information to keep in the back of your mind while considering your solutions to the issue. Any other sort criteria specified by a user on the front end other than Last Modified Date or Post Date appears to behave in this way..

But one step at a time ... 🙂

Have a great weekend and hope to hear more good news soon!

Thanks and kind regards
Simon

#1720545

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+00:00)

Thank you Simon,

I think I got a solution. It consists of duplicating posts when created and then enforcing the user to edit only the original post, and not the duplicate.

The original post is the first post created by the user, be it English or German. The duplicate post is the one created automatically by the custom code:

function quick_duplicate_save_data_action( $post_id, $form_data ){
        // Change your CRED Form "ID" accordingly below
        if ($form_data['id']==19){
 
            // Use WPML API   
            do_action( 'wpml_make_post_duplicates', $post_id ) ;
        }      
}    
add_action( 'cred_save_data', 'quick_duplicate_save_data_action', 10, 2 );

To enforce the user to edit only the original post, I came up with a custom function that will be used in conditional shortcodes to display the "Edit link" only if the post is original. Because below what happens:
- If the original post is updated, WPML will propagate the update to the posts duplicates.
- If the duplicate post is updated, the original post does not get the update automatically. And once it is updated(the original) it will override the updates on the duplicate.
This is the function:

function ts_is_post_wpml_original( $post_id ){
	$args = array('element_id' => $post_id, 'element_type' => 'ad' );
	$info = apply_filters( 'wpml_element_language_details', null, $args );
	if ($info->source_language_code !== null) {
		return 0;
	} else {
		return 1;
	}
}

It will need to be registered in Toolset->Settings->Front-end Content->Functions inside conditional evaluations. Then you can use it in the loop content template as follow:

[wpv-conditional if="( ts_is_post_wpml_original() eq '1' )"]
Edit link shortcode goes here.
[/wpv-conditional]

I'll let you implement this on your website. Then test a couple of posts in both languages. We can then continue on other questions such as the order, which I think is related to the creation or modification date.

You may need to run the WPML troubleshooting action "Assign language information" to attach language information to existing ads. Then you can bulk duplicate those post in WPML->Translation Management.

Below some links from Toolset and WPML documentation that are related to our case:
- https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
- https://wpml.org/wpml-hook/wpml_make_post_duplicates/
- https://wpml.org/wpml-hook/wpml_element_language_details/

Thank you Simon for your patience. Let me know what results in you will get from your end and we'll continue on.

#1722303

HI Jamal

Thanks for this. Here's what I did and my results:

1) I believe your first piece of code is just to create the duplicates when a user saves the Form with that particular ID. Minesh had already provided us with code in previously mentioned ticket (https://toolset.com/forums/topic/split-posts-created-updated-in-2nd-language-only-visible-in-2nd-language-update-post-form-with-wpml-with-translations/page/2/). Stored as code snippet "duplicate-posts-all-languages"
- it already had the code in the form of an array for multiple forms, so I didn't add this code again. Minesh's code seems to duplicate the original posts in the other languages successfully already.

2) I added the function ts_is_post_wpml_original to Custom Code and registered it manually under Toolset > Settings > Front-end Content > Functions inside conditional evaluations.

3) Added the conditional test to the Edit Links in "Edit My Nanny Ads View" and "Edit My Job Ads View"

4) In WMPL Settings, set both Ads post types to Translate

5) Deleted all previous Ads, and created one in EN and one in DE

6) Click the Ads icon in menu to expose the view of existing Ads. Here I'm seeing the following error message:

"Notice: Trying to get property 'source_language_code' of non-object in /var/www/kle1116-204/apps/dev/wp-content/toolset-customizations/function-ts-is-post-wpml-original.php on line 15"

7) Deactivated Minesh's code for duplication (snippet "duplicate-posts-all-languages") to test if that was the issue and activated your code with Post Form (929), name of the snippet is "function-quick-duplicate-save-data-action"

8) Deleted all previous Ads, and created another Job Ad in EN and another in DE.

9) Seeing the same error message, once per Ad. I conclude here that Minesh's code would have sufficed for the duplication and that we're seeing a different issue here.

10) My next suspicion was that your conditional code to test if the wpml is an original or a dup would only work if I am only displaying the single post, instead of a view showing all Ads.
- so I added the edit link to the Job Ad - single Content Template and viewed the single Job Ad
- unfortunately the edit link is visible regardless of whether it was an original or a dupe (ie button still visible in both languages) and still seeing the error message on the single Job Ad.

a) Please advise me if we can use Minesh's code for duplication. It is slightly different to yours in that it includes the statement global $sitepress;
b) At this stage we can delete all Ads as we please so we don't need to worry about existing Ads
c) What would be your next suggestions?

Thank you and best regards
Simon

#1722649

Hi Jamal

Further to my previous update, I have just noticed that the field "Show in Search Results" (family-show-in-search-results (Job Ad) and nanny-show-in-search-results (Nanny Ad)) is not being duplicated correctly on submission, even although it is pre-defaulted to Yes to ensure it is always not null on submission. It is curious that all other fields are copying across to the duplicates except this one.

Kind regards
Simon

#1722943

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+00:00)

Hello Simon,

I think I got it fixed, at least for the "Job Ads" 🙂 Check this screenshot hidden link

The error was actually erroneous. You are right, the current code should only work in the post's single page. But it did not work because of the wrong post type slug. In my test environment, I have used "ad" and on your website, you are using "job-ad" and "nanny-ad".

My conditional code was erroneous as it does not pass the post id to the function, this is the correct post:

[wpv-conditional if="( ts_is_post_wpml_original([wpv-post-id]) eq '1' )"]
Edit link shortcode goes here.
[/wpv-conditional]

Before finding out that there is a difference in the custom post slug, I have also created a shortcode. For a shortcode to be used in conditions, it has to be registered in Toolset->Settings->Front-end Content, too. Just like we registered the function.

At this stage, the code is only handling "Job Ads", to handle "Nanny Ads", we need to update our code, or to create a new function that looks if a "nanny-ad" is original or not.

I hope this makes sense. If yes, I'll let you create another function to handle "Nanny Ads". If you find any troubles, let me know and I'll prepare it for you 😉

Please note that I have:
- Changed the password for the user nativefamily6.
- Installed the plugins "User Switching" and "File Manager".
- Renamed the debug.log file, because it was too big.

Best regards,
Jamal

#1723663

Hi Jamal

Sorry quick update, I am away for a few days break and will probably only get to this on Sunday evening. I'm annoyed with myself because I spotted that slug was wrong and forgot to change it 😀

I will update you again on Sunday night! But so far everything going in the right direction! 😉

Best regards
Simon

#1727073

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hi Simon,

Jamal is on vacation. This is Minesh here and I will handle take care of this ticket. Hope this is OK.

Pleas test the solution shared by Jamal and let me know your feedback. If everything is working, feel free to close the ticket.

#1732773

HI Minesh

1) Apologies for the long delay in my response. Since the last update some things have stopped working, but I'll prepare a qualified response this morning and get some feedback to you. .
2) When is Jamal back from holidays?

Thanks and regards
Simon

#1732971

Hi Minesh

It seems there is a general problem with the ticket support system.

1) Normally when I click on Support > Technical Support, I see all my open tickets. Currently only seeing one, instead of the 4 I currently have open.
2) It is not currently possible for me to start a new ticket.

Regards
Simon

#1734301

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+00:00)

Hi Simon! I hope you had a nice time during your break.

Please add screenshots about the issue you are encountering on our forum and I'll check with our system team about both errors(opening a new ticket, missing tickets on your profile).

If you are still unable to open a new ticket, please explain what you need in a separate reply and I'll split it into a separate ticket.

Let's focus on this ticket about our original issue(AJAX filtering and WPML support) and let me know if you still need assistance with it.

Best regards,
Jamal

#1735063

Hi Jamal

Nice to have you back and apologies for the delay again. I wasn't on holiday - it was family-related, but I'm back now. I hope you had a good break yourself! So, since your last update, we are seeing the following behaviour for Job Ads:

- The Job Ad appears to be duplicated successfully.

- The Edit Ad functionality appears to work as intended, ie link (button) to edit the Ad is only shown when viewing the site in the language in which the Ad was created in the View edit-my-job-ads-view.

- Editing the Job Ad appears to copy all the information correctly to its duplicates.

- The Search (find-a-job-search-and-results-view) appears to work with or without AJAX in both languages - is that what you would expect? That surprised me actually.

- The Sort By criteria are not working correctly, with the exception of Last Modified, which is using Toolset custom code. Currently it only works with AJAX off, however Minesh provided a version that should work with AJAX on too. According to Minesh Toolset is already working on the bug with Last Modified.

- The Favourites plugin is having issues consequently - I believe this is because it is adding the favourite flag "1" only to the record of the language in which it is being viewed. When viewing the Job Ads CPT in the back end, I can see the Favourites flag is flipped from 0 to 1 only for the language in which it was set. We need to ensure the flag is set in all languages.

I think it is wise to first get everything working with Job Ads including the search/sorting, before extending all the code to Nanny Ads. Would you agree?

Regarding the tickets, I can now create a new one, but I'm still not seeing all my open tickets so I'll send screenshots in a new ticket. Thanks!

Kind regards
Simon

#1735737

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+00:00)

Hi Simon, thank you! It was a nice break. And I am very happy we are progressing with this. We'll get it handled at the end 🙂

Can you please provide more details about the following points:
- The Sort By criteria are not working correctly, with the exception of Last Modified, which is using Toolset custom code. Currently, it only works with AJAX off, however Minesh provided a version that should work with AJAX on too. According to Minesh Toolset is already working on the bug with Last Modified.
Please give me an example that will show me what filters are not working, I'll analyze it and find out why. Maybe that's happening only with custom field filters, or only with taxonomy filters?
Minesh is on vacation, can you point me to his reply, so I can check which bug exactly he was referring to?

- The Favourites plugin is having issues consequently - I believe this is because it is adding the favourite flag "1" only to the record of the language in which it is being viewed. When viewing the Job Ads CPT in the back end, I can see the Favourites flag is flipped from 0 to 1 only for the language in which it was set. We need to ensure the flag is set in all languages.
As I am not yet accustomed to this plugin, can you give more details? How do you switch the flag?
Maybe we can have a WPML configuration to make the plugin sync the value across languages https://wpml.org/documentation/support/language-configuration-files/#custom-fields
But, we'll also ensure that this is always being done in the original language of the "Ad/post" or it will need custom code too. We'll find out.

I think it is wise to first get everything working with Job Ads including the search/sorting, before extending all the code to Nanny Ads. Would you agree?
I agree. Let's do it.

Looking forward to your reply.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.