Skip to content
Coding Script or NotebookGitHubFree

Link Menu to Linking Rules: Python Script for Auto-linking Plugins

ForInternal linking

By Lazarina Stoy· Updated 5 Dec 2026

A link suggestion is one link: this page, this anchor, that target. An auto-linking rule fires on every page that mentions the anchor. Turning one into the other without thinking is how sites end up with the same anchor pointing at two pages. This free Python script makes the decision for you, from links you have already approved.

Who it’s for

SEOs and content teams who have a reviewed list of internal link suggestions (a link menu) and use, or plan to use, an auto-linking plugin.

How it decides

  • Only rows marked approved are used.
  • An anchor becomes a rule only when you approved it for the same target on at least two different pages. Everything else stays a single link.
  • Anchors are compared without case, and each rule keeps the casing you used most.
  • An anchor that points to two targets is a conflict: the rule is written but switched off, and its note names the other target.
  • Every rule states case sensitivity and a cap of one link per page, so a plugin never falls back to “link every mention”.

How to use it

  1. Download the folder and run it on the 40-row sample menu in sample_data/ to see each kind of output.
  2. Export your own menu with the columns listed in the README, and mark the links you approve.
  3. Run python menu_to_rules.py --menu link_menu_master.csv. Add --min-sources 3 or --min-score to be stricter.
  4. Preview the rules on a few saved pages with apply_link_rules.py before you import anything.
  5. Import link_rules.json or link_rules.csv into your plugin, and write the pair links into your posts.

What you get

  • link_rules.json: one rule per target page, with a stable id so importing again updates it
  • link_rules.csv: keyword, URL and cap, for plugins that import a flat file
  • pair_links.csv: the approved links that stay single links
  • A printed summary listing every disabled rule waiting on a decision from you

Limits

It needs Python and a link menu in this format. It decides which links become rules; it doesn’t judge whether a link is a good idea, so the approvals stay with you. No API key and nothing touches your site.

The same folder has the commands that write approved single links into WordPress through the REST API, with a dry run, backups and rollback. Read 10 Ways to Automate Internal Linking with AI, and see the AI-powered Internal Linking Audit and Automation course for how the menu is built and measured.

What’s included

  • Promotes an anchor to a rule only when you approved it for the same target on two or more pages
  • Anchor conflicts are written but switched off, with a note naming the other target
  • Outputs link_rules.json, a flat link_rules.csv and pair_links.csv for single links
  • Preview what the rules would do before you import them
  • 40-row sample menu and unit tests, no API key