Link Menu to Linking Rules: Python Script for Auto-linking Plugins
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
approvedare 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
- Download the folder and run it on the 40-row sample menu in
sample_data/to see each kind of output. - Export your own menu with the columns listed in the README, and mark the links you approve.
- Run
python menu_to_rules.py --menu link_menu_master.csv. Add--min-sources 3or--min-scoreto be stricter. - Preview the rules on a few saved pages with
apply_link_rules.pybefore you import anything. - Import
link_rules.jsonorlink_rules.csvinto 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 itlink_rules.csv: keyword, URL and cap, for plugins that import a flat filepair_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
Where this resource is used on MLforSEO
Walkthroughs on the blog
Concepts behind it
More internal linking resources
- Coding Script or NotebookLink Blocks for WordPress: Related Posts, Topic Cards, Siblings and Mentions
- ChecklistBefore You Build: A Checklist for Internal Linking Tools
- Coding Script or NotebookInternal Linking QA and Holdout Group Notebook
- Visualization TemplateInternal Linking Impact Dashboard (Data Studio Template) with Setup Guide
- Coding Script or NotebookInternal Link Menu Notebook: Priority Score and Versioned Recommendations
- Coding Script or NotebookTopic Clustering Notebook for Internal Linking: BERTopic and K-means
See all 26 internal linking resources →See all 73 coding script or notebook resources →
