This isn’t really a WordPress specific question. However, I recently wrote a function for this (and blocking other user agents). Perhaps you could use it, or some of it?:
function is_a_bot(){
$is_bot = false;
$user_agents = array( 'GTmetrix', 'Googlebot', 'Bingbot', 'BingPreview', 'msnbot', 'slurp', 'Ask Jeeves/Teoma', 'Baidu', 'DuckDuckBot', 'AOLBuild' );
$user_agent = $_SERVER['HTTP_USER_AGENT'];
foreach ( $user_agents as $agent ){
if ( strpos( $user_agent, $agent) ){
$is_bot = true;
}
}
return $is_bot;
}
Simply add/remove the bots that you want/don’t want from the array $user_agents
and use in the following way:
if ( ! is_a_bot() ){
// Do something if it's not a bot (eg. display ad code, template part or run your function here...)
}
Related Posts:
- WordPress User Meta value variable into Google Gauge Chart
- Cleaning up WordPress to improve performance?
- List of all theme customizer control types?
- How can I modify the permalink via a filter?
- wp_set_object_terms and arrays
- How to get the original price of the product in woocommerce?
- How to use WordPress (PHP) functions in AngularJS partials files?
- The best way to customize “nav-menu-template.php” to add if the ‘link_before’ is “checkbox”
- How to add 2 posts under another post? Formatting should be intact
- Get widget settings function?
- Output a WooCommerce product custom field in WooCommerce using get_post_meta()
- enqueue multiple Google fonts with multiple weights and styles (italic)
- How the functions in WP are called in tags
- How to speed up admin-ajax.php in wordpress
- My Own layout in WooCommerce pages [closed]
- Woocommerce – remove product from category
- Differences when using the the_time and the_date functions
- How do I publish only one page to production after making changes on staging?
- get current product name in functions.php
- Displaying the Month and Year that a page was Created?
- How to style options page in dashboard?
- How to access global variable $menu inside a class function
- Why ajax doesn’t work on certain wordpress hooks?
- Print last modified date only on posts
- WooCommerce: How can I get orders with a custom ID in order meta data object?
- Modify function to print tags/categories/exclude tags/categories/ number of posts
- Search not showing all results
- How to Reference/Echo Variable from Another PHP Function
- Security for data obtained from the database
- Make Current/Active Page Number a Link (WP_LINK_PAGES)
- How to make modifications only to certain elements of an HTML string on the server-side?
- How do I modify the custom footer text and get theme version number to show in wordpress admin?
- How to use html inside a functions.php code?
- Nested “do” and “add” Actions is possible?
- Custom Pagination is Broken
- Sidebar not appearing in footer, but it appears everywhere else!
- linking stylesheets and scripts with functions.php
- How can I hook into the wp_mail function used by BackWPup?
- Why i can’t get custom fields value or post ID via Ajax?
- List post by title length
- Fallback image URL if no featured image exists via a shortcode function
- how to enqueque code in a php file from functions.php
- Only load certain artists on this page
- Deactivate a plugin on wp version
- the_post_thumbnail issues
- Some problems calling a function into sprintf() inside functions.php template file [closed]
- Convert all uploaded PNG files to PNG-8 format
- Uses for function: wp_update_user
- WP insert post Redirect after function has executed
- How to Redirect Buddypress Directories To Login Page For Non Logged in Users
- Woocommerce order status payment
- How to define a rule in functions.php that ONLY disables the default wp functionality that undesirably changes ‘&’ to ‘#038;’?
- Uncaught TypeError: extract(): Argument #1 ($array) must be of type array, null given
- Display logged in user name and lastname on page
- How to Add a cutsom slug to my custom author role
- How to Send Pingbacks for all Posts in WordPress?
- How to output the taxonomies that are assigned to each custom post?
- HTML comment cause issue in functions.php script root
- Programmatically add a product to the cart and set price to zero when buying from certain category
- I want to show cart items number if any or nothing, simple CART text
- Exclude category from function – PHP
- How to escape echo for input tag?
- Hide a specific category title from displaying on site
- Display current user metadata on WordPress page
- Reload page with a different shortcode when a user selects from a dropdown
- Handling form actions in WordPress
- Need help with AJAX login to call php in functions.php to handle redirects based on user cap (role)
- Woocommerce Add custom “Add to cart button” in single page with tags
- Set user status to absent on WordPress
- Shortcode’s output to use as other shortcode’s parameter
- Populate Posts based on category selected using AJAX that means without page refresh
- Displaying POST content with HTML tags and all
- PHP script to redirect traffic based on website accessed
- How to remove title tag with this filter
- Why transition_post_type hook is called twice for the same post?
- Can’t get_users info by using json_encode
- Showing wordpress latest post thumbnails in slider with auto increment
- What is wrong in this code?
- My function echos only “array”
- Notify admin (by email) if post added with specific tag
- Retrieving specific images from Media Library
- Can’t put a hyperlink on Featured Post’s Image
- Page returning ID from array, how to return the correct values for post in acf wordpress
- WordPress HTML Helper
- Modify php code to pass a page id as a parameter in order to create a breadcrumb
- Show custom default avatar instead of gravatar
- List sibling pages widget, exclude current page
- Permission error on plugin save
- Removing all my hardcoded URLs with get_site_url()
- WordPress 2014 Theme – Featured Content with multiple tags?
- Display default WordPress login/registration form into a modal window
- Highlight main menu when on specific posts or pages
- Pull GET parameter from URL in functions.php
- Adding Gravity Form With if(is_page) Is Not Working
- Send foreach $_post method to contact form 7 [closed]
- Woocommerce – Shipping tax class based on cart items not using the highest tax available
- How to display the date under the post title?
- How to limit the number of posts in the wordpress loop in conjunction with the “download more” button?
- Need help with conditional logic for menus
- Custom Admin Menu Report for Specific User ID