Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Basic file upload handler for WP-Members
<?php
// Add to your functions.php file below this line.
/**
* Basic file uploader script for WP-Members.
*
* This code snippet handles file uploads when the "file" field type is
* selected for a registration field. Note that this snippet is basic and
* is not intended to be a full featured solution. Please do not ask me
* for support unless you are already a premium support subscriber at
* the plugin's support site http://rocketgeek.com.
*
* To get full support for WP-Members along with access to priority
* support, support forum, plugin extensions, and an extensive library
* of member exclusive code snippets, visit http://rkt.bz/pQ
*
* USAGE INSTRUCTIONS:
*
* To use this code snippet, copy/paste everything except the "<?php" at
* the top of the page into your theme's functions.php file.
*
* This saves the file as an "attachment" which is a WP post type. The
* post ID is then saved in the user meta for the file upload field. To
* get the file or information about the file, use get_user_meta to get
* the ID and use that ID with wp_get_attachment_metadata to get the
* file info.
*
* https://codex.wordpress.org/Function_Reference/get_user_meta
* https://codex.wordpress.org/Function_Reference/wp_get_attachment_metadata
*
* You can also use any of these to get specific file data with the ID
*
* https://codex.wordpress.org/Function_Reference/wp_get_attachment_url
* https://codex.wordpress.org/Function_Reference/wp_get_attachment_link
* https://codex.wordpress.org/Function_Reference/wp_get_attachment_thumb_url
*
* If a file upload field is required, the plugin's default form validation
* does not yet support this. You will need to set up some custom validation
* with the wpmem_pre_register_data action hook. There is an example at the
* end of this file.
*
* NOTE:
*
* This script is a change from the previously released script that saved
* the file URL as the user meta. If you used that in production, you may
* need to change to be compatible with how the plugin will store file info
* when this is incorporated into the main plugin.
*/
/**
* Get uploaded file information.
*
* This process hooks to the registration function after the registration
* form has been validated and the new user created.
*
* See: http://rkt.bz/5X
*/
add_action( 'wpmem_post_register_data', 'my_upload_file' );
function my_upload_file( $fields ) {
global $wpmem, $new_user_id;
$new_user_id = $fields['ID'];
// Check the form for uploads and process.
if ( ! empty( $_FILES ) ) {
foreach ( $wpmem->fields as $file_field ) {
// Array keys will need to change when the field settings array is changed.
$field_type = $file_field[3];
$field_meta = $file_field[2];
if ( 'file' == $field_type && is_array( $_FILES[ $field_meta ] ) ) {
// Upload the file and save it as an attachment.
$file_post_id = my_upload_user_file( $_FILES[ $field_meta ] );
// Save the attachment ID as user meta.
update_user_meta( $new_user_id, $field_meta, $file_post_id );
}
}
}
}
/**
* Uploads file.
*
* This function uploads the file from the user.
*/
function my_upload_user_file( $file = array() ) {
// Get WordPress file upload processing scripts.
if ( ! function_exists( 'wp_handle_upload' ) ) {
require_once( ABSPATH . 'wp-admin/includes/file.php' );
}
$file_return = wp_handle_upload( $file, array( 'test_form' => false ) );
if ( isset( $file_return['error'] ) || isset( $file_return['upload_error_handler'] ) ) {
return false;
} else {
$filename = $file_return['file'];
$attachment = array(
'post_mime_type' => $file_return['type'],
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
'post_content' => '',
'post_status' => 'inherit',
'guid' => $file_return['url'],
);
$attachment_id = wp_insert_attachment( $attachment, $file_return['url'] );
require_once( ABSPATH . 'wp-admin/includes/image.php' );
$attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename );
wp_update_attachment_metadata( $attachment_id, $attachment_data );
if ( 0 < intval( $attachment_id ) ) {
// Returns an array with file information.
return $attachment_id;
}
}
return false;
}
/**
* This is a prefilter that uses the wp_handle_upload_prefilter hook.
* This allows you to run checks on the file before allowing it to be uploaded.
*
* Note: this is still being worked out.
* See: https://developer.wordpress.org/reference/hooks/wp_handle_upload_prefilter/
*
* @param array $file An array containing information about the file to be uploaded.
*/
add_filter( 'wp_handle_upload_prefilter', 'my_upload_prefilter' );
function my_upload_prefilter( $file ) {
// Any type of validation of $file contents here.
// Return error or empty if rejected, otherwise return $file.
return $file;
}
/**
* If the file field is required, you will need to make the field NOT required in
* the Fields Tab and then manually check it using wpmem_pre_register_data to make
* sure the field is not empty.
*
* To use this function, remove the comment (//) before "add_action" below.
*
* see: http://rkt.bz/RJ
*/
// add_action( 'wpmem_pre_register_data', 'my_upload_file_required' );
function my_upload_file_required( $fields ) {
global $wpmem_themsg;
// Set the names of your file upload fields.
// These should be 'meta_key' => 'display name'
$required_file_fields = array(
'upload_1' => 'Field Display Name',
// Add additional array items for each file field
//'upload_2' => 'Field Display Name',
);
foreach ( $required_file_fields as $key => $val ) {
if ( empty( $_FILES[ $key ]['name'] ) ) {
$wpmem_themsg = "Sorry, $val is a required field.";
}
}
return;
}
// End of file.
<?php
// Add to your functions.php file below this line.
// NOTE: THIS IS FOR WP-MEMBERS 3.1 AND HIGHER ONLY!!!
// If you have a version installed that is below 3.1, use the file-uploader.php script.
/**
* Basic file uploader script for WP-Members.
*
* This code snippet handles file uploads when the "file" field type is
* selected for a registration field. Note that this snippet is basic and
* is not intended to be a full featured solution. Please do not ask me
* for support unless you are already a premium support subscriber at
* the plugin's support site http://rocketgeek.com.
*
* To get full support for WP-Members along with access to priority
* support, support forum, plugin extensions, and an extensive library
* of member exclusive code snippets, visit http://rkt.bz/pQ
*
* USAGE INSTRUCTIONS:
*
* To use this code snippet, copy/paste everything except the "<?php" at
* the top of the page into your theme's functions.php file.
*
* This saves the file as an "attachment" which is a WP post type. The
* post ID is then saved in the user meta for the file upload field. To
* get the file or information about the file, use get_user_meta to get
* the ID and use that ID with wp_get_attachment_metadata to get the
* file info.
*
* https://codex.wordpress.org/Function_Reference/get_user_meta
* https://codex.wordpress.org/Function_Reference/wp_get_attachment_metadata
*
* You can also use any of these to get specific file data with the ID
*
* https://codex.wordpress.org/Function_Reference/wp_get_attachment_url
* https://codex.wordpress.org/Function_Reference/wp_get_attachment_link
* https://codex.wordpress.org/Function_Reference/wp_get_attachment_thumb_url
*
* If a file upload field is required, the plugin's default form validation
* does not yet support this. You will need to set up some custom validation
* with the wpmem_pre_register_data action hook. There is an example at the
* end of this file.
*
* NOTE:
*
* This script is a change from the previously released script that saved
* the file URL as the user meta. If you used that in production, you may
* need to change to be compatible with how the plugin will store file info
* when this is incorporated into the main plugin.
*/
/**
* Get uploaded file information.
*
* This process hooks to the registration function after the registration
* form has been validated and the new user created.
*
* See: http://rkt.bz/5X
*/
add_action( 'wpmem_post_register_data', 'my_upload_file' );
function my_upload_file( $fields ) {
global $wpmem, $new_user_id;
$new_user_id = $fields['ID'];
// Check the form for uploads and process.
if ( ! empty( $_FILES ) ) {
foreach ( $wpmem->fields as $file_field ) {
// Array keys will need to change when the field settings array is changed.
$field_type = $file_field[3];
$field_meta = $file_field[2];
if ( 'file' == $field_type && is_array( $_FILES[ $field_meta ] ) ) {
// Upload the file and save it as an attachment.
$file_post_id = $wpmem->forms->do_file_upload( $_FILES[ $field_meta ] );
// Save the attachment ID as user meta.
update_user_meta( $new_user_id, $field_meta, $file_post_id );
}
}
}
}
/**
* This is a prefilter that uses the wp_handle_upload_prefilter hook.
* This allows you to run checks on the file before allowing it to be uploaded.
*
* Note: this is still being worked out.
* See: https://developer.wordpress.org/reference/hooks/wp_handle_upload_prefilter/
*
* @param array $file An array containing information about the file to be uploaded.
*/
add_filter( 'wp_handle_upload_prefilter', 'my_upload_prefilter' );
function my_upload_prefilter( $file ) {
// Any type of validation of $file contents here.
// Return error or empty if rejected, otherwise return $file.
return $file;
}
/**
* If the file field is required, you will need to make the field NOT required in
* the Fields Tab and then manually check it using wpmem_pre_register_data to make
* sure the field is not empty.
*
* To use this function, remove the comment (//) before "add_action" below.
*
* see: http://rkt.bz/RJ
*/
// add_action( 'wpmem_pre_register_data', 'my_upload_file_required' );
function my_upload_file_required( $fields ) {
global $wpmem_themsg;
// Set the names of your file upload fields.
// These should be 'meta_key' => 'display name'
$required_file_fields = array(
'upload_1' => 'Field Display Name',
// Add additional array items for each file field
//'upload_2' => 'Field Display Name',
);
foreach ( $required_file_fields as $key => $val ) {
if ( empty( $_FILES[ $key ]['name'] ) ) {
$wpmem_themsg = "Sorry, $val is a required field.";
}
}
return;
}
// End of file.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment