Module API

Estimated reading: 5 minutes 1018 views

Module API

The myCred Plugin consist of several modules that work together to manage and execute different tasks and features. You can use the Module API to create your own modules for myCred or adjust / disable existing modules to suit your needs. Modules should be used when you want to:

  • Add an admin page under the myCred or any point type menu and or
  • Add settings to the myCred > Settings page.

The Module API uses the myCred_Module abstract class which provides several built in functions to make module creations as easy as possible, providing you the code for mundane tasks like adding a new admin page or added custom settings to the myCred Core.

Modules are not built to be extended but replaced or added.

Built-in Modules

myCred comes with the following built-in modules: 

Module ID Module Class Description
addons myCRED_Addons_Module Handles add-ons.
buddypress myCRED_BuddyPress_Module Handles BuddyPress integrations. Only available if BuddyPress is installed.
export myCRED_Export_Module Handles exports. Added in 1.7
hooks myCRED_Hooks_Module Handles hooks.
log myCRED_Log_Module Handles all log pages in the admin area.
management myCRED_Management_Module Handles balance adjustments and user profile edits. Added in 1.7
network myCRED_Network_Module Handles network settings. Only available on Multisite installations.
general myCRED_Settings_Module Handles the settings page.

Create New Module

Creating a module using the myCred_Module class is relatively easy and straight forward. In order for us to utilize the myCred_Module abstract class, our module class must extend it. This gives us the ability to use pre-defined class functions or replace them with our own before using them if they do not fit our needs.

Basic myCred Module class example.

Class Constructor

First we will need to setup or class constructor using the myCred_Module constructor. This way, the abstract class will setup our module based on the settings we provide.

 

While our class constructor only uses one variable; the point type, the parent constructor uses three:

  • Module id – This needs to be a unique module ID, by default I use the class name as the ID.
  • Module arguments – An associative array of settings for our module.
  • Point Type – The point type used with this module.

Module Arguments

The following arguments are available for myCred Modules:

Argument Type Required Description
module_name string Yes A unique ID for the module. Used for settings and module loading.
register bool Yes Must be set to true if you are registering settings. Set to false if you want to register your own settings. Defaults to true.
option_id string No If you want the module to register your own settings using the WordPress Settings API, you can define either a single option key in a form of a string or an array of option values. Not used if no settings are required to be saved / used.
defaults array No Array of default options. Only used if you are using option_id to register your settings.
screen_id string No The screen ID if we are adding a custom admin screen.
labels array No If adding a custom admin screen, this array needs to contain the menu label and the screen page title.
cap string No The capability required to access the custom admin screen.
menu_pos int No If adding a custom admin screen, the menu position.
add_to_core bool No Must be set to true if the module needs to insert settings on the point type's settings page.
accordion bool No Should myCRED enqueue the accordion script on the custom admin screen. Defaults to false.

Module argument example for adding in a custom admin screen under the myCred menu.

Accessing myCred Instances

Once we have constructed our module, we need to load the module via the load() function. Using this function will hook your module into several myCred instances allowing you to use any of the module_{instance}() functions.

By default, there are four available instances you can use:

myCRED Instance Accessed via class method
mycred_pre_init module_pre_inti()
mycred_init module_init()
mycred_admin_init module_admin_init()
mycred_widgets_init module_widgets_init()

Loading Modules

When loading our custom module from outside the myCred plugin, the most important thing is to load the module once the myCred_Module abstract hook is available and not before.

You can use the mycred_pre_init action hook to load your module class which only fires when myCred is ready and the abstract class is available.

If you are using myCred 1.7 or higher and need to load your module for all point types, you could instead use the mycred_load_modules filter. (Not available before 1.7)

Example: Load our custom module for all point types. This example will add an admin screen under each point type menu called “My Admin Page”.

Example of how to use the mycred_pre_init action hook to load our custom myCred module in versions older then 1.7.

Previously

Next up