Helping Organisations get organised!

Developer reference

What is changed

On a single post, page or custom post type, the main post’s content is parsed with PHP’s DOMDocument and every p, h1, h2, h3 and h4 element without an id gets id="lh_element_id-{tag}{post ID}-{n}", where n counts from 0 for each tag. Every one of those elements gets the class lh-paragraph-id. Nothing is done where the DOM extension is not available.

When content is changed

A the_content filter at priority 10 is added on template_redirect for singular, non-feed requests. It acts once per request, only on the queried post’s content, and only when that content is rendered for the page body: after wp_body_open in classic and PHP templates, or while a block template renders its markup (after template_include, before wp_head). Content run for the page head, such as excerpts built for meta tags, and content of other posts shown on the page are left alone.

Filters

  • lh_paragraph_ids_return_elegible_tags: the tag names that get ids (default p, h1 to h4). Adding a tag numbers it from 0 without changing the ids of the others.
  • lh_paragraph_ids_link_enabled: return true to add the ¶ paragraph links (default false).
  • lh_paragraph_ids_init_plugin: return false to switch the plugin off in code without deactivating it.

Script

The paragraph link script (handle lh_paragraph_ids-script, deferred, in the footer) is enqueued only when links are enabled and the content has paragraphs. It appends <span> <a class="lh-paragraph-id-anchor" href="#{id}">¶</a></span> to every p.lh-paragraph-id with an id. The link title (“Link to this paragraph”) is translatable.

If you like this plugin please consider making a donation to further development