0

I need to create a JSON file to save a list in it. This code has to go inside the Wordpress functions.php file. How can I do it?

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');

the code has to be similar to this. Save the variable "lista" on a JSON file

  • Without any more information about where the data comes from etc, it's difficult to suggest something specific - you can have a look at https://stackoverflow.com/questions/1435072/how-to-generate-json-using-php or https://stackoverflow.com/questions/2467945/how-to-generate-json-file-with-php – Nigel Ren Apr 03 '19 at 18:44
  • I modified the post, thanks! – Joan Calafat Sastre Apr 03 '19 at 18:48
  • How is the JSON Data generated? Is the JSON Data contained in static JSON File? Your Code only shows Javascript Function that pushes some IDs into an Array? Is your goal to export this Array out to a JSON File and then reuse? If yes? Would you want it imported directly into Wordpress? – Poiz Apr 03 '19 at 18:54
  • My goal is to create a JSON file and add the IDs when the condition is met. Then read that file and apply an AddClass with Javascript – Joan Calafat Sastre Apr 04 '19 at 07:16

1 Answers1

0

If the array is being generated in javascript, you can use JSON.stringify

var json = JSON.stringify(lista);
imvain2
  • 15,480
  • 1
  • 16
  • 21