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.
myCRED comes with the following built-in modules:
|Module ID||Module Class||Description|
||myCRED_BuddyPress_Module||Handles BuddyPress integrations. Only available if BuddyPress is installed.|
||myCRED_Export_Module||Handles exports. Added in 1.7|
||myCRED_Log_Module||Handles all log pages in the admin area.|
||myCRED_Management_Module||Handles balance adjustments and user profile edits. Added in 1.7|
||myCRED_Network_Module||Handles network settings. Only available on Multisite installations.|
||myCRED_Settings_Module||Handles the settings page.|
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.
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.
The following arguments are available for myCRED Modules:
||string||Yes||A unique ID for the module. Used for settings and module loading.|
||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.|
||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.|
||array||No||Array of default options. Only used if you are using
||string||No||The screen ID if we are adding a custom admin screen.|
||array||No||If adding a custom admin screen, this array needs to contain the menu label and the screen page title.|
||string||No||The capability required to access the custom admin screen.|
||int||No||If adding a custom admin screen, the menu position.|
||bool||No||Must be set to true if the module needs to insert settings on the point type's settings page.|
||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.
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
By default, there are four available instances you can use:
|myCRED Instance||Accessed via class method|
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.