1. Knowledge Base
  2. WooCommerce Protected Categories
  3. Advanced Usage

Bulk importing passwords into WooCommerce Protected Categories

There are occasions where you may want to bulk import passwords for the WooCommerce Protected Categories plugin, rather than add them manually from the WordPress dashboard. For example, you may need to create lots of categories and passwords, or you may be migrating from another system where multiple passwords are already stored.

Please note that this information is aimed at developers and if you don’t know how to use it then you should ask your developer. If you don’t have one, we recommend posting a job on Codeable. We have partnered with them to provide plugin customizations for our customers.

Need professional assistance?

We've partnered with Codeable to provide our customers with expert help if required.

How to bulk import passwords

At the preset time, WP-CLI is not supported for setting the visibility or passwords. However, you can do this via manual import to the database or adding some custom PHP code.

Note: you must create your product categories before importing the passwords.

Data storage

The category protection data is stored in the ‘term meta’ table in WordPress – this is usually called wp_termmeta, although some sites use a custom table prefix so it may be slightly different on your site. This table has the following columns: meta_id, term_id, meta_key, meta_value. The meta_id is generated automatically on insert, so we only need to worry about the other 3 columns.

The term_id column refers to the category ID and will be the same for all data associated with that category. If you don’t know the term IDs for your categories, you can find them from the WordPress dashboard. There are plugins which can help with this, such as Catch IDs which will show IDs in the categories list.

Setting the protection level

The first thing to do is set the protection level for your category. There are 3 protection levels – public, protected, and private. The default protection is public.

The protected level applies to any type of category protection – whether you have passwords, user role, or user protection (or a combination thereof).

In this case we’re importing passwords, so we need to set the visibility to protected as part of the import process.

The meta_key column should be set to visibility and the meta_value set to protected.

Adding the passwords

There is one row in the table for the passwords associated with a category, and the passwords are stored as a serialized array:

The term ID will be as before. The meta_key column should be set to password.

The passwords are stored in the meta_value column. They are stored as a serialized array, so if you’re interacting directly with the database, you need to make sure you serialize the data beforehand.

Example PHP code

If your import routine is running within WordPress (e.g. code in your theme or custom plugin), then your import code might look something like this. In this example, the $all_passwords array maps term IDs to their respective category password(s):

<?php

// Set up array of all passwords to insert (term ID => password).
$all_passwords = array(
    12 => 'test',
    45 => 'password123',
    31 => array( 'secret', 'test-password', 'letmein' )
);

// Set the protection level
add_woocommerce_term_meta( $term_id, 'visibility', 'protected' );

// Add each password to the database.
foreach ( $all_passwords as $term_id => $passwords ) {
    add_woocommerce_term_meta( $term_id, 'password', (array) $passwords );
}

?>

Example SQL

If you’re dealing directly with the database, your SQL would look something like this. Remember, the passwords array needs to be serialized beforehand using the PHP serialize function (or equivalent):

INSERT INTO wp_termmeta (term_id, meta_key, meta_value) VALUES (12, 'visibility', 'protected'), (12, 'password', 'a:1:{i:0;s:3:"test";}');

The above SQL would need to be repeated for each category.

Need professional assistance?

We've partnered with Codeable to provide our customers with expert help if required.

Still need help?

If searching the knowledge base hasn't answered your question, please contact support.

Related Articles