Below, you’ll find a detailed guide on how to add the Import and export users and customers Shortcodes to your WordPress website, including their parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the Import and export users and customers Plugin shortcodes not to show or not to work correctly.
Before starting, here is an overview of the Import and export users and customers Plugin and the shortcodes it provides:
"Import and Export Users and Customers is a powerful WordPress plugin that allows you to effortlessly import or export users and customers data from CSV files with additional metadata."
- [import-users-from-csv-with-meta]
- [export-users]
[import-users-from-csv-with-meta] Shortcode
The ‘import-users-from-csv-with-meta’ shortcode allows users to import user data from a CSV file. It checks user capabilities, validates file upload, and processes the data. It handles various options such as sending emails, assigning roles, updating existing users, and deleting users not present in the CSV. It also supports actions before and after the import process.
Shortcode: [import-users-from-csv-with-meta]
Parameters
Here is a list of all possible import-users-from-csv-with-meta shortcode parameters and attributes:
role
– Specifies the role to assign to the imported usersdelete-only-specified-role
– If true, only deletes users with the specified role
Examples and Usage
Basic example – Displays the import form with default settings.
[import-users-from-csv-with-meta /]
Advanced examples
Specify a user role for the imported users. This will override the default role set in the plugin settings.
[import-users-from-csv-with-meta role="subscriber" /]
Specify user role and enable deletion of users not present in the CSV file who have the specified role. This can be useful for maintaining a list of users with a specific role.
[import-users-from-csv-with-meta role="subscriber" delete-only-specified-role=true /]
Specify user role, enable deletion of users not present in the CSV file who have the specified role, and assign their posts to a specific user.
[import-users-from-csv-with-meta role="subscriber" delete-only-specified-role=true delete-users-assign-posts="john_doe" /]
Note: In the above example, replace “john_doe” with the username of the user to whom the posts should be assigned.
PHP Function Code
In case you have difficulties debugging what causing issues with [import-users-from-csv-with-meta]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'import-users-from-csv-with-meta', array( $this, 'shortcode_import' ) );
Shortcode PHP function:
function shortcode_import( $atts ) {
$atts = shortcode_atts( array( 'role' => '', 'delete-only-specified-role' => false ), $atts );
ob_start();
if( !current_user_can( apply_filters( 'acui_capability', 'create_users' ) ) )
wp_die( __( 'Only users who are able to create users can manage this form.', 'import-users-from-csv-with-meta' ) );
if ( $_FILES && !empty( $_POST ) ):
if ( !wp_verify_nonce( $_POST['security'], 'codection-security' ) ){
wp_die( __( 'Nonce check failed', 'import-users-from-csv-with-meta' ) );
}
if( $_FILES['uploadfile']['error'] != 0 || $_FILES['uploadfile']['size'] == 0 ){
_e( 'You must choose a file', 'import-users-from-csv-with-meta' );
}
else{
do_action( 'acui_pre_frontend_import' );
$file = array_keys( $_FILES );
$csv_file_id = $this->upload_file( $file[0] );
// start
$form_data = array();
$form_data["path_to_file"] = get_attached_file( $csv_file_id );
// emails
$form_data["sends_email"] = get_option( "acui_frontend_send_mail" );
$form_data["send_email_updated"] = get_option( "acui_frontend_send_mail_updated" );
$form_data["force_user_reset_password"] = get_option( "acui_frontend_force_user_reset_password" );
// roles
$form_data["role"] = empty( $atts["role"] ) ? get_option( "acui_frontend_role") : $atts["role"];
// update
$form_data["update_existing_users"] = empty( get_option( "acui_frontend_update_existing_users" ) ) ? 'no' : get_option( "acui_frontend_update_existing_users" );
$form_data["update_roles_existing_users"] = empty( get_option( "acui_frontend_update_roles_existing_users" ) ) ? 'no' : get_option( "acui_frontend_update_roles_existing_users" );
// delete
$form_data["delete_users_not_present"] = ( get_option( "acui_frontend_delete_users" ) ) ? 'yes' : 'no';
$form_data["delete_users_assign_posts"] = get_option( "acui_frontend_delete_users_assign_posts" );
$form_data["delete_users_only_specified_role"] = empty( $form_data[ "role" ] ) ? false : $atts['delete-only-specified-role'];
// others
$form_data["empty_cell_action"] = "leave";
$form_data["activate_users_wp_members"] = empty( get_option( "acui_frontend_activate_users_wp_members" ) ) ? 'no_activate' : get_option( "acui_frontend_activate_users_wp_members" );
$form_data["security"] = wp_create_nonce( "codection-security" );
$form_data = apply_filters( 'acui_frontend_import_form_data', $form_data );
$acui_import = new ACUI_Import();
$acui_import->fileupload_process( $form_data, false, true );
wp_delete_attachment( $csv_file_id, true );
do_action( 'acui_post_frontend_import' );
}
else:
?>
<?php do_action( 'acui_frontend_import_before_form' ); ?>
<form method="POST" enctype="multipart/form-data" action="" accept-charset="utf-8" class="acui_frontend_form">
<?php do_action( 'acui_frontend_import_before_input_file' ); ?>
<label><?php _e( 'CSV file <span class="description">(required)</span>', 'import-users-from-csv-with-meta' ); ?></label></th>
<input class="acui_frontend_file_button" type="button" onclick="document.getElementById('uploadfile').click();" value="<?php echo apply_filters( 'acui_import_shortcode_file_button_text', __( 'Choose file', 'import-users-from-csv-with-meta' ) ); ?>">
<input class="acui_frontend_file" type="file" name="uploadfile" id="uploadfile" class="uploadfile" style="display:none;" onchange="document.getElementById('acui_frontend_selected_file').innerHTML=this.value.replace(/C:\\fakepath\\/i, '');"/>
<label id="acui_frontend_selected_file"><?php _e( 'No file selected', 'import-users-from-csv-with-meta' ) ?></label>
<?php do_action( 'acui_frontend_import_after_input_file' ); ?>
<input class="acui_frontend_submit" type="submit" value="<?php echo apply_filters( 'acui_import_shortcode_button_text', __( 'Upload and process', 'import-users-from-csv-with-meta' ) ); ?>" />
<?php do_action( 'acui_frontend_import_after_submit' ); ?>
<?php wp_nonce_field( 'codection-security', 'security' ); ?>
</form>
<?php do_action( 'acui_frontend_import_after_form' ); ?>
<?php endif; ?>
<?php
return ob_get_clean();
}
Code file location:
import-users-from-csv-with-meta/import-users-from-csv-with-meta/classes/frontend.php
[export-users] Shortcode
The Import Users from CSV with Meta plugin shortcode, ‘export-users’, is designed to export user data. This shortcode allows users with the ‘create_users’ capability to export user data based on certain attributes. The attributes include user role, date range, delimiter type, alphabetical order, column selection, and order by clause. The exported data is presented in a progress bar, showing the percentage of data exported.
Shortcode: [export-users]
Parameters
Here is a list of all possible export-users shortcode parameters and attributes:
role
– defines the user role to be exportedfrom
– sets the start date for user registrationto
– sets the end date for user registrationdelimiter
– specifies the character to separate fieldsorder-alphabetically
– sorts users alphabetically if setcolumns
– selects specific user data to exportorderby
– determines the data field to sort users byorder
– sets the sorting order, ascending or descending
Examples and Usage
Basic example – Display an export form for users with role ‘subscriber’
[export-users role="subscriber"]
Advanced examples
Export users from a specific date range, ordered alphabetically and separated by a semicolon.
[export-users role="subscriber" from="2021-01-01" to="2021-12-31" order-alphabetically="yes" delimiter=";"]
Export users with specific columns ‘ID’, ‘user_login’, ‘user_email’ and ordered by ‘ID’ in descending order.
[export-users role="subscriber" columns="ID,user_login,user_email" orderby="ID" order="DESC"]
PHP Function Code
In case you have difficulties debugging what causing issues with [export-users]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'export-users', array( $this, 'shortcode_export' ) );
Shortcode PHP function:
function shortcode_export( $atts ) {
$atts = shortcode_atts( array( 'role' => '', 'from' => '', 'to' => '', 'delimiter' => '', 'order-alphabetically' => '', 'columns' => '', 'orderby' => '', 'order' => '' ), $atts );
ob_start();
if( !current_user_can( apply_filters( 'acui_capability', 'create_users' ) ) )
wp_die( __( 'Only users who are able to create users can export them.', 'import-users-from-csv-with-meta' ) );
ACUI_Exporter::enqueue();
ACUI_Exporter::styles();
?>
<form method="POST" class="acui_frontend_form" id="acui_exporter">
<input type="hidden" name="acui_frontend_export" value="1"/>
<?php foreach( $atts as $key => $value ): ?>
<input type="hidden" name="<?php echo $key; ?>" value="<?php echo $value; ?>"/>
<?php endforeach; ?>
<input class="acui_frontend_submit" type="submit" value="<?php apply_filters( 'acui_export_shortcode_button_text', _e( 'Export', 'import-users-from-csv-with-meta' ) ); ?>"/>
<?php wp_nonce_field( 'codection-security', 'security' ); ?>
<div class="user-exporter-progress-wrapper">
<progress class="user-exporter-progress" value="0" max="100"></progress>
<span class="user-exporter-progress-value">0%</span>
</div>
</form>
<?php
return ob_get_clean();
}
Code file location:
import-users-from-csv-with-meta/import-users-from-csv-with-meta/classes/frontend.php
Conclusion
Now that you’ve learned how to embed the Import and export users and customers Plugin shortcodes, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.
Leave a Reply