FullCalendar on Wordpress integration - wpdb and j

2019-08-17 06:38发布

问题:

I'm looking for help to use fullcalendar in Wordpress.

I'm integrating a schedule from a custom table in phpmyadmin called wp_evenement

So, I've two files

One with template page, where the calendar is displaying and with the script from fullcalendar. This call an other file called 'events.php' where I'm trying to get json result from database.

I tried to use $wpdb but it doesn't work (or I can't make it work). And I tried to connect to database with new PDO but it only works when I'm connected as admin...

Does someone help me with global $wpdb solution ?

Here is my code :

Template :

<div id='calendar' style="margin-top:100px;"></div>    
<script>
            var $ = jQuery;
            $(document).ready(function() {
              var date = new Date();
              var d = date.getDate();
              var m = date.getMonth();
              var y = date.getFullYear();


              var calendar = $('#calendar').fullCalendar({
               editable: true,
               header: {
                left: 'prev,next today',
                center: 'title',
                right: 'month,agendaWeek,agendaDay'
               },


               events: "https://www.exemple.org/wp-content/mytheme/fullcalendar/events.php/",


               eventRender: function(event, element, view) {
                if (event.allDay === 'true') {
                 event.allDay = true;
                } else {
                 event.allDay = true;
                }
               }

              });

             });

            </script>

events.php

  <?php


// List of events
 $json = array();

 // Query that retrieves events
 $requete = "SELECT * FROM wp_evenement ORDER BY id";

 // connection to the database
 try {
 $bdd = new PDO('mysql:host=***.mysql.db;dbname=****', '***', '***');
 } catch(Exception $e) {
  exit('Unable to connect to database.');
 }
 // Execute the query
 $resultat = $bdd->query($requete) or die(print_r($bdd->errorInfo()));

 // sending the encoded result to success page
 echo json_encode($resultat->fetchAll(PDO::FETCH_ASSOC));

?>

Try with $wpdb Works !

    <?php

    include_once $_SERVER['DOCUMENT_ROOT'] . '/wp-config.php';
    include_once $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php';
    include_once $_SERVER['DOCUMENT_ROOT'] . '/wp-includes/wp-db.php';


    // List of events
    $json = array();

    // Query that retrieves events
    global $wpdb;
    $requete = "SELECT * FROM wp_evenement ORDER BY id";

    // Execute the query
    $resultat = $wpdb->get_results($requete);

    // sending the encoded result to success page
    echo json_encode($resultat);

?>

回答1:

Try this code.

<?php

    include_once $_SERVER['DOCUMENT_ROOT'] . '/wp-config.php';
    include_once $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php';
    include_once $_SERVER['DOCUMENT_ROOT'] . '/wp-includes/wp-db.php';


    // List of events
    $json = array();

    // Query that retrieves events
    global $wpdb;
    $requete = "SELECT * FROM wp_evenement ORDER BY id";

    // Execute the query
    $resultat = $wpdb->get_results($requete);

    // sending the encoded result to success page
    echo json_encode($resultat);

?>

your code has spell mistake

try this $wpdb insteal of $wpbd