This code is used to add a class on a specific day. On that day, the class is well-added, but the next day this class disappears. Which is the right code? I want to develop a function in a WordPress web to add a class on a specific day that does not disappear the day after.
function ProgramarBorrado(){
?>
<script type="text/javascript">
var day = (new Date).getDate(); //Get today
var month = (new Date).getMonth()+1; //Get this month
var year = (new Date).getFullYear(); //Get this year
var lista = [];
if ((day == 1) && (month == 4) && (year==2019)) { //Push ID on a list
lista.push('#borrar22marc');
}
if ((day == 6) && (month == 4) && (year==2019)) {
lista.push('#borrar6abril');
}
if ((day == 5) && (month == 5) && (year==2019)) {
lista.push('#borrar5maig');
}
if ((day == 19) && (month == 5) && (year==2019)) {
lista.push('#borrar19maig');
}
if ((day == 8) && (month == 6) && (year==2019)) {
lista.push('#borrar8juny');
}
if ((day == 10) && (month == 8) && (year==2019)) {
lista.push('#borrar10agost');
}
lista.forEach(function(element) {
jQuery(element).addClass('borrar-programado');// For each element on the list add a class on the ID
});
</script>
<?php
}
add_action('wp_footer', 'ProgramarBorrado');
This code is to add a class on a specific day. When that concrete day arrives he adds the class well, but then the next day this class does not appear anymore. What would be the correct code?
What I want to say is that the next day is eliminated, and what I want is that it never be eliminated.
Why not extending your check for the specific dates? E.g.
if ((day == 10 || day == 11) && (month == 8) && (year==2019)) {
lista.push('#borrar10agost');
}
You can do it as:
Updated:
function ProgramarBorrado(){
?>
<script type="text/javascript">
var day = (new Date).getDate(); //Get today
var month = (new Date).getMonth()+1; //Get this month
var year = (new Date).getFullYear(); //Get this year
var lista = [];
if ( isNextDay(1, day ) && (month == 4) && (year==2019)) { //Push ID on a list
lista.push('#borrar22marc');
}elseif (isNextDay(6, day ) && (month == 4) && (year==2019)) {
lista.push('#borrar22marc');
lista.push('#borrar6abril');
}elseif (isNextDay(5, day ) && (month == 5) && (year==2019)) {
lista.push('#borrar22marc');
lista.push('#borrar6abril');
lista.push('#borrar5maig');
}elseif (isNextDay(19, day ) && (month == 5) && (year==2019)) {
lista.push('#borrar22marc');
lista.push('#borrar6abril');
lista.push('#borrar5maig');
lista.push('#borrar19maig');
}elseif (isNextDay(8, day ) && (month == 6) && (year==2019)) {
lista.push('#borrar22marc');
lista.push('#borrar6abril');
lista.push('#borrar5maig');
lista.push('#borrar19maig');
lista.push('#borrar8juny');
}elseif (isNextDay(10, day ) && (month == 8) && (year==2019)) {
lista.push('#borrar22marc');
lista.push('#borrar6abril');
lista.push('#borrar5maig');
lista.push('#borrar19maig');
lista.push('#borrar8juny');
lista.push('#borrar10agost');
}else{
lista.push('#borrar22marc');
lista.push('#borrar6abril');
lista.push('#borrar5maig');
lista.push('#borrar19maig');
lista.push('#borrar8juny');
lista.push('#borrar10agost');
}
lista.forEach(function(element) {
jQuery(element).addClass('borrar-programado');// For each element on the list add a class on the ID
});
// check for next day
function isNextDay(day, cday){
if(cday == day || cday == (day+1)){
return true;
}else{
return false;
}
}
</script>
<?php
}
add_action('wp_footer', 'ProgramarBorrado');
Note: isNextDay()
returns boolean (true/false) as condition meets.