Last active
July 22, 2020 19:22
-
-
Save webghostx/eaad55f2de1c99a5ec80ac15fe32e89f to your computer and use it in GitHub Desktop.
In Überschriften automatisch id-Attribut setzten und Anker-Link einfügen
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Wordpress Content Filter | |
* Automatisch html-Anker Elemente in Überschriften setzen | |
* | |
* Im ersten Abschnitt (edit bis /edit) können Einstellungen vorgenommen werden. | |
* | |
* @author usysto https://usysto.net/wordpress-content-filter-anker-links-in-ueberschriften-ohne-plugin | |
* @copyright free | |
*/ | |
add_filter('the_content', function ($content) { | |
# edit | |
$h_numbers = '2'; // um zB. H2,H3 und H5 zu erwischen, einfach '235' eingeben | |
$insert_link = true; // verlinktes Icon einfügen true/false | |
$link_attr = 'class="h-link"'; // Attribute für den Link setzen | |
$icon_html = '<i class="fa fa-link"></i>'; // html für Icon zB. Font Awesome | |
# /edit | |
$regex = '/(\<h[' . $h_numbers . '](.*?))\>(.*)(<\/h[' . $h_numbers . ']>)/i'; | |
$content = preg_replace_callback( | |
$regex, | |
function ($headings) use ($insert_link, $icon_html, $link_attr) { | |
if (!stripos($headings[0], 'id=')) { | |
$heading_link = ''; | |
$id = sanitize_title(remove_accents($headings[3])); | |
if ($insert_link) | |
$heading_link = '<a href="#' . $id . '" ' . $link_attr . '>' . $icon_html . '</a> '; | |
$headings[0] = $headings[1] . $headings[2] . ' id="' . $id . '">' . $heading_link . $headings[3] . $headings[4]; | |
} | |
return $headings[0]; | |
}, | |
$content | |
); | |
return $content; | |
}, 9); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Die vollständige Beschreibung zu diesem kleinen Code für Wordpress ist unter folgendem Link einsehbar:
Wordpress Content-Filter: Anker-Links in Überschriften ohne Plugin