Skip to content

Instantly share code, notes, and snippets.

@webghostx
Last active July 22, 2020 19:22
Show Gist options
  • Save webghostx/eaad55f2de1c99a5ec80ac15fe32e89f to your computer and use it in GitHub Desktop.
Save webghostx/eaad55f2de1c99a5ec80ac15fe32e89f to your computer and use it in GitHub Desktop.

Revisions

  1. webghostx revised this gist Jul 22, 2020. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions wp-content-filter-anchor-heading.php
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,9 @@
    <?php
    /**
    * Wordpress Content Filter
    * Automatisch html-Anker Elemente in überschriften setzen
    * Automatisch html-Anker Elemente in Überschriften setzen
    *
    * Im ersten Abschnitt (edit) können Einstellungen vorgenommen werden.
    * 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
  2. webghostx renamed this gist Jul 22, 2020. 1 changed file with 0 additions and 0 deletions.
  3. webghostx renamed this gist Jul 22, 2020. 1 changed file with 0 additions and 0 deletions.
  4. webghostx revised this gist Jul 22, 2020. No changes.
  5. webghostx created this gist Jul 22, 2020.
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    <?php
    /**
    * Wordpress Content Filter
    * Automatisch html-Anker Elemente in überschriften setzen
    *
    * Im ersten Abschnitt (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);