import .doc files to wordpress - wordpress

I am modifying a website on wordpress (codex) and I would like to import all the .doc files (about 200) from the old website and make them into posts on the wordpress website.
Any ideas how to do this bulk import or is the only way to copy and paste each one, which will take a long time.

There Is a way to do this - but as far as I know - it is not easy ..
I struggled with it one time - but could achieve it in the end .
The reason is that a WORD *.doc is a complicated format , and even simple Copy&Paste can make problems (as it actually copies also formatting)
You need to use the COM interface in php , turn the DOC to a TXT file or a string , and then create the post with wp_insert_post( $my_post );
so - opening the DOC . (you need to loop for bulk )
$filename="file.doc";
$TXTfilename = $filename . ".txt";
$word = new COM("word.application") or die("Unable to instantiate Word object");
$word->Documents->Open($filename);
$word->Documents[1]->SaveAs($TXTfilename ,2);// '2' for txt format
$word->Documents[1]->Close(false);
$word->Quit();
$word->Release();
$word = NULL;
unset($word);
$my_doc_string = file_get_contents($TXTfilename); //write file
unlink($TXTfilename);
Then, either read a txt file , or if in the same function use the $my_doc_string directly.. :
$title_p = 'mypost no'.$i;
$my_post = array(
'post_title' => $title_p,
'post_content' => $my_doc_string,
'post_status' => 'publish',
'post_author' => 1
);
$post_id = wp_insert_post( $my_post );
wp_insert_post( $my_post );
NOTE : the COM interface , as far as I know , Is available only on the WINDOWS vesion of PHP , and you will also need to have MS WORD installed ...
some more reading for you :
http://php.net/manual/en/book.com.php
http://docstore.mik.ua/orelly/webprog/php/ch15_03.htm
http://www.gsdesign.ro/blog/extracting-text-from-word-documents-in-php-with-com-objects/
EDIT I -
After reading your question again - if you have an OLD website - why do you need the DOC files ? you might be able to do that with an HTML PARSER (if the OLD website has the same content as the DOCs - but in HTML)
EDIT II - unbelievably - today I have stumbled into this plugin, it might be of some help although I did not tried it .
http://wordpress.org/extend/plugins/auto-poster/screenshots/

l downloaded a plugin https://wordpress.org/plugins/mammoth-docx-converter/ that is able to import word documents into wordpress editor for publishing. Format mapping is not 100% but does the basic stuff. All i need to do now is to be able to populate metadata in association with that particular document (custom post) upon import using particular key words from the post Content to asynchronously populate the meta boxes. I am also trying to generate excerpts automatically from a particular paragraph (second paragraph) of the document.I read about wp all import plugin and find it quite close to what i want but cant really figure out how word documents can be imported at the same time automating the operations stated above. I was able to come up with an XML file from source using WRX specification leveraging exported Content from my Website and then uploading again for test purposes but that is not what i really want to achieve. Is there anyway wp all import plugin can be customized to do what i want?
I also find your php approach quite suitable and might be more appropriate for what i need it to do and my goal. Important for me is an automated import of the word document as a post keeping the formatting intact and and then asynchronously populating associated meta data automatically. Do you have any recommendations based on the above php COM Code?

Related

Wordpress - polylang in command launch post traduction

Today I need some help with Wordpress, as is is far from being my cup of tea.
Currently, I'm tring to create a plugin to export translation from post in a xliff file then reimport this xliff file to set the translation. This plugin we only be used If wordpress uses polylang for translations. For now I'm only exporting meta data.
I've managed to find how polylang is linking two post, saying this one is in english and this one is in french. So for now I have a command to export a post in a xliff file, and a command to import a traduction from an xliff file.
Everything work fine as long, as the post I'm trying to translate already has existing translation (basically I'm just updating meta data).
But my problem, is when I have a post in french, but I do not have a matching post in english.
What I wanted to do was to do, was to create the missing post and after overwrite the meta data.
But I do not know how to create the missing post as a copy of my post and saying that this one is in english. Does anyone know how I can do that from my command ?
Thanks
Actually I found what I wanted. Polylang has commande line for duplicating a post in an other language. And I just had to use \WP_CLI::runcommand
$options = array(
'return' => true,
'parse' => 'json',
'launch' => false,
'exit_error' => true,
);
$command = 'pll post duplicate ' . $idPost . ' ' . $destLocale;
\WP_CLI::runcommand($command, $options);

