如何批量生成PHP自定义用户角色函数?替换指定关键词
Hey there! Generating 682 custom WordPress add_role() functions is totally doable with free tools like Notepad++—no paid software or complex coding required. Let's walk through the easiest methods step by step.
方法1:Notepad++ 纯正则替换(最快最直接)
This is my go-to for quick text manipulation tasks like this:
- Prepare your role name list
- Copy all 682 unique role names into a new Notepad++ document, making sure each name is on its own line (no empty lines unless intentional). Save this as
roles.txtfor easy access.
- Copy all 682 unique role names into a new Notepad++ document, making sure each name is on its own line (no empty lines unless intentional). Save this as
- Set up your base function template
- Create a placeholder version of your
add_role()function, replacing the target string with a unique marker like%%ROLE%%:add_role( '%%ROLE%%', __( '%%ROLE%%' ), array( 'read' => true, 'edit_posts' => false, ) );
- Create a placeholder version of your
- Run the batch replace
- Open your
roles.txtfile in Notepad++, pressCtrl+Hto open the Replace dialog. - In the Find what field, enter the regex
^(.*)$(this matches every full line of text). - In the Replace with field, paste your template, replacing
%%ROLE%%with\1(this is a regex backreference that inserts the current line's role name):add_role( '\1', __( '\1' ), array( 'read' => true, 'edit_posts' => false, ) ); - Check the Regular expression box (leave ". matches newline" unchecked).
- Click Replace All—boom, you'll have 682 fully populated functions in seconds!
- Open your
Critical note: If any of your role names contain single quotes ('), you'll need to escape them first. Use Notepad++'s replace function again to swap ' with \' in your role list before running the batch.
方法2:Excel辅助(great if you need to clean up role names first)
If your role list is in a spreadsheet or needs quick formatting, Excel makes this dead simple:
- Paste all 682 role names into column A of a new Excel sheet.
- In cell B1, enter this formula to build the function:
=CONCATENATE("add_role( '", A1, "', __( '", A1, "' ), array( 'read' => true, 'edit_posts' => false, ) );") - Drag the formula down from B1 to B682 to populate all functions.
- Copy the entire B column, paste into Notepad++, and save as a PHP file.
方法3:Simple Python Script(for more flexibility)
If you know a tiny bit of Python, this script will handle the task automatically and let you tweak the template easily:
# Read the role names from a text file (one per line) with open('roles.txt', 'r', encoding='utf-8') as role_file: roles = [line.strip() for line in role_file if line.strip()] # Define your function template function_template = """add_role( '{role_name}', __( '{role_name}' ), array( 'read' => true, 'edit_posts' => false, ) ); """ # Generate all functions generated_functions = [function_template.format(role_name=role) for role in roles] # Save to output file with open('custom_roles.php', 'w', encoding='utf-8') as output_file: output_file.writelines(generated_functions)
Just save your role list as roles.txt in the same folder as the script, run it, and you'll get a custom_roles.php file with all 682 functions.
内容的提问来源于stack exchange,提问作者redlaw

