Skip Navigation

[Fermé] How can I generate CRED form inputs with data from a non wordpress MySQL table?

This support ticket is created Il y a 8 années et 6 mois. 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.

This topic contains 11 réponses, has 2 voix.

Last updated by Bigul Il y a 8 années et 6 mois.

Assigned support staff: Bigul.

Auteur
Publications
#188737

Hi guys,

I'm due to use the toolset to create a website where people pay to advertise their cars for sale. Instead of having the user to manually input all the specifications for their car (such as engine cylinders, number of doors, length, fuel capacity etc) we have acquired a MySQL database of car data from hidden link to provide an easier and more automated service.

If you look at the demo here: hidden link (the demo just uses javascript for everything) we want to use the same kind of functionality where the user will select the year, make, model and trim (with ajax) to select the exact car model they are selling which therefore pre-fills all of the respective input fields for their car advertisement listing (but still allowing the user to change the fields' input if they desire).

How could this be possible with CRED?

Many thanks for pointing me in the right direction.

Aaron

#189117

Bigul
Supporter

Dear Aaron,

CRED can only create or edit content for posts. Therefore you have to do custom coding to achieve this task. May following idea will help you.

You can relate the Car Data to a Custom Post Type Car with specific custom fields for each information. It will help you to handle the data as WordPress posts. Then you can
create a view to search and list the data. The data should be list in the form of CRED edit form. Then user can edit and submit the form.

You can contact our custom development team for custom coding services hidden link

--
With Regards

Bigul

#189193

Hi Bigul,

Thanks for your suggestion, it was most helpful. I have gone ahead and made a 'cars' custom post type using my original database data, with this post type I have added custom fields such as car yea, car make, car body style etc.

So I can now use these car posts as a template for users to input their car adverts. What I now need to know is how I can use the CRED edit form of this post type 'cars' to then save the car as a new post type called 'car_adverts' which will use the same respective fields as the 'cars' post type but with the addition of a couple of advert specific fields such as 'advert-end-date' and 'is-sold'. How could I do this? Is there a hook I could use to make it so when a 'cars' post type is using a CRED edit form, instead of updating that 'cars' post when submitted, it uses the data to create a new post under 'car_adverts'? So the 'cars' post type is just going to be used as a template of data for each respective car.

Many thanks,
Aaron

#189198

Looking at it further, I think I can achieve what I want by using the hook cred_before_save_data, I could then insert the forms data into a new post type in car-adverts. But how in this hook could I stop the original edit form from saving it's data?

#189559

Bigul
Supporter

Dear Aaron,

Our developer is looking for a easy solution and we will try to get back to you as soon as possible. Please wait.

--
With Regards

Bigul

#190428

Bigul
Supporter

Dear Aaron,

Please try the following and let me know your feedback. It is working for us.

1. create two custom post types 'cars' and 'car-adverts'. One important think to consider when creating these custom post types is to establish a post relationship where 'cars' is parent to 'car-adverts'
2. create the default custom fields and associate to both custom post types
3. populate the cars and corresponding custom fields with the information
4. create a view with a parametric search that would list cars and allow for front-end filtering
5. create a CRED form to create content for the 'car-adverts' custom post type and add all the custom fields you want to have associated to this custom post in the form
6. create a page and add the shortcode for the CRED form that will create the 'car-adverts'
7. in the view with a parametric search that will list cars add for each post a cred child link that will redirect to the page created in the previous point and allow for the 'car-adverts' post to be created

So far we have 'cars' and can search those and associate then with a 'car-advert'. This because of the view and the cred create child content link.
Now for the duplication/copying of content from 'cars' to 'car-adverts'.

For this we will create a custom shortcode for retrieving the information for each field from the parent post 'Cars' and pass it to the children post the 'car-adverts'. Note that we will retrieve the id of the post 'cars' selected from the url parameter automatically generated when the cred link is created.
(hidden link)

8. add the following to your function.php file

add_shortcode('wpv-post-id-from-url', 'my_wpv_post_id_from_url_func');
function my_wpv_post_id_from_url_func($atts, $content = '') {
    extract( shortcode_atts( array(
		'field' => '',
	), $atts ) );
	$out = '';
	if (isset($_GET['parent_cars_id'])) {
		$parent_id = intval($_GET['parent_cars_id']);
	}
	switch ($field){
		case 'single-line-field':
		case 'numeric-field':
			$out = do_shortcode('[types field="'.$field.'" separator=", " id="'.$parent_id.'"][/types]');
			break;			
	}
	return $out;
}

this custom code will allow us to use a custom shortcode to retrieve the content of the fields of 'cars'. In this example we have two custom fields configured 'single-line-field' and 'numeric-field' (these names are the slugs we have set in Types). What we are doing is getting the post id of the 'cars' from the url and pass it to the Types shortcode for the corresponding field, thus getting the value of that field from the 'cars' post type.

9. go back to the cred form for creating 'car-adverts' and add the custom shortcode in the value of the fields. Considering our custom fields this would be how the form would look

		<div class="cred-field cred-field-single-line-field">
			<div class="cred-label">Single line field</div>
			[cred_field field="single-line-field" post="car-adverts" value="[wpv-post-id-from-url field="single-line-field"]" urlparam=""]
		</div>
		<div class="cred-field cred-field-numeric-field">
			<div class="cred-label">Numeric field</div>
			[cred_field field="numeric-field" post="car-adverts" value="[wpv-post-id-from-url field="numeric-field"]" urlparam=""]
		</div>

at this point save the form and test. This will allow you to save the same content in the car-adverts that it was in the cars custom post type or to edit this content and save it only in the car-adverts custom post type.


With Regards

Bigul

#190463

Hi Bigul,

Thank you very much, this works perfectly. I just have two more questions, the above code for the functions.php file only deals with copying the custom fields of the parent post. How could I also copy the parent posts' title and custom taxonomies to that they are both automatically inputed like the custom fields are?

Thank you very much again!
Aaron

#190475

I've managed to create a function to get the post parent title, so that is done. So my final question is just how can the child post automatically inherit its parents' custom taxonomies?

Thanks

#190575

Bigul
Supporter

Dear Aaron,

Hope you want to show parent taxonomies in Child post page. If so, you can try following short code. You need to replace "taxonomy-slug" with Your Taxonomy Slug and "parent-post-slug" with your Parent Post Slug.

[wpv-post-taxonomy type="taxonomy-slug" id="$parent-post-slug"]

Please check this tutorial for more details https://toolset.com/documentation/user-guides/displaying-fields-of-parent-pages/


With Regards

Bigul

#190784

Hi Bigul,

Thanks for all your help! I have just noticed one last problem: for the [cred_child_link_form] shortcode, it doesn't seem to be working correctly with WPML because it always outputs the link to the page that will use the CRED form in the default language (even if you are in another language). For example my CRED shortcode is: [cred_child_link_form form="156" parent_id="-1" text="Add New Advert" target="_self"] the form="156" part points to the page ID of the page to use the form in, this is the default page in English, but if I'm using the site in French it still outputs the page to use the form in English and not it's respective translated page. For example the page to use my form in French is one with an ID of 156, but it doesn't seem to switch to that automatically as it should.

Could you please look into this as well?

Many thanks again for all of your help,
Aaron

#190951

Bigul
Supporter

Dear Aaron,

We will check it and get back to you. Please wait.


With Regards

Bigul

#193027

Bigul
Supporter
2014-01-30_1234.png
2014-01-30_1234_001.png
2014-01-30_1235.png

Dear Aaron,

Sorry for the delay. I was away for couple of days. Please try the following and let me know your feedback.

The page ID is passed in the form parameter. Because the ID of the page is given as a parameter to the shortcode and the page where this shortcode is added in the other language needs to be translated individually, so the ID to pass can be different. If using a Views content template we will also need to translate it individually so we can set a particular page ID to each language.

In the default language page
[cred_child_link_form form="156" parent_id="-1" text="Add New Advert" target="_self"]

in the another language page (here we can both set a different page ID and a translated text for the link)
[cred_child_link_form form="157" parent_id="-1" text="Ajouter une nouvelle annonce" target="_self"]

The form parameter is passing the ID of the page to go to. This page is where the CRED form for creating the child content will be. For translating CRED forms we use WPML String Translation and so we can have the form translated in different languages. This will be picked up by CRED depending on the language the page where the form is inserted in.

Please check attached images for adding WPML localization to CRED Form Strings. Visit WPML>>String Translation section and translate the Form Strings.


With Regards

Bigul

Le sujet ‘[Fermé] How can I generate CRED form inputs with data from a non wordpress MySQL table?’ est fermé à de nouvelles réponses.