How to work with external classes in wordpress

Gislef asked
php wordpress cookies google-analytics
via

To add new functions in wordpress I know we have to do something like this:

function favicon() {
    // function code
}
add_action('wp_head', 'favicon');

I found in github an interesting project, which parses the cookies of Google Analyitics and with that I can access some information about visitor access: origin, media, sessions, etc.

Google-Analytics-Cookie-Parser-PHP

But my question as I transform the project classes into wordpress function?

I understand that this may be a broad question but maybe the answer might be short. For example insert a php url include from the file where the class is in functions.php

thanks

EDIT

Files created by composer, function.php is also at the root of the theme

——————

  /THEME
      /vendor
        autoload.php
        /composer
          files of composer
        /jflight
           /gacookie
              /src
              /tests
              other files
  composer.json
  composer.lock
  function.php

function.php

require_once dirname(__FILE__).'/vendor/autoload.php';

function get_data_cookies(){

            use JflightGACookieGACookie;

            //utma

            $utma->time_of_first_visit; // DateTime
            $utma->time_of_last_visit; // DateTime
            $utma->time_of_current_visit; // DateTime
            $utma->session_count // Integer

            // utmz

            $utmz->timestamp; // DateTime
            $utmz->session_count // Integer
            $utmz->campaign_number // Integer
            $utmz->source // string
            $utmz->medium // string
            $utmz->campaign // string
            $utmz->term // string
            $utmz->content // string

}
add_action( 'wp_head', 'get_cookies' );

How do I call the class correctly?

use JflightGACookieGACookie;

Parse error: syntax error, unexpected 'use' (T_USE) in.. directory theme...functions.php on line 622 

the get_data_cookies function is correct?

to print anywhere on the theme I simply use:

echo $utmz->source;

or should I somehow mention the function?

——- UPDATE——

require get_template_directory().'/vendor/autoload.php';

use JflightGACookieGACookie;


function get_cookies(){

    $utma = GACookie::parse('utma');
    $utmz = GACookie::parse('utmz');
    var_dump( $utma );
    var_dump( $utmz ); 

                    //utma

             $args = $utma->time_of_first_visit; // DateTime
             $args = $utma->time_of_last_visit; // DateTime
             $args = $utma->time_of_current_visit; // DateTime
             $args = $utma->session_count; // Integer

            // utmz

             $args = $utmz->timestamp; // DateTime
             $args = $utmz->session_count; // Integer
             $args = $utmz->campaign_number; // Integer
             $args = $utmz->source; // string
             $args = $utmz->medium; // string
             $args = $utmz->campaign; // string
             $args = $utmz->term; // string
             $args = $utmz->content;  // string


}

and in the form of contacts of the theme I am putting

<?php get_cookies() ?>

but it is returning:

bool(false)bool(false)


Answer
via

The standard for PHP applications, whether they be WordPress plugins or not, is that you use Composer to satisfy dependencies and take of advantage of the autoloading that Composer offers.

Notice that the project Google-Analytics-Cookie-Parser-PHP has a composer.json file in the GitHub repo? You can take full advantage of that. Also notice (by reviewing its composer.json file) that the name of the package is "name": "jflight/gacookie". So the name is jflight/gacookie.

Therefore, if your goal is to utilize functionality offered by this class, here are a suggested list of next actions that will get you where you need to be:

With Composer installed, from a terminal (command-line) type:

$ cd /path/to/local/project/dir
$ composer require jflight/gacookie

That automatically downloads the class files and puts them all into a special folder in your project directory named vendor/. It will also create a composer.json file in your project directory.

Now you can take advantage of a Composer feature called autoloading. To utilize the new dependency that you just required at the command line, add the following line into your PHP file, which is where you began when asking this question.

<?php
require_once dirname(__FILE__).'/vendor/autoload.php';

function favicon() {
    // Refer to the class documentation.
    // You can now use the class here to achieve something.
}
add_action('wp_head', 'favicon');

Tip: It’s worth noting that any additional dependencies that you add by typing composer require something/else from the command-line, will automatically become available as well, because the vendor/autoload.php file will automatically load them when/if code in your own PHP files attempts to use a PHP class that would otherwise be missing. That’s why it’s called an ‘auto’ loader.


WordPress: When you install this into WordPress, you should upload everything, including the entire vendor/ directory and its contents. Those are now your plugin’s dependencies.

Share This
Posted in: