hi
i have a big problem
my app must be ready tomorrow and the biggest thing is not working.
i have a photowall the users can take a picture this picture should be inserted in a new post in WordPress, the posts from are already displayed.
in wordpress is use with custom post types and have to fields titel and image and i have the plugin acess with this guest can create a new post.
Is there any help?
thanks Daniel
IONIC SIDE (Controller)
` var params = {
‘action’ : ‘thumbnail’,
‘id’: post_id
};
$http.get( Backend.url , { params: data }).success(function(response) {
var options = new FileUploadOptions();
options.fileKey = "file";
options.fileName = fileURL.substr(fileURL.lastIndexOf('/') + 1);
options.mimeType = "image/jpeg";
var params = {};
params.action = "thumbnail";
params.id = post_id ;
options.params = params;
var ft = new FileTransfer();
ft.upload(fileURL, encodeURI(Backend.url), win, fail, options);
var win = function (r){
console.log(r);
}
var fail = function (error){
console.log(error);
}
}
WORDPRESS SIDE
`add_action( ‘wp_ajax_nopriv_thumbnail’, ‘thumbnail’ );
add_action( ‘wp_ajax_thumbnail’, ‘thumbnail’ );
function thumbnail(){`
if ( ! function_exists( 'wp_handle_upload' ) ) {
require_once( ABSPATH . 'wp-admin/includes/file.php' );
}
$uploadedfile = $_FILES['file'];
$upload_overrides = array( 'test_form' => false );
$movefile = wp_handle_upload( $uploadedfile, $upload_overrides );
if ( $movefile && !isset( $movefile['error'] ) ) {
// $filename should be the path to a file in the upload directory.
$filename = $movefile['file'];
// Check the type of file. We'll use this as the 'post_mime_type'.
$filetype = wp_check_filetype( basename( $filename ), null );
// Get the path to the upload directory.
$wp_upload_dir = wp_upload_dir();
// Prepare an array of post data for the attachment.
$attachment = array(
'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ),
'post_mime_type' => $filetype['type'],
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
'post_content' => '',
'post_status' => 'inherit'
);
// Insert the attachment.
$attach_id = wp_insert_attachment( $attachment, $filename );
// Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
require_once( ABSPATH . 'wp-admin/includes/image.php' );
// Generate the metadata for the attachment, and update the database record.
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
wp_update_attachment_metadata( $attach_id, $attach_data );
set_post_thumbnail( $_POST['id'], $attach_id );
}
die();
}`
i hope that help you .
Hi @system65 , could you please till me what input type should I use to upload photo to wordpress ?
you can use ‘text, hidden, file’ types to pass the image path to cordova file transfer to send the image file to the backend.
1 Like
@system65 I am trying to do the same thing via wp-json/wp/v2/media but without any luck , can you please guide me , what can I do
createReport(score: string, report: string, category: number[] = [], img) {
this.events.publish('wordpress:savestatus', { state: 'saving' });
let data = {
title: score,
excerpt: report,
content: report,
categories: category,
status: 'pending'
};
this.http.post('/server/wp-json/wp/v2/posts', data).subscribe(data => {
this.events.publish('wordpress:savestatus', { state: 'finished' });
this.events.publish('wordpress:createdreport');
let options: FileUploadOptions = {
fileKey: 'file',
fileName: 'name.jpg',
mimeType: 'image/jpeg'
}
var targetPath = img;
const fileTransfer: FileTransferObject = this.transfer.create();
fileTransfer.upload(targetPath, '/server/wp-json/wp/v2/media', options)
.then((data) => {
console.log(data)
}, (err) => {
console.log(err)
})
}, error => {
this.events.publish('wordpress:savestatus', { state: 'error', message: error });
});
}
make sure of all required params here rest-api create-media