ExpressionEngine

2.11.9 User Guide

Retired Documentation

You are using the documentation for version 2.11.9. Go here for the latest version or check here for your available upgrades to the latest version.

ExpressionEngine API

Overview

The API libraries attempt to provide a simple, unified abstraction to common ExpressionEngine operations. This includes managing templates and channels, as well as creating, editing, and deleting channel entries. Typically operations of this sort are complex with multiple steps to maintain database consistency. That makes them prone to errors and difficult to maintain. Making use of the provided APIs removes the burden of staying up-to-date with all of the required steps and ensures that your add-ons will remain functional even if the underlying architecture changes.

Calling the API

class Api
ee()->load->library('api');

After loading the parent API library, the child classes are loaded with instantiate():

ee()->api->instantiate('channel_entries');

At this point, methods within the api_channel_entries api are callable via ee()->api_channel_entries->method_name();

Available APIs

Function Reference

The following public functions are accessible:

Instantiate

Api::instantiate($which)

Instantiate an API:

ee()->api->instantiate('channel_entries');
Parameters:
  • $which (string) – Name of the API to instantiate. Options: channel_categories, channel_entries, channel_fields, channel_structure, and template_structure.
Exception:

Raises an exception if the specified API doesn’t exist

Return type:

Void

Error Count

Api::error_count()

Get the number of API errors:

ee()->api->error_count();
Returns:The number of errors generated in API functions
Return type:Integer

Make URL Safe

Api::make_url_safe($str)

Makes a string safe for use in a URL segment:

ee()->load->library('api');
$str = 'this is a string that's not URL safe.  (we will clean it for $5).';
$str = ee()->api->make_url_safe($str); // Result thisisastringthatsnotURLsafe.wewillcleanitfor5.

Note

Valid Characters are: a-zA-Z0-9_-.

Parameters:
  • $str (string) – String to make URL safe
Returns:

Cleansed string

Return type:

String

Is String URL Safe?

Api::is_url_safe($str)

Checks if a string is safe for use in a URL segment:

ee()->load->library('api');
$str = 'this is a string that\'s not URL safe.  (we will clean it for $5).';
if ( ! ee()->api->is_url_safe($str))
{
    // Do additional Processing on the string to make it URL safe
}
Parameters:
  • $str (string) – String to verify URL safety
Returns:

TRUE on success, FALSE on failure

Return type:

Boolean