Skip to content

Instantly share code, notes, and snippets.

@marklchaves
Last active May 24, 2025 14:00
Show Gist options
  • Save marklchaves/b24765b812b2c3a01a112da08bc00db8 to your computer and use it in GitHub Desktop.
Save marklchaves/b24765b812b2c3a01a112da08bc00db8 to your computer and use it in GitHub Desktop.
Popup Maker custom targeting condition for Google Pixel phones
/**
* Install custom JS to the footer of your WP posts and pages.
*
* This SAMPLE code adds a JavaScript function to detect Google Pixel phones.
* It uses the WordPress wp_footer action hook to safely inject the script.
*
* Installation:
* 1. Create a child theme if you don't have one
* 2. Add this code to your child theme's functions.php file
* 3. Or use the Code Snippets plugin for easier management
*
* @uses wp_footer Action hook to add scripts to the footer
*/
add_action( 'wp_footer', function() {
?>
<script type="text/javascript">
/**
* Detects if the current device is a Google Pixel phone
*
* This function checks the browser's user agent string for "Pixel"
* and returns true if found, false otherwise.
*
* @returns {boolean} True if device is a Pixel phone, false otherwise
*/
function testForPixel() {
// Check if "Pixel" exists in the user agent string (case insensitive)
if (/Pixel/i.test(navigator.userAgent)) return true;
return false;
}
</script>
<?php
} );
/**
To install this code, you have two options:
1. **Using the Code Snippets Plugin (Recommended for Beginners)**:
- Install the Code Snippets plugin from WordPress.org
- Go to Snippets > Add New in your WordPress admin
- Paste the code above
- Click "Save Changes and Activate"
2. **Using Your Child Theme's functions.php**:
- Create a child theme if you don't have one
- Open your child theme's functions.php file
- Add the code above
- Save the file
Remember to:
- Back up your site before making any changes
- Test the code on a staging site first
- Clear your cache after installation
- Disable the code if you need to troubleshoot other issues
The code will add a JavaScript function that detects Google Pixel phones by checking
the browser's user agent string. You can call this function from other JavaScript code
using `testForPixel()`. For Popup Maker's Advanced Targeting Conditions Custom JS Function
use `testForPixel`. I.e., no parentheses.
*/
@marklchaves
Copy link
Author

marklchaves commented May 22, 2025

Demo

pm-pixel-conditions-test

Advanced Targeting Conditions Settings

pm-atc-custom-js-pixel-phone

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment