[Resolved] Hooks and Code Snippets

Home Forums Support [Resolved] Hooks and Code Snippets

Home Forums Support Hooks and Code Snippets

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

    Hi there,

    I’m struggling 🙂

    I’m trying to add a social icon to the mobile menu.

    Having looked at many previous posts I’ve had a few attempts. This is the latest I dropped into a hook element – but it blanks my site (using Hook: inside_mobile_menu_bar):

    <?php
    add_action( ‘generate_inside_mobile_header’,’nb_mobile_menu_html’ );
    function nb_mobile_menu_html() { ?>

    <?php }

    I’ve tried adding <script></script> tags too. But still it doesn’t execute properly (blank page, and yes the execute PHP is checked.

    Yet, if I strip out all the PHP and just use the HTML it works fine.

    Q1 – Can I just simply use the HTML in the hook or is using PHP better practice?
    Q2 – What PHP do I paste into the element box – exactly? Do I include script tags, if so where?
    Q3 – Or do I need to use in combination with code snippets? I’ve created a snippet, but I’m not sure how to call the snippet from the hook, if that’s what I need to do.

    Some exact code to cut and paste would be wonderful.

    Thanks!

    #1055784
    David
    Staff
    Customer Support

    Hi there,

    that code is a PHP snippet and would need to go in the Code Snippet plugin.
    Thats all you need to do.

    #1056185
    Joe

    Hey David,

    It’s not working for me.

    I put this code into the Code Snippet program and the RSS icon doesn’t display on mobile (as I said it does if I just use the html).

    add_action( ‘generate_inside_mobile_menu_bar’,’nb_mobile_menu_html’ );
    function nb_mobile_menu_html() { ?>

    <?php }

    The documentation is unclear to me with regard to how I do this. Are Code Snippet and Element Hooks alternatives to accomplish the same thing, or do I need to call the Code Snippet from inside the element hook?

    Also, am I using the correct hook for putting this next to the search icon?

    #1056256
    David
    Staff
    Customer Support

    Code Snippets can be used for any type of php function – which would alternatively be added to a Child Themes function.php file.
    The function you’re using ie. add_action attaches your function ( nb_mobile_menu_html ) to the inside_mobile_menu_bar hook. That is all that is required.

    In the Hook Element it’s doing the same thing without the need for the add_action or function. So in Elements you would just add the HTML you want to output.

    That hook is correct but only exists if it has been enabled, eg. Navigation search is enabled.

    I would use the hook element method. If you’re not seeing anything output then try hooking in just some plain text to see whether it is your HTML that is not working.

    #1056547
    Joe

    Thanks David.

    So to confirm… in Hook Elements I can just used the HTML (which works), and there is no need for the PHP?

    It’s just that all the forum advise says to use PHP to inject the HTML – so which way is better practice?

    #1056637
    David
    Staff
    Customer Support

    Thats correct for what you require only the HTML is needed.
    The Hook Element is using PHP – its performing the add_action('the_hook','the_function');

    #1056667
    Joe

    Thanks for the clarity.

    #1056670
    David
    Staff
    Customer Support

    You’re welcome

Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.