Last active
May 15, 2023 00:01
-
-
Save marklchaves/9092c9dded3055ecaccf3a0fc964b262 to your computer and use it in GitHub Desktop.
Popup Maker Custom Trigger PHP Function for Loading a Popup Based URL contains using the ATC extension
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php // Ignore this first line when copying to your child theme's functions.php file. | |
/** | |
* This PHP function will load a popup on a post or page only if the URL | |
* contains the string "yourstringgoeshere" in it. | |
* | |
* Usage: | |
* 1) Install the code snippet below. Make sure you change "yourstringgoeshere" to the string that'll | |
* be in your URL that you want to launch the popup. Example: https://twenty-twentyone.test/yourstringgoeshere/ | |
* 2) Install the Popup Maker Advanced Targeting Conditions extension. | |
* 3) Edit a popup. | |
* 4) Go to Popup Settings > Targeting. | |
* 5) Select Custom PHP Function and enter custom_url_contains_condition as the function name. | |
* 6) Click Publish or Update to save your changes. | |
* | |
* API calls: | |
* 1) pum_get_popup_id | |
* 2) PUM_Site_Popups::preload_popup_by_id_if_enabled | |
*/ | |
function custom_url_contains_condition() { | |
global $wp; | |
if ( !str_contains( home_url( $wp->request ), 'yourstringgoeshere' ) ) return; // Only load the popup on a page if the URL path has the string "yourstringgoeshere". Change "yourstringgoeshere" to what you want. | |
PUM_Site_Popups::preload_popup_by_id_if_enabled( pum_get_popup_id() ); | |
} | |
/** | |
* You can add the PHP code snippet to your child theme's functions.php file | |
* or with third-party plugins such as My Custom Functions and Code Snippets. | |
* | |
* Learn more: | |
* - https://docs.wppopupmaker.com/article/84-getting-started-with-custom-js | |
* - https://docs.wppopupmaker.com/article/552-getting-started-with-custom-php | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This is the ACF version. | |
* | |
* Instead of using a hardcoded string to test for, you can add an ACF field to your popups. | |
* | |
* First, create your custom field group and add a location rule for it to show up in popup post types. | |
* | |
* https://www.advancedcustomfields.com/resources/creating-a-field-group/ | |
* | |
* Second, edit your popup. Make sure you're showing your new field in the Screen Options. Under Settings > Targeting, | |
* change your Custom PHP Function name to custom_url_contains_condition_acf. | |
* | |
* Lastly, enter the string value you want to url to contain into your new ACF field. | |
* | |
* In the example below, the ACF field name for my popups is "test". If I edit my popup, | |
* set the ACF "test" field value to "content", set my targeting custom PHP to "custom_url_contains_condition_acf" | |
* my popup will load and launch on pages that have the string "content" in the URL path. | |
* | |
* Example URL: https://twenty-twentyone.test/content-control-shortcode/ | |
* | |
*/ | |
function custom_url_contains_condition_acf() { | |
global $wp; | |
$contains_str = get_post_meta( pum_get_popup_id(), 'test', true ); // Grab the value stored in the ACF field named "test" from the popup. | |
if ( !str_contains( home_url( $wp->request ), $contains_str ) ) return; // If my ACF value was "content" and the page's URL is "/this-post-has-cool-content", my popup would load and display. | |
PUM_Site_Popups::preload_popup_by_id_if_enabled( pum_get_popup_id() ); | |
} |
Screen captures for the ACF version
1) Creating the ACF field called Test
2) Creating an ACF location rule for the Test field to show on the popup edit page
3) On the popup editor page, make sure the Test field screen option is checked (turned on) so it displays on the popup edit page. Redundant, I know.
4) ACF Test Field with its value set to the word "content" on the popup editor page
5) The popup targeting condition now using the ACF version of the custom PHP function
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Screen capture of the Targeting condition