Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #1357041

    I have inserted a script in the function.php at the enfold child (see below 1:)

    Include JavaScript in the head (see below 2:)
    After that, a snippet must be included in the head of the page. The conditional tag of WordPress ensures that the code is only loaded within the search.

    now I do not know where I should embed it
    can you help me

    thanks in advance

    kind regards
    Franz

    #1357056

    Hi Franz,

    You can add a script to the head section using this code snippet and add it to functions.php of your child theme:

    //-------------------------------
    // Custom script in head section
    //-------------------------------
    
    function custom_script_name(){
    ?>
    <script>
    
    // Your script here
    
    </script>
    <?php
    }
    add_action('wp_head', 'custom_script_name');

    For further information on this topic, please refer to our docs: https://kriesi.at/documentation/enfold/add-custom-js-or-php-script/#add-a-script-to-head-section

    Also, the is_search() only triggers when a search result page archive is being displayed.

    Best regards,
    Nikko

    #1357096

    Hello Niko
    Thank you very much for your quick reply.
    I have done it correctly.

    below my function.php in the enfold child

    Thank you and nice greetings
    Franz

    #1357109

    thank you for the reopening

    my question is if I have entered this code correctly

    no error appears but the function is not executed

    I would like the word to be highlighted and highlighted with a color when doing a text search

    thank you and

    kind regards
    Franz

    #1357110

    i know some filters ( no scripts ) as child-theme functions.php snippet to use – but they only do highlight the search word in the search-results page.

    // highlight search word in results
    function highlight_results($text) {
        if (is_search() && !is_admin()) {
            $sr = get_query_var('s');
            $keys = explode(' ', $sr);
            $keys = array_filter($keys);
            $text = preg_replace('/('.implode('|', $keys) .')/iu', '<span class="search-highlight">\0</span>', $text);
        }
        return $text;
    }
    add_filter('the_excerpt', 'highlight_results');
    add_filter('the_title', 'highlight_results');

    But I would recommend this plugin:
    It highlights the search term not only on the search results page, but also on the related links.
    https://en-gb.wordpress.org/plugins/highlight-search-terms/

    #1357118

    Hi,

    Thanks for helping out @guenni007, maybe a plugin would be a better option for you @schweg33?

    Best regards,
    Rikard

    #1357172

    hello Rikard

    I would also like to thank guenni007

    i will use the recommended plugin then.

    now this request can be closed

    kind regards
    Franz

    #1357200

    Hi,
    Glad Guenni007 could help, thank you Guenni007, if you have any further questions please create a new thread and we will gladly try to help you. Thank you for using Enfold.

    Best regards,
    Mike

Viewing 8 posts - 1 through 8 (of 8 total)
  • The topic ‘Include JavaScript in the head’ is closed to new replies.