Support

Account

Home Forums General Issues How to assign random ID to headings (h1,h2,h3,h4 exc) in AFC WYSIWYG field?

Helping

How to assign random ID to headings (h1,h2,h3,h4 exc) in AFC WYSIWYG field?

  • How to assign random ID to headings (h1,h2,h3,h4 exc) in AFC WYSIWYG field?
    I need to print content in various languages without using polylang or WPML, so i’ve decided to build ACF WYSIWYG fields, in which i put content in various languages. My post are printed by elementor templates depending on category, so i can use tabs widget to switch between ACF field.
    The facts is that i use also TOC widget to anchor various chapters by their headings, but when template prints content, the anchor headings are progressive and the same for all languages.

    I’ve managed to solve this manually, writing by myself an ID into <h> tag, in order to let anchors work, but i need to automate this process.

    I’ve found some snippets to put into functions.php but only for POST CONTENT, not for ACF fields.

    Thanks in advance to all

  • Up!

    I’ve managed to apply IDs to all WYSIWYG fields, but how could intercept a specific field with its key?

    this is the code applied:

    function my_heading_ids($content) {
    $content = preg_replace_callback(“/\<h([4])/”, function ($matches) {
    static $num = 1;
    $hTag = $matches[4];
    return ‘<h4′. $hTag .’ id=”my’ . $num++ . ‘”‘;
    }, $content);
    return $content;
    }
    add_filter(‘acf_the_content’, ‘my_heading_ids’);

    Thanks

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.