Ich versuche, mithilfe von Gravity Forms und dem Plugin "Gravity Forms + Custom Post Types" mehrere Bilder als Anhänge zu einem Beitrag hochzuladen.
Ich habe ein wiederholbares Feld zum Hochladen von Dateien mit dem Gravity Forms gform_column_input_filter. Siehe diese Frage wie es erreicht wurde.
Ich habe dann diesen Code von BandonRandon angepasst, um das wiederholbare Feld zum Hochladen von Dateien wie folgt zu verwenden:
add_filter("gform_after_submission", "nifty_add_post_attachements", 10, 3);
function nifty_add_post_attachements($entry) {
//you'll need this later, TRUST ME.
if ( !function_exists('wp_generate_attachment_metadata') ) {
require_once(ABSPATH . 'wp-admin/includes/image.php');
}
//do we even have a file?
if ( isset($_FILES['input_2']) ) {
$file_urls = $entry['2']; //great but what is its url?
$file_array = unserialize($file_urls);
foreach ( $file_array as $file_url ) {
$upload_dir = wp_upload_dir(); //where do you want to put it?
$file_data = file_get_contents($file_url); //show me what you're made of
$filename = basename($file_url); //so cute but what's its name?
if(wp_mkdir_p($upload_dir['path'])) //can we put it there?
$file = $upload_dir['path'] . '/' . $filename; //yes great
else //or no, okay fine let's try somewhere else
$file = $upload_dir['basedir'] . '/' . $filename; //get the whole location
file_put_contents($file, $file_data); // tada home at last
$wp_filetype = wp_check_filetype($filename, array('jpeg' => 'image/jpeg','jpg' => 'image/jpeg', 'gif' => 'image/gif', 'png' => 'image/png') ); //is it the right type of of file?
$attachment = array( //set up the attachment
'post_mime_type' => $wp_filetype['type'],
'post_title' => sanitize_file_name($filename),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $file, $entry['post_id'] ); //insert attachment
$attach_data = wp_generate_attachment_metadata( $attach_id, $file ); //asign the meta
wp_update_attachment_metadata( $attach_id, $attach_data ); //update the post
}
//remove the entry
//(to keep the entry comment or delete these line)
// IMPORTANT See: http://Pastebin.com/quvsvGHJ for the function
/*if(!function_exists('sedc_gform_remove_entries')){ die('next time maybe you should read the comments');}
else{ nifty_gform_remove_entries($entry, $form );}*/
}
}
Ich habe versucht, $ _FILES ['input_2'] mit dem folgenden Code oben in der Funktion 'nifty_add_post_attachements' zu speichern, aber auf meinem Bildschirm wird keine Ausgabe angezeigt, der Beitrag wird weiterhin erstellt, und das Ajax-Ladespinner-Formular hängt nur.
<pre>
<?php print_r($_FILES['input_2']); die();?>
</pre>
Jede Hilfe oder Hinweise in die richtige Richtung wäre dankbar.
Verwenden Sie statt eines wiederholbaren Upload-Felds eine bedingte Logik, um ein Dropdown-Menü oder ein Radio zu verknüpfen, in dem Sie gefragt werden, wie viele Uploads der Benutzer benötigt (1, 2, 3, 4, 5 ...), und anschließend eine Reihe von vorgefertigten Uploads Felder, die basierend auf der ausgewählten Nummer angezeigt werden. Es ist nicht zu 100% dynamisch, aber Sie haben hoffentlich ein gewisses Limit, wie viele davon Sie überhaupt wollen.
Ich würde diese dann als Metafelder speichern und auf sie verweisen. Sie können jedes Upload-Feld einem Metafeld zuordnen. Wenn dieser Beitrag dann generiert wird (ist es ein Beitrag, oder?), Können Sie auf post_meta verweisen.