• Resolved Charlie Ray

    (@charlie-ray)


    Hi,

    First, thank you very much for this pluggin, it’s very GREAT, I love it!

    I have a question about the position of the buttons, because none of the options fit to me, I would like to insert them after two paragraphs (the introduction)… Or in my case, before the table of contents and after the content. Because right before is “too early”… And of course I would like an automatic insertion, not by using the shortcode.

    Would it be possible?

    The page I need help with: [log in to see the link]

Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Author Fernando Tellado

    (@fernandot)

    Bonjour @charlie-ray ! Merci pour ton gentil commentaire 🙂

    Votre proposition est très intéressante. Sur votre site web, j’ai vu que vous utilisiez le thème Neve et le plugin Easy Table of Contents. Si le thème était basé sur des blocs, il serait plus facile d’utiliser l’éditeur de site pour insérer le shortcode par programmation, mais ce n’est pas le cas. Vous aurez donc besoin d’un snippet personnalisé pour insérer les boutons (le shortcode) après x articles.

    Je ne souhaite pas ajouter ce type de personnalisation dans la page des paramètres, car celle-ci doit répondre à de nombreux cas d’utilisation possibles, mais cela peut être facilement résolu à l’aide d’un snippet de code.

    Dans votre cas, vous voulez garder les boutons qui apparaissent à la fin de l’article et, à la place de ceux du haut, les mettre après le deuxième paragraphe (juste avant votre TOC). Faites-le en deux étapes :

    1) Dans Réglages → AI Share & Summarize → Settings, changez Automatic insertion position de Both Ã  After content (ainsi le plugin n’insère que ceux du bas et laisse libre la position du haut).

    2) Ajoutez ce snippet dans le functions.php du thème enfant, ou mieux avec un plugin de snippets comme Code Snippets:

    /**
    * Insère les boutons de AI Share & Summarize juste après le
    * deuxième paragraphe de l'article (avant le TOC de Easy TOC dans mon cas).
    * Nécessite le plugin AI Share & Summarize 1.9.0 ou supérieur.
    */
    add_filter( 'the_content', 'mes_aiss_boutons_apres_deuxieme_paragraphe', 9 );
    function mes_aiss_boutons_apres_deuxieme_paragraphe( $content ) {
    // Uniquement sur les articles singuliers dans la boucle principale.
    if ( ! is_singular( 'post' ) || ! in_the_loop() || ! is_main_query() ) {
    return $content;
    }
    // Le plugin doit être actif.
    if ( ! class_exists( 'AyudaWP_AISS_Buttons' ) ) {
    return $content;
    }
    // Respecte l'exclusion par article (la case à cocher de l'éditeur).
    if ( function_exists( 'ayudawp_aiss_is_post_excluded' )
    && ayudawp_aiss_is_post_excluded( get_the_ID() ) ) {
    return $content;
    }

    $buttons = AyudaWP_AISS_Buttons::ayudawp_generate_buttons_html();
    if ( empty( $buttons ) ) {
    return $content;
    }

    // Insère après la fermeture du deuxième </p>.
    $closing = '</p>';
    $position = 0;
    for ( $i = 0; $i < 2; $i++ ) {
    $position = strpos( $content, $closing, $position );
    if ( false === $position ) {
    // Si l'article a moins de deux paragraphes, on le met au début.
    return $buttons . $content;
    }
    $position += strlen( $closing );
    }
    return substr( $content, 0, $position ) . $buttons . substr( $content, $position );
    }

    Notes :

    • Si vous voulez l’insérer après un autre paragraphe que le deuxième, changez le 2 de la boucle par le nombre de votre choix.
    • Si vous voulez qu’il apparaisse aussi sur les pages ou les CPT, ajustez is_singular( 'post' ).
    • Si votre TOC se base sur un titre spécifique (<h2>), une alternative est de chercher la première occurrence de <h2 et de couper le contenu à cet endroit — dites-le moi et je vous passerai cette variante.

    Espero haberte ayudado 😉

    Thread Starter Charlie Ray

    (@charlie-ray)

    Thanks a lot for your help! It was very useful, just, a last request, because the position after the 2nd paragraph is not the best, I think it would be more relevant to put it right before the TOC, because in some cases my introductions have 2, 3 or 4 paragraphs… Could you help with this?

    Thank you again for your help and for your work!

    Xavier

    Plugin Author Fernando Tellado

    (@fernandot)

    Parfait, alors le plus simple est de viser directement le conteneur du TOC. Easy Table of Contents génère un <div id="ez-toc-container">, donc on insère juste avant ce conteneur, quel que soit le nombre de paragraphes au-dessus.

    1) Dans Réglages → AI Share & Summarize → Settings, mettez Automatic insertion position sur After content (pour que le plugin ne place plus que les boutons du bas en automatique ; ceux du haut seront gérés par le snippet).

    2) Ajoutez ce snippet dans le functions.php du thème enfant ou avec un plugin de snippets (Code Snippets, etc.) :

    /**
    * Insère les boutons de AI Share & Summarize juste avant le TOC
    * de Easy Table of Contents (#ez-toc-container).
    * Si l'article n'a pas de TOC, rien n'est inséré en haut.
    * Nécessite AI Share & Summarize 1.9.0 ou supérieur.
    */
    add_filter( 'the_content', 'mes_aiss_boutons_avant_toc', 9 );
    function mes_aiss_boutons_avant_toc( $content ) {
    // Uniquement sur les articles singuliers dans la boucle principale.
    if ( ! is_singular( 'post' ) || ! in_the_loop() || ! is_main_query() ) {
    return $content;
    }
    // Le plugin doit être actif.
    if ( ! class_exists( 'AyudaWP_AISS_Buttons' ) ) {
    return $content;
    }
    // Respecte l'exclusion par article (la case à cocher de l'éditeur).
    if ( function_exists( 'ayudawp_aiss_is_post_excluded' )
    && ayudawp_aiss_is_post_excluded( get_the_ID() ) ) {
    return $content;
    }

    // Cherche le conteneur du TOC d'Easy Table of Contents.
    $needle = 'id="ez-toc-container"';
    $position = strpos( $content, $needle );
    if ( false === $position ) {
    return $content; // Pas de TOC : on ne touche à rien.
    }

    // Remonte jusqu'au début de la balise qui contient cet id.
    $tag_start = strrpos( substr( $content, 0, $position ), '<' );
    if ( false === $tag_start ) {
    return $content;
    }

    $buttons = AyudaWP_AISS_Buttons::ayudawp_generate_buttons_html();
    if ( empty( $buttons ) ) {
    return $content;
    }

    return substr( $content, 0, $tag_start ) . $buttons . substr( $content, $tag_start );
    }

    Notes :

    • Si un article n’a pas de TOC, le snippet ne fait rien (les boutons du bas continueront d’apparaître normalement grâce au réglage After content).
    • Si vous voulez l’appliquer aussi aux pages ou à des CPT, adaptez is_singular( 'post' ).
    • Si un jour vous voulez désactiver les boutons du haut sur un article précis, utilisez la case à cocher du plugin dans la sidebar de l’éditeur — elle agit aussi sur ce snippet (il respecte l’exclusion).

    Pruébalo y me dices. 😉

    Thread Starter Charlie Ray

    (@charlie-ray)

    Hi, thank you again for your answer! Unfortunately, it seems like it doesn’t work? The buttons are not displayed at the top with this code :s

    Thanks you!

    Plugin Author Fernando Tellado

    (@fernandot)

    Do you have any page or post with the snippet applied to see the source code?

    Thread Starter Charlie Ray

    (@charlie-ray)

    Hi!

    On all the pages or posts actually, except the HP.

    i’ve only changed is_singular( ‘post’ ) into is_singular( ” ) to have the buttons in posts and pages (it worked with the first code).

    thanks a lot!

    Plugin Author Fernando Tellado

    (@fernandot)

    Ah, my fault — sorry! The issue is the filter priority. Easy Table of Contents injects its <div id="ez-toc-container"> into the_content at priority 100, but my snippet was running at priority 9: when my code executes, the TOC isn’t in $content yet, so strpos can’t find it and the snippet returns without inserting anything. The first snippet worked because it looked for </p>, which is there from the start.

    Also, your change from is_singular('post') to is_singular('') with an empty string is undocumented syntax — better to use is_singular() with no argument to cover both posts and pages.

    Here’s the corrected snippet:

    /**
    * Inserts AI Share & Summarize buttons just before the
    * Easy Table of Contents container (#ez-toc-container).
    * If the post has no TOC, nothing is inserted at the top.
    *
    * Priority 110: runs AFTER Easy Table of Contents
    * (which hooks into the_content at priority 100).
    *
    * Requires AI Share & Summarize 1.9.0 or later.
    */
    add_filter( 'the_content', 'my_aiss_buttons_before_toc', 110 );
    function my_aiss_buttons_before_toc( $content ) {
    // Singular posts AND pages, in the main loop only.
    if ( ! is_singular() || ! in_the_loop() || ! is_main_query() ) {
    return $content;
    }
    // The plugin must be active.
    if ( ! class_exists( 'AyudaWP_AISS_Buttons' ) ) {
    return $content;
    }
    // Respect per-post exclusion (the checkbox in the editor sidebar).
    if ( function_exists( 'ayudawp_aiss_is_post_excluded' )
    && ayudawp_aiss_is_post_excluded( get_the_ID() ) ) {
    return $content;
    }

    // Look for the Easy Table of Contents container.
    $needle = 'id="ez-toc-container"';
    $position = strpos( $content, $needle );
    if ( false === $position ) {
    return $content; // No TOC on this post: leave content untouched.
    }

    // Walk back to the opening of the tag that holds this id.
    $tag_start = strrpos( substr( $content, 0, $position ), '<' );
    if ( false === $tag_start ) {
    return $content;
    }

    $buttons = AyudaWP_AISS_Buttons::ayudawp_generate_buttons_html();
    if ( empty( $buttons ) ) {
    return $content;
    }

    return substr( $content, 0, $tag_start ) . $buttons . substr( $content, $tag_start );
    }

    The two key changes from the previous version:

    • add_filter( ..., 110 ) instead of 9 → runs after Easy TOC.
    • is_singular() with no argument → covers posts, pages and any singular content type.

    Replace your existing snippet with this one and clear any cache you may have. That should do it … I guess 🙂

    Thread Starter Charlie Ray

    (@charlie-ray)

    Amazing, it’s working! Thank you very much for you support and for this plugin.

    Have a nice week-end!

    Plugin Author Fernando Tellado

    (@fernandot)

    You’re welcome 😉

    I’ll appreciate any review

Viewing 9 replies - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.