I just need for one site with a front en video with sound to remove the video after two view
I achieved to make it with cookie to show/hide a class
can be seen a demo at
https://accedinfo.com/2021/12/28/wordpress-masquer-ou-afficher-un-bloc-apres-x-vues/
and this is the used code
it works with post or blog but need to adjust everything inside
Maybe somebody can use this to create CCCP = Conditional Cookie Content Post bloc ?
(and no, i’m not an old cccp citizen)
/* elarifr - Set a cookie and count x displayed page to hide or show or reset content */
/* https://ccedinfo.com/2021/12/28/wordpress-masquer-ou-afficher-un-bloc-apres-x-vues/ */
/* Add this snippet in function.php of your child theme */
/* https://wordpress.stackexchange.com/questions/285140/hide-show-content-based-on-cookie */
/* https://wordpress.stackexchange.com/questions/21752/setting-custom-cookies-in-wordpress */
/* Remove cookie from cache Lightspeed /wp-admin/admin.php?page=litespeed-cache#excludes */
add_action('init', 'my_set_cookie_show_count');
function my_set_cookie_show_count(){
// set cookie expiration time
// https://www.php.net/manual/fr/function.strtotime.php
// https://www.php.net/manual/fr/datetime.formats.relative.php select +1 day week month year or value
$expiry = strtotime('+1 week');
if(!isset($_COOKIE["cookie_show_count"])) {
setcookie('cookie_show_count',0,$expiry, COOKIEPATH, COOKIE_DOMAIN);
}
}
/* snippet to add to hide content on home frontpage */
/* Shortcode [cookie_show_count_hide] [/cookie_show_count_hide] */
/* Should be updated to pass arg from short code instead of function */
// Choose page ID where you want the cookie to be updated
// Set a Page_Id number 2,42 , a post_title 'Contact' or post_name (slug) 'about-us', 'contact', 'management'
// array( 2, 42, 'Contact', 'about-us',.... )
// You can change code to dispaly element every time cookie exceed on number of view
// Would be nice to add a toggle to force to show / hide
function cookie_show_count_hide() {
// Choose page ID where you want the cookie to be updated
$cookie_show_hide_page_id = array( 951 );
//number of max allowed repetition before hiding element
$cookie_show_before_hide=1;
$cookie_show_before_reset=3;
// Copyright Banner you're not allowed to modify the content or remove display of $cookie_show_hide_element
$cookie_show_hide_element ="n<!-- Copyright Banner you're not allowed to modify or remove display of cookie_show_hide_element n";
$cookie_show_hide_element.="https://accedinfo.com/2021/12/28/wordpress-masquer-ou-afficher-un-bloc-apres-x-vues n";
$cookie_show_hide_element.="Use shortcode [cookie_show_count_hide] [/cookie_show_count_hide] -->n";
//change style div or css element to show hide
//you should even add a script but not tested
//$cookie_hide_element ='<style> figure.wp-block-video{display:none!important}; video.wp-block-video{muted: muted}} </style>';
$cookie_hide_element ='<style> pre.wp-block-code{display:none!important};}} </style>';
$cookie_hide_element.='<div class="wp-block-pullquote">Vous avez déja vu la vidéo ' . $_COOKIE["cookie_show_count"] . ' fois... De retour dans ';
$cookie_hide_element.=$cookie_show_before_reset - $_COOKIE["cookie_show_count"] . 'vues</div>';
$cookie_show_element ='<div class="wp-block-pullquote">Vous avez déja vu la vidéo ' . $_COOKIE["cookie_show_count"] . ' fois</div>';
// to select a post use is_page(), for a news use is_single()
if ( is_single($cookie_show_hide_page_id) ) {
if (isset($_COOKIE["cookie_show_count"]) ) {
// update cookie count
$show_count=$_COOKIE["cookie_show_count"]+1;
setcookie('cookie_show_count',$show_count,'' , COOKIEPATH, COOKIE_DOMAIN);
}
// $cookie_show_hide .= "_COOKIE[cookie_show_count=".$_COOKIE["cookie_show_count"];
if ($_COOKIE["cookie_show_count"] >= $cookie_show_before_hide ) {
// $cookie_show_hide .= " I WILL HIDE DIV";
$cookie_show_hide = $cookie_show_hide_element . $cookie_hide_element;
} else {
// $cookie_show_hide .= " I WILL SHOW DIV";
$cookie_show_hide = $cookie_show_hide_element . $cookie_show_element;
}
//reset count if > 10
if ($_COOKIE["cookie_show_count"] >= $cookie_show_before_reset ) {setcookie('cookie_show_count',0,'' , COOKIEPATH, COOKIE_DOMAIN);}
//$cookie_show_hide .= "page id". is_page( $cookie_show_hide_page_id ). is_single( $cookie_show_hide_page_id );
return $cookie_show_hide;
}
}
add_shortcode( 'cookie_show_count_hide', 'cookie_show_count_hide' );