ExpressionEngine® User Guide

Legacy Documentation

You are using the documentation for version 4.3.8. Go here for the latest version.

CP Class

Calling the CP Class

class Cp

This class is initialized automatically in the control panel.

Set Variables

Cp::set_variable($name, $value)

Deprecated since version 2.6: Use $this->EE->view->cp_page_title = '...' instead.

This is a simple alternative for setting a page variable. It is used almost exclusively for setting the title of a control panel page:

$this->EE->cp->set_variable('cp_page_title', lang('page_title'));
Parameters:
  • $name (string) – Name of the variable to set
  • $value (string) – Value to set
Return type:

Void

Adding Header Data

Cp::add_to_head($data)

The <head> tag of the control panel can be extended with new styles, meta tags, and other data. Multiple calls to this method are additive:

ee()->cp->add_to_head('<style type="text/css" media="screen">div { display: none; }</style>');
Parameters:
  • $data (string) – String to add to the <head> of the control panel
Return type:

Void

Add JavaScript Files to the JavaScript Combo Loader

Cp::add_js_script($script_type, $script_name)

This method allows you to include scripts found in the main JavaScript directory in the combo load routine, thus reducing HTTP requests. As an example, the call to load filename.js file from the themes/javascript directory would look like this:

ee()->cp->add_js_script('file', 'filename');
Parameters:
  • $script_type (string) – Type of script to load
  • $script_name (string) – Name of script to load
Returns:

Associative array of loaded js files

Return type:

Array

Note

This method will only load files from the themes/javascript directory. To load a third-party add-on package’s JavaScript files, use Cp::load_package_js.

Several custom jQuery plugins are included with ExpressionEngine and available for third-party developers to use. Plugins available include ee_interact.event, tablesorter, ee_table, and wysihat.

An example call to load one of the jQuery plugins:

ee()->cp->add_js_script('plugin', 'tablesorter');

The jQuery UI interactions and widgets are also included with ExpressionEngine for third-party developers to use. The call to load the jQuery UI Autocomplete plugin would look like this, for example:

ee()->cp->add_js_script('ui', 'autocomplete');
Cp::add_js_script([$script = array()[, $in_footer = TRUE]])

Several scripts can be included in a single call as an array:

ee()->cp->add_js_script(
    array(
        'ui'      => array('widget', 'position', 'autocomplete'),
        'plugins'  => array('ee_notice', 'ee_table')
    )
);
Parameters:
  • $script (array) – Associative array containing the scripts you need to load
  • $in_footer (boolean) – Adds to the footer if set to TRUE, otherwise it’s added to the <head>
Returns:

Associative array of loaded js files

Return type:

Array

Loading Third-Party JavaScript Files

Cp::load_package_js($file)

Use this method to load a third-party add-on package’s JavaScript files:

ee()->cp->load_package_js('my_file');

This will load from the current package’s javascript directory:

system/user/addons/my_package/javascript/my_file.js
Parameters:
  • $file (string) – JavaScript file to load, path relative to the current package’s JavaScript directory
Return type:

Void

Allowed Group

Cp::allowed_group($which)

When a user or logged in member visits an EE site, the Session class ascribes user data to them that, among other things, pertains to their member groups’s access to various parts of the site. Returns FALSE if they have access, TRUE if they do:

if ( ! ee()->cp->allowed_group('can_delete_all_entries'))
{
    show_error(lang('unauthorized_to_delete_others'));
}
Parameters:
  • $which (string) – permission string to check for
Returns:

TRUE if they have access, FALSE if they don’t or if the permission doesn’t exist

Return type:

String

Safe Refresh

Cp::get_safe_refresh()

Some pages of the control panel can only be reached after the user submits a form. If you need to perform an action elsewhere and the redirect to the current page, get_safe_refresh() will return a url that takes these considerations into account. To use the result, prefix it with BASE.AMP:

<?=form_open(
    'C=myaccount'.AMP.'M=notepad_update',
    array('id' => 'notepad_form'),
    array('redirect_to' => $this->cp->get_safe_refresh())
)?>
Returns:URL to the current page unless POST data exists, in that case it goes to the root controller
Return type:String

Fetch an Action ID

Cp::fetch_action_id($class, $method)

Modules have certain actions for forms, links, etc. that are recognized via an action ids that are inserted into the database upon installation of that module. This method returns the action id number from the database. (See also functions->fetch_action_id):

$aid = $this->EE->cp->fetch_action_id($class, $method);
Parameters:
  • $class (string) – Class that contains the method
  • $method (string) – Name of the method
Returns:

Action ID

Return type:

Integer

Publish Page Layout Methods

Administrators may extensively customize publish pages on a per member group and per channel basis. Since these custom layouts are saved as a serialized array in the database, any additions or deletions to publish page tabs and fields must be synced to any saved layouts. The control panel library provides 4 methods to facilitate custom layout updates. (See also Tab File Function Reference)