-2

I am trying to develop a plugin to inject a javascript banner code right before and after the wordpress navigation menu but I am unable to find any solution to this problem. Does anyone have any idea?

I cannot append to a Div ID either since every wordpress theme uses different div id and class for navigation menu.

Thanks

Edit: Banner code is adsense javascript code. Wordpress does have wp_nav_menu functionality and i want to inject javascript before and after wp_nav_menu.

2
  • What is a "banner code"? Answer with an edit please.
    – kaiser
    Commented Apr 30, 2014 at 21:10
  • And there really is no "WordPress" menu (unless you mean the Admin bar). The theme creates the menus.
    – s_ha_dum
    Commented Apr 30, 2014 at 21:16

1 Answer 1

0

The Core menu system, which was not mentioned in the initial version of the question, does not have any "before" and "after" hooks that I can find. You could create a custom Walker that could include those hooks but it is going to be a theme problem, either way.

If that is not satisfactory, you will need to alter the DOM with Javascript. I don't know if that is even possible/viable for the code you want to include but if that is an option you need to hook code to wp_enqueue_scripts something like this: https://wordpress.stackexchange.com/a/128250/21376.

You can use wp_localize_script() to write data to the page that your script can use. For example: https://wordpress.stackexchange.com/a/92184/21376.

2
  • I cannot use external script js file here since I am pulling script code from wordpress db using "get_option()"
    – Zohaib
    Commented May 1, 2014 at 0:47
  • It is nonetheless correct that you would need to use Javascript to alter the DOM. See the edit.
    – s_ha_dum
    Commented May 1, 2014 at 13:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.