WordPress hook directly after body tag

后端 未结 9 923
野性不改
野性不改 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:46

    Alternatively, if you are creating the theme yourself and/or can modify it, you can create an action yourself using WordPress' do_action function. This is also how they create their other hooks. So basically in your theme, you would go where you want to, right after the <body> tag, and do something like:

    do_action('after_body');
    

    You can also pass arguments to the action callback, see the linked documentation for information.

    Then afterwards, you would simply use the add_action function to hook onto it.

    add_action('after_body', 'my_callback');
    

    Hope that helps. Sorry if I misunderstood.

    0 讨论(0)
  • 2020-12-14 16:51

    WordPress 5.2 or newer:

    Use the wp_body_open hook.

    WordPress 5.1 or older:

    That's kinda difficult... Most themes don't have any hooks in that area. You could hook a javascript/html solution into wp_footer and display it at the top of the page... sort of how Stack Overflow does it, or how Twitter does their notifications.

    This is the best reference for all the hooks included in WordPress: http://adambrown.info/p/wp_hooks/

    0 讨论(0)
  • 2020-12-14 16:51

    WordPress has now addressed this by adding the wp_body_open hook in version 5.2. You can now hook or inject into HTML body by doing:

    <?php add_action('wp_body_open', function() { //some code to fire or inject HTML here }); ?>
    

    Putting this in your functions.php file in your theme might be best for most basic users.

    0 讨论(0)
  • 2020-12-14 16:53

    Changes, changes, changes. So it appears that since March 2019 (from WP 5.2) we have a little nicer way to do this.

    There is a new function wp_body_open(). To support it, your theme has to call this function right after <body> opening tag:

    <html>
      <head>
    
        ..
        ..
    
        <?php wp_head(); ?>
    
      </head>
      <body>
    
        <?php wp_body_open(); ?>
    
        ..
        ..
    
        <?php wp_footer(); ?>
    
      </body>
    </html>
    

    And then you can use it in the same way you use wp_head or wp_footer hooks to print anything just after <body>.

    0 讨论(0)
  • 2020-12-14 16:54

    A very, very, very dirty solution would be:

    /* Insert tracking code or others directly after BODY opens */
    add_filter('body_class', 'wps_add_tracking_body', PHP_INT_MAX); // make sure, that's the last filter in the queue
    function wps_add_tracking_body($classes) {
    
      // close <body> tag, insert stuff, open some other tag with senseless variable      
      $classes[] = '"><script> /* do whatever */ </script><noscript></noscript novar="';
    
      return $classes;
    }
    
    0 讨论(0)
  • 2020-12-14 16:55

    I could not find any working example online, but I got one solution and I hope it might help anyone. It is quite simple, just add jquery and do whatever you want. The below example might help anyone.

    jQuery(document).ready( function($) {
    $('body').prepend('<h1>Hello world</h1>');
    });
    

    Here is the website url https://wordpress.org/support/topic/add_action-right-after-ltbodygt-tag

    With the help of the above answer, I have made a plugin which directly adds content after the body tag. Here is the sample code :

    var wcs_thankyou_msg =  " Your coupon code is <?php echo $post_title; ?>."+"<?php echo get_option('wcs_thankyou_msg'); ?>"
    
    var wcs_scratch_after_text =  "<?php  echo $wcs_scratch_after_text;?>"
    
    var discount_text = "<?php  echo $post_excerpt;?>"
    
    var offer_message = "<?php echo get_option('wcs_offer_text'); ?>" 
    
    var id = "<?php echo $post_id; ?>"
    
    
    
    $('body').prepend('<div id="scratch_main"><div class="scratch_open">'+offer_message+'</div><div id="scratchmain" style="overflow:hidden;"><div class="scratchinnermain"><div class="scratch_text"><div class="scratch_close"></div><div class="scratchtxtstl">'+wcs_top_text+'</div><div class="scratchtxtstl2">'+wcs_top_text_h2+'</div><div id="wscratchpad" class="scratch_img"><div id="scratchBg" class="scratchpad"></div><div class="scratch_offer" style="display:none">'+discount_text+'</div></div></div><div class="scratch_form"><div id="thankYouDiv" style="display:none"><div class="scratch_close"></div><div class="form_txt1">'+wcs_thankyou_msg+'</div></div><div class="scratchinnermain" id="scratchinnermain" style="display:none"><div class="form_txt1">'+wcs_scratch_after_text+'</div><div class="scratch_form_main"><div id="returnmessage"></div><div id="ajax-loading-wp">Sending, Please wait...</div><form id="mycontactform" action="" method="post"><div class="clear_input"><div class="label">Name :</div><div class="wc_input"><input id="name" type="text" name="name"/></div></div><div class="clear_input"><div class="label">Email :</div><div class="wc_input"><input id="email" type="text" name="email"/><input id="submit" type="button" value="send" class="register_btn"/></div></div></form></div></div></div></div></div></div>');
    
    });
    

    The most amazing thing is that I don't know how I have done the above code in an easy way, pulling the data dynamic right after the body tag. I hope my answer will help someone and give a better idea. Live working example : http://codecanyon.net/item/woocommerce-coupon-scratch-discount/9235287

    0 讨论(0)
提交回复
热议问题