You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何批量生成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:

  1. 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.txt for easy access.
  2. 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, 
      ) );
      
  3. Run the batch replace
    • Open your roles.txt file in Notepad++, press Ctrl+H to 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!

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:

  1. Paste all 682 role names into column A of a new Excel sheet.
  2. In cell B1, enter this formula to build the function:
    =CONCATENATE("add_role( '", A1, "', __( '", A1, "' ), array( 'read' => true, 'edit_posts' => false, ) );")
    
  3. Drag the formula down from B1 to B682 to populate all functions.
  4. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 15:47:36