Skip to content

Instantly share code, notes, and snippets.

@adczk
Created September 24, 2021 13:21
Show Gist options
  • Select an option

  • Save adczk/7e0a1c766bbaeb0c242f2c1a996c2f89 to your computer and use it in GitHub Desktop.

Select an option

Save adczk/7e0a1c766bbaeb0c242f2c1a996c2f89 to your computer and use it in GitHub Desktop.
Dynamic popup alerts with Hustle based on CTP and cookies
<?php
########################################
#
# dynamic popup alerts with Hustle
#
# create an informational popup in Hustle
# put shortocde [hustle-alert-content] in popup content body
#
# set visibility condition to show popup
# if a "popup_alert_show" cookie equals 1
#
# create your alerts in Popup Alerts (custom post type)
# in WordPress
#
# USE CODE AS MU-PLUGIN
#
#######################################
// shortocde
// to fetch most recent "alert" into popup content
// place the shortcode [hustle-alert-content] in
// hustle popup
function hustle_alert_show()
{
global $post;
$html = "";
$my_query = new WP_Query( array(
'post_type' => 'PopupAlert',
'posts_per_page' => 1
));
if( $my_query->have_posts() ) : while( $my_query->have_posts() ) : $my_query->the_post();
$html .= "<h2>" . get_the_title() . "</h2>";
$html .= "<p>" . get_the_excerpt() . "</p>";
# uncomment line below if you want to include
# direct link to alert in content
// $html .= "<a href=\"" . get_permalink() . "\" class=\"button\">Read more</a>";
endwhile; endif;
return $html;
}
add_shortcode( 'hustle-alert-content', 'hustle_alert_show' );
# let's get the ID of the newest alert and set cookie in user's browser
function hustle_alert_cookie_set() {
global $post;
$my_query = new WP_Query( array(
'post_type' => 'PopupAlert',
'posts_per_page' => 1
));
if( $my_query->have_posts() ) : while( $my_query->have_posts() ) : $my_query->the_post();
$alert_id = get_the_ID();
if (!isset($_COOKIE['popup_alert_seen']) || $_COOKIE['popup_alert_seen'] < $alert_id ) {
//set cookie (valid for year) to save alert ID and show popup
setcookie( 'popup_alert_seen', $alert_id, time()+3600*24*365 );
setcookie( 'popup_alert_show', '1', time()+3600*24*365 );
}
else {
//set cookie (valid for year) to hide popup and extend hide time
setcookie( 'popup_alert_seen', $alert_id, time()+3600*24*365 );
setcookie( 'popup_alert_show', '0', time()+3600*24*365 );
}
endwhile; endif;
}
add_action( 'template_redirect', 'hustle_alert_cookie_set' );
// ---------------------------------------------
// ---------------------------------------------
# STANDARD CUSTOM POST TYPE REGISTRATION
# just a basic post type (title and content)
// Register Custom Post Type
// Register Custom Post Type
function custom_post_type_hustle_alert() {
$labels = array(
'name' => _x( 'PopupAlert', 'Post Type General Name', 'text_domain' ),
'singular_name' => _x( 'PopupAlerts', 'Post Type Singular Name', 'text_domain' ),
'menu_name' => __( 'Popup Alerts', 'text_domain' ),
'name_admin_bar' => __( 'Popup Alerts', 'text_domain' ),
'archives' => __( 'Item Archives', 'text_domain' ),
'attributes' => __( 'Item Attributes', 'text_domain' ),
'parent_item_colon' => __( 'Parent Item:', 'text_domain' ),
'all_items' => __( 'All Alerts', 'text_domain' ),
'add_new_item' => __( 'Add New Alert', 'text_domain' ),
'add_new' => __( 'Add New', 'text_domain' ),
'new_item' => __( 'New Alert', 'text_domain' ),
'edit_item' => __( 'Edit Alert', 'text_domain' ),
'update_item' => __( 'Update Alert', 'text_domain' ),
'view_item' => __( 'View Alert', 'text_domain' ),
'view_items' => __( 'View Alerts', 'text_domain' ),
'search_items' => __( 'Search Alerts', 'text_domain' ),
'not_found' => __( 'Not found', 'text_domain' ),
'not_found_in_trash' => __( 'Not found in Trash', 'text_domain' ),
'featured_image' => __( 'Featured Image', 'text_domain' ),
'set_featured_image' => __( 'Set featured image', 'text_domain' ),
'remove_featured_image' => __( 'Remove featured image', 'text_domain' ),
'use_featured_image' => __( 'Use as featured image', 'text_domain' ),
'insert_into_item' => __( 'Insert into Alert', 'text_domain' ),
'uploaded_to_this_item' => __( 'Uploaded to this Alert', 'text_domain' ),
'items_list' => __( 'Alerts list', 'text_domain' ),
'items_list_navigation' => __( 'Alerts list navigation', 'text_domain' ),
'filter_items_list' => __( 'FilterAlerts list', 'text_domain' ),
);
$args = array(
'label' => __( 'Alert', 'text_domain' ),
'description' => __( 'Post Type Description', 'text_domain' ),
'labels' => $labels,
'supports' => array( 'title', 'editor' ),
'taxonomies' => array(),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => false,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'page',
);
register_post_type( 'PopupAlert', $args );
}
add_action( 'init', 'custom_post_type_hustle_alert', 0 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment