Back to News for Developers

Official Facebook Pixel Plugin for Wordpress, Drupal, and Joomla

June 21, 2019ByMarvin Kwok

We recently revamped our official Facebook pixel plugin for WordPress, Drupal, and Joomla. The Facebook pixel plugin, available for download, allows website owners to easily manage the Facebook pixel on their website.

What You Can Do with the Official Facebook Pixel Plugin

Website owners provide their own Facebook pixel in the admin page and the Facebook pixel with the PageView event is fired on the website by default. Using the following settings, a website marketer can use the signal collected to build the website custom audience and enable their retargeting campaigns.

With the new structure of the Facebook pixel plugin, other plugin developers can now easily build on top of our plugin to fire the lower-funnel standard event or custom event on the website. They don't need to manage Facebook pixel settings in their own respective plugins. This allows website marketers to create campaigns targeted to further down funnel people.

We use WordPress as an example, while the other two platforms (Drupal and Joomla) use similar logic. The samples for each are provided in the open-sourced Github:

Implementing the Integrated Plugin Class

Under the new plugin folder structure lives a folder (named “integration” in Wordpress or “integrations” in Drupal and Joomla). This folder contains the integrated plugin class; for example, Wordpress

The integrated plugin class extends the FacebookWordpressIntegrationBase class (similar to all other platform). As a developer, you must define two constants:

  • PLUGIN_FILE — Used for the Facebook pixel plugin to check whether your plugin exists. If it is not, this integration class is not processed.
  • TRACKING_NAME — Used for the Facebook to track the integration. This is fired at the pixel initiation code at the page load as follows.

ExampleEasy Digital Downloads

Additionally, you must specify the injectPixelCode function, which is where the pixel code is being fired. Inside the function, you can specify their hook through the add_action WordPress function call or our pre-defined addPixelFireForHook function call from the parent class. The pre-defined addPixelFireForHook function is used to simplify the implementation where you just have to specify one Hook handling function.

If you're using the add_action Wordpress function

  1. Specify the hook from the plugin
  2. Optional. In the function handling the hook, you can fire the pixel at wp_footer through another add_action Wordpress frunction call with the handle function.

If you're using our addPixelFireForHook function, specify the hook name, classname, and its handle function. By default, the pixel is fired at wp_footer.

The handle function is triggered when the page loads.

Fetch Facebook Pixel and Display

We provide the FacebookPixel class to fetch our Facebook pixel code snippet in JavaScript format.

We predefined a list of functions for all standard events that Facebook supports. To use the function, specify the param (default is an empty array, the parameter will be Json encoded when it returns), the TRACKING_NAME (reuse the class constant), and script tag option (default is true, meaning the code snippet is encapsulated by the <script> tag).

Example — Using the FacebookPixel class to return checkout code snippet

Good to Know

There's a hack in the param that if the parameters are in JavaScript format, you can specify the value of the param is the parameter name in string format. Then the JavaScript code snippet returned will use the value passed in. Learn more in the Easy Digital Downloads integration class. After you've completed the development, you can submit the class for merge review.

We make this design change such that you can now focus on your own plugin development, while you can also rely on our Official Facebook pixel plugin to fetch and fire the Facebook pixel accordingly for your own use case. For example, for an e-commerce website plugin, you can focus on improving the e-commerce feature instead of needing to put the Facebook pixel code snippet everywhere in your plugin. Further, you can reply on Official Facebook pixel plugin for handling the pixel and its code snippet.

We look forward to your integration!