How to use Parse in WordPress Template?

I want to create a backend in WordPress for my Parse app which displays information from the app using the Parse PHP SDK.

The SDK is included and I am using the autoloader.php to load it but I keep getting the following error:

Read More

Fatal error: Class ‘ParseParseClient’ not found in http://site

I have ensured that my file path is correct.

Any suggestions.

Thanks!

define( 'PARSE_SDK_DIR', bloginfo( 'template_url' ) . '/parse/' );


// include Parse SDK autoloader
require_once('autoload.php' );

// Add the "use" declarations where you'll be using the classes

use ParseParseClient;
use ParseParseObject;
use ParseParseQuery;
use ParseParseACL;
use ParseParsePush;
use ParseParseUser;
use ParseParseInstallation;
use ParseParseException;
use ParseParseAnalytics;
use ParseParseFile;
use ParseParseCloud;

ParseClient::initialize('wOI4ED7sqFMI9TN9bBbwVc9WGEePcUuq15V04liY', 'lfcUvPmvT6ayZFZflLWf7rZBgbZKICuS3ppwYIxo', 'NBHXiPtiz6ECMPjnKH33P2WZwxNAMdLJEpooPCe4');

And then for the autoload.php:

<?php

/**
 * You only need this file if you are not using composer.
 * Adapted from the Facebook PHP SDK 4.0.x autoloader
 */

if (version_compare(PHP_VERSION, '5.4.0', '<')) {
  throw new Exception('The Parse SDK requires PHP version 5.4 or higher.');
}

/**
 * Register the autoloader for the Parse SDK
 * Based off the official PSR-4 autoloader example found here:
 * https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader-examples.md
 *
 * @param string $class The fully-qualified class name.
 * @return void
 */
spl_autoload_register(function ($class)
{
  // Parse class prefix
  $prefix = 'Parse';

  // base directory for the namespace prefix
  $base_dir = defined('PARSE_SDK_DIR') ? PARSE_SDK_DIR : bloginfo( 'template_url' ) . '/parse/';

  // does the class use the namespace prefix?
  $len = strlen( $prefix );
  if ( strncmp($prefix, $class, $len) !== 0 ) {
    // no, move to the next registered autoloader
    return;
  }

  // get the relative class name
  $relative_class = substr( $class, $len );

  // replace the namespace prefix with the base directory, replace namespace
  // separators with directory separators in the relative class name, append
  // with .php
  $file = $base_dir . str_replace( '', '/', $relative_class ) . '.php';
  echo $file;
  // echo $relative_class . '<br/>';

  // if the file exists, require it
  if ( file_exists( $file ) ) {
    require $file;
  }
});

Notice that I am echoeing the $file and this points to the correct directory :/

Related posts

Leave a Reply

2 comments

  1. if the folder structure is:

     autoload.php
     yourcurrentfile.php
     /src
     /src/Parse
    

    you can skip / remove:

    define( 'PARSE_SDK_DIR', bloginfo( 'template_url' ) . '/parse/' );
    

    if you want to set another url just use:

    define( 'PARSE_SDK_DIR', __DIR__.'/src/Parse/' ); 
    

    and change to the correct URL.