reverse order of projects in wordpress

I have a list of latest projects displaying on the home page of my wordpress site. I can see the section that calls for the projects, but not sure where I can reverse the order.
I know this works for posts.
<?php query_posts($query_string . "&order=ASC"); ?>
but don't know where to add it. This is the code that calls the projects:
$wpGrade_Options->get('homepage_portfolio_limit') ? $projects_nr = $wpGrade_Options->get('homepage_portfolio_limit') : $projects_nr = 3;
wpgrade_display_portfolio( $projects_nr, true, true); ?>
You are probably using plugin or a theme.
You need to find this function in the plugin/theme files: wpgrade_display_portfolio
From command line you would simply do:
grep -ir "wpgrade_display_portfolio" wordpress/dir
Inside that function there is either a direct call to DB, or if writer of that script was wise built in wp get_posts, WP_POSTS, or query_posts functions.
IF you find any of these wordpress native functions than you can easily reverse order by adding:
'order' => 'DESC',
If there is a mysql query, then I will have to see it first to give you meaningful answer. I also need to see DB and how you actually would like to order things.

Running WordPress plugin periodically to import data

Here is the situation: I have a program producing .csv files with fields for title and post text (according to http://wordpress.org/extend/plugins/csv-importer/other_notes/). I actually can modify the program's output if it's required.
I can now manually log in into WordPress admin console, go to CSV importer plug-in tab, select file and import articles contained in the file.
However, I would like to run this task automatically -- everything will be running on the server (application producing (CSV) data, application/script importing data), not remote file uploading etc ... any idea how to do it?
You'll want to look into cron and/or WP's own wp_schedule_event.
I believe BlogSense Automation Tools provides a CSV import module that has the capability to be automated. As long as you have new csv files being generated and the titles of the posts are unique then the automation should flow.
Otherwise It will take some significant modification to the plugin; in which you would want to have it try to load a local static csv file with CURL (to replace the need to manually load one from your hard-darve) and then use the internal wordpress cronjob system to execute the process from there on out.
Here's the code I use to execute a wordpress internal cronjob every minute. You could alter the second intervals to something significantly larger.
add_filter('cron_schedules', 'add_per_min');
function add_per_min() {
return array(
'perminute' => array('interval' => 60, 'display' => 'Every Minute'),
);
}
if (!wp_next_scheduled('the_name_of_my_custom_interval')) {
wp_schedule_event(time(), 'perminute', 'the_name_of_my_custom_interval' );
}
add_action('the_name_of_my_custom_interval', 'the_function_to_run_here');

Re-processing attached images in drupal 7

I'm trying to import nodes from my forum to drupal 7. Not in bulk, but one by one so that news posts can be created and referenced back to the forum. The kicker is that I'm wanting to bring image attachments across as well...
So far, using the code example here http://drupal.org/node/889058#comment-3709802 things mostly work: Nodes are created, but the images don't go through any validation or processing.
I'd like the attached images to be validated against the rules defined in the content type. in particular the style associated with my image field which resizes them to 600x600.
So, instead of simply creating the nodes programatically with my own form, i decided to modify a "new" node using hook_node_prepare and using the existing form to create new content (based on passed in url args). This works really well and a create form is presented pre-filled with all my data. including the image! very cute.
I expected that i could then hit preview or save and all the validation and resizing would happen to my image, but instead i get the error:
"The file used in the Image field may not be referenced."
The reason for this is that my file doesn't have an entry in the file_usage table.. *le sigh*
so, how do i get to all the nice validation and processing which happens when i manually choose a file to upload? like resizing, an entry in the file_usage table.
The ajax upload function does it, but i can't find the code which is called to do this anywhere in the api.
What file upload / validation functions does Drupal call which i'm not doing?
Anybody have any experience with the file/image api for Drupal 7 who can help me out?
For getting the usage entry (in essence, checking out a file to a specific module so that it doesn't get deleted while its in use) look up the Drupal function 'file_usage_add()'
For validating incoming images, I got this example from user.module (if you're comfortable with PHP, you can always look at the core to see how something is done the 'Drupal way'):
function user_validate_picture(&$form, &$form_state) {
// If required, validate the uploaded picture.
$validators = array(
'file_validate_is_image' => array(),
'file_validate_image_resolution' => array(variable_get('user_picture_dimensions', '85x85')),
'file_validate_size' => array(variable_get('user_picture_file_size', '30') * 1024),
);
// Save the file as a temporary file.
$file = file_save_upload('picture_upload', $validators);
if ($file === FALSE) {
form_set_error('picture_upload', t("Failed to upload the picture image; the %directory directory doesn't exist or is not writable.", array('%directory' => variable_get('user_picture_path', 'pictures'))));
}
elseif ($file !== NULL) {
$form_state['values']['picture_upload'] = $file;
}
}
That function is added to the $form['#validate'] array like so:
$form['#validate'][] = 'user_validate_picture'

Saving nodes with a filefield

I'm in the progress of creating a bulk upload function for a Drupal site. Using flash I'm able to upload the files to a specific url that then handles the files. What I want to do, is not just to upload the files, but create a node of a specific type with the file saved to a filefield that has been setup with CCK. Since these are audio files, it's important that filefield handles the files, so addition meta data can be provided with the getid3 module.
Now I've looked through some of the code as I wasn't able to find an API documentation, but it's not clear at all how I should handle this. Ideally I could just pass the file to a function and just use the data returned when saving the node, but I haven't been able to find that function.
If any one has experience with this I would apreciate some pointers on how to approach this matter.
I had to do something similar some weeks ago and ended up adapting some functionality from the Remote File module, especially the remote_file_cck_attach_file() function. It uses the field_file_save_file() function from the filefield module, which might be the function you're looking for.
In my case, the files are fetched from several remote locations and stored temporarily using file_save_data(). Attaching them to a CCK filefield happens on hook_nodeapi() presave, using the following:
public static function attachAsCCKField(&$node, $filepath, $fieldname, $index=0) {
// Grab the filefield definition
$field = content_fields($fieldname, $node->type);
$validators = array_merge(filefield_widget_upload_validators($field), imagefield_widget_upload_validators($field));
$fieldFileDirectory = filefield_widget_file_path($field);
// This path does not necessarily exist already, so make sure it is available
self::verifyPath($fieldFileDirectory);
$file = field_file_save_file($filepath, $validators, $fieldFileDirectory);
// Is the CCK field array already available in the node object?
if (!is_array($node->$fieldname)) {
// No, add a stub
$node->$fieldname=array();
}
$node->{$fieldname}[$index] = $file;
}
$filepath is the path to the file that should be attached, $fieldname is the internal name of the filefield instance to use within the node and $index would be the 0 based index of the attached file in case of multiple field entries.
The function ended up within a utility class, hence the class syntax for the verifyPath() call. The call just ensures that the target directory is available:
public static function verifyPath($path) {
if (!file_check_directory($path, FILE_CREATE_DIRECTORY)) {
throw new RuntimeException('The path "' . $path . '" is not valid (not creatable, not writeable?).');
}
}
That did it for me - everything else happens on node saving automatically.
I have not used the getid3 module yet, so I have no idea if it would play along with this way of doing it. Also, I had no need to add additional information/attributes to the filefield, so maybe you'd have to put some more information into the field array than just the file returned by field_file_save_file(). Anyways, hope this helps and good luck.
I have done something whith imagefield which worked, I think the structure has to be right otherwise it won't work. It took a lot of trial and error. This is is what I populated the imagefield with.
$image['data'] =array(
'title' => $media_reference_attributes->getNamedItem("source")->value,
'description' => $description,
'alt' => "",);
$image['width'] = $width;
$image['height'] = $height;
$image['mimetype'] = $mime_type
$image['uid'] = 1;
$image['status'] = 1;
$image['fid'] = $fid;
$image['filesize'] = $file->filesize;
$image['nid'] = $id;
$image['filename'] = $url;
$image['timestamp'] = $file->timestamp;
$image['filepath'] = $file_path;
Hope this is of some help.
You might want to look at Image FUpload if you need a look at integrating the flash upload.
To push the files on to another server while still handling them through Drupal sounds a little like the CDN space, maybe look at the behavior in the CDN or CDN2 projects?
If you find a clear solution please come back and post it!

Resources