Home / Archive / MemberPress: Import Dropdown Options for Custom Fields
Duplicate Snippet

Embed Snippet on Your Site

MemberPress: Import Dropdown Options for Custom Fields

This code snippet allows bulk import of dropdown options into the custom MemberPress dropdown field.

For this code snippet to work, a custom dropdown field must be created at Dashboard > MemberPress > Settings > Fields tab. Then, the mepr_custom_dropdown value should be replaced with the slug of that custom dropdown field, on the following line:

$key = 'mepr_custom_dropdown';

Next, options should be specified following this pattern:

$options[0] = array(
"option_name" => "Option 1 Name", // Option name
"option_value" => "Option 1 Value" // Option value
);

The dummy Option 1 name text should be replaced with the actual name of this dropdown option. Also, the dummy Option 1 value text should be replaced with the actual value to be stored (it can be the same).

This pattern should be copied and added for each additional dropdown option. For every option added, the number should be increased on this line within the pattern:

$options[0] = array(

The first option should have the value of 0, the second option should have the value of 1, as shown in the example. Accordingly, the next option should have a value of 2, and so on.

Save the code snippet (and activate the code) once all dropdown options are added.

To run this script, visit the following URL: [yoursite_url]?import-options=true (e.g. https://yourdomain.com?import-options=true). The “Options Imported Successfully!” message should be displayed to confirm the import.

Code Preview
php
<?php
function mepr_cust_import_options() {
  if( isset( $_REQUEST['import-options'] ) ) {
    // For this code snippet to work, a custom dropdown field must be created at Dashboard > MemberPress > Settings > Fields tab.
    $mepr_options = MeprOptions::fetch( true );
    $key = 'mepr_custom_dropdown'; // Replace the mepr_custom_dropdown with the custom dropdown field slug
    foreach( $mepr_options->custom_fields as $i => $field ) {
      if ( $field->field_key == $key ) {
        $options = $field->options;
        // Specify dropdown options
        $options[0] = array(
          "option_name" => "Option 1 Name", // Option name
          "option_value" => "Option 1 Value" // Option value
        );
        // Copy the item code for each additional option
        $options[1] = array( // The number in the brackets [] needs to be incremented with each new option
          "option_name" => "Option 2 Name",
          "option_value" => "Option 2 Value"
        );
        $field->options = $options;
        $mepr_options->custom_fields[$i] = $field;
      }
    }
    $mepr_options->store( false );
    echo( "Options Imported Successfully!" ); // Visit the following URL: [yoursite_url]?import-options=true to run this script. The “Options Imported Successfully!” message should be displayed to confirm the import.
    die();
  }
}
add_action( 'init','mepr_cust_import_options' );

Comments

Add a Comment