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 3 replies, has 2 voices.

Last updated by Bruno 5 years, 2 months ago.

Assigned support staff: Bruno.

Author Posts
August 1, 2014 at 4:19 pm #446063

Timothy

I'm having trouble at the moment where I've been able to create a form where user fills in information and creates a post afterwards. I've been able to duplicate the posts using

$new_lang_id = $iclTranslationManagement->make_duplicate($post_id, $language_code);

However, the images which I have included is not showing until I am within the backend WPML -> Media Translation (Start Translation). Then I return back to the CMS's Post that I have created I am able to see the image in the Media Library.

Does anyone have any idea on why it doesn't show and what code I have to add to make it show? And how could I make the attachment (attachment id) a feature image of the custom post?

Thanks in advance

August 1, 2014 at 6:04 pm #446103

Bruno
Supporter

Languages: English (English ) Portuguese (Brazil) (Português )

Timezone: America/Sao_Paulo (GMT-03:00)

Dear Timothy,

Please could you add the complete function you are using to duplicate posts?

Thank you.

August 1, 2014 at 6:13 pm #446110

Timothy

This is the code I am using to duplicate the post in different languages.

However, the images are not working.

	// Save to Database
	$post = array(
		'post_content' => '',
		'post_title' => $_POST['location'],
		'post_status' => 'pending',
		'post_type' => 'listings'
	);
	
	$post_id = wp_insert_post( $post );
	
	update_post_meta($post_id, 'dlllocation', $_POST['dlllocation']);
	update_post_meta($post_id, 'dllregion', $_POST['dllregion']);
	update_post_meta($post_id, 'fullpartial', $_POST['fullpartial']);
	update_post_meta($post_id, 'fullprice', $_POST['fullprice']);
	update_post_meta($post_id, 'partialprice', $_POST['partialprice']);
	update_post_meta($post_id, 'actualsize', $_POST['actualsize']);
	update_post_meta($post_id, 'availabledate', $_POST['availabledate']);
	update_post_meta($post_id, 'landlordagent', $_POST['landlordagent']);
	update_post_meta($post_id, 'noofrooms', $_POST['noofrooms']);
	update_post_meta($post_id, 'noofbathroom', $_POST['noofbathroom']);
	if(count($_POST['chkfacilities']) != 0)
	{
		$facil_id = "";
		foreach($_POST['chkfacilities'] as $facilities)
		{
			$facil_id .= $facilities.',';
		}
		update_post_meta($post_id, 'facilities', substr($facil_id, 0, -1));
	}
	
	// Upload Image
	$uploadedfile = $_FILES['myfile'];
	$upload_overrides = array( 'test_form' => false );
	$movefile = wp_handle_upload( $uploadedfile, $upload_overrides );
	
	if ( $movefile ) {
		$wp_filetype = $movefile['type'];
		$filename = $movefile['file'];
		$wp_upload_dir = wp_upload_dir();
		$attachment = array(
			'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ),
			'post_mime_type' => $wp_filetype,
			'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
			'post_content' => '',
			'post_status' => 'inherit',
			'post_parent' => $post_id
		);
		
		// Insert the attachment.
		$attach_id = wp_insert_attachment( $attachment, $filename, $post_id );

		$get_attachment_info = $wpdb->get_results('SELECT * FROM `wp_posts` WHERE `ID` = "'.$attach_id.'"');
		
		// Generate the metadata for the attachment, and update the database record.
		$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
		
		$attach_id = media_handle_upload( 'myfile', $post_id );
		
		wp_update_attachment_metadata( $attach_id, $attach_data );
		
		set_post_thumbnail( $post_id, $attach_id );
	}
		 
	
	// get languages
	$langs = $sitepress->get_active_languages();
	unset($langs[$sitepress->get_current_language()]);
 
	//now lets create duplicates of all new posts in all languages used for translations
	foreach($langs as $language_code => $v){
		$new_lang_id = $iclTranslationManagement->make_duplicate($post_id, $language_code);
		update_post_meta($new_lang_id, 'dlllocation', $_POST['dlllocation']);
		update_post_meta($new_lang_id, 'dllregion', $_POST['dllregion']);
		update_post_meta($new_lang_id, 'fullpartial', $_POST['fullpartial']);
		update_post_meta($new_lang_id, 'fullprice', $_POST['fullprice']);
		update_post_meta($new_lang_id, 'partialprice', $_POST['partialprice']);
		update_post_meta($new_lang_id, 'actualsize', $_POST['actualsize']);
		update_post_meta($new_lang_id, 'availabledate', $_POST['availabledate']);
		update_post_meta($new_lang_id, 'landlordagent', $_POST['landlordagent']);
		update_post_meta($new_lang_id, 'noofrooms', $_POST['noofrooms']);
		update_post_meta($new_lang_id, 'noofbathroom', $_POST['noofbathroom']);
		update_post_meta($new_lang_id, 'facilities', substr($facil_id, 0, -1));
	}
	
}

August 5, 2014 at 12:03 am #447227

Bruno
Supporter

Languages: English (English ) Portuguese (Brazil) (Português )

Timezone: America/Sao_Paulo (GMT-03:00)

Dear Timothy,

I'll ask for some information so I can verify the problem. The fields for you to insert the credentials will be below its comment field.

Please let me know exactly what steps should I follow to see the problem.

Note: Please make a full backup of your site.

Thank you.

The topic ‘[Closed] Front End Form Attach Image To Custom Post Type’ is closed to new replies.