WordPress hook directly after body tag

后端 未结 9 929
野性不改
野性不改 2020-12-14 16:39

I\'m having problems finding the right hook to use for my plugin. I\'m trying to add a message to the top of each page by having my plugin add a function. What\'s the best h

9条回答
  •  伪装坚强ぢ
    2020-12-14 16:57

    Creating custom hook is really easy in WordPress. in header.php (or anywhere you may need a hook) locate:

    >
    

    and make it:

    >
    
    

    That's our hook, now let's make it work. In functions.php add:

    function body_begin() {
    do_action('body_begin');
    }
    

    Now hook is ready to use, simply add any actions you need in functions.php:

    function my_function() {
    /* php code goes here */
    }
    add_action('body_begin', 'my_function');
    

    or JavaScript (tracking code etc - this is not the perfect way though, it is a better practice to load JavaScript from .js files, but it is definitely better than adding JavaScript directly to template files):

    function my_function() { ?>
    
    

提交回复
热议问题