The question is: How do I create an extension or module for CiviCRM?
I will be answering my own question below, as a very basic How To guide for noobs (like me)!
These are just very basic How To instructions on getting a custom extension/module/plugin running on WordPress CiviCRM:
Step 1 - You need to have civix installed on your server. For this step, please take a look here: https://docs.civicrm.org/dev/en/latest/extensions
Step 2 - From your terminal, use the cd command change dirs to the ext directory which is the uploads folder:
/something/public_html/wp-content/uploads/civicrm/ext
Step 3 - Use the following command to create the extension: NOTE: abc123_thenameofmyextension can be anything you want, but it will need to be unique. So for example you might want to use something like com_mydomain_thenameofmyextension NOTE: When you click enter, it should generate the extension and it will ask you if you want to ENABLE it right away. I usually choose NO and then you can go the CiviCRM Extensions menu later and enable it there when you are ready to use it. But you can choose YES and it will be enabled right away.
civix generate:module abc123_thenameofmyextension --author="My Name" --email="[email protected]"
Step 4 - Once the extension is generated, you can then go your new /wp-content/uploads/civicrm/ext/abc123_thenameofmyextension
directory and edit the info.xml
file. The only two things you really need to worry about are the <name>FIXME</name>
and <description>FIXME</description>
fields. So set the NAME to whatever you want and add a DESCRIPTION and save the file.
Step 5 - Now add some code that will make the extension do something! The file you want to add your code to is in the same /wp-content/uploads/civicrm/ext/abc123_thenameofmyextension
directory and is called:
abc123_thenameofmyextension.php
Step 6 - In this step, as an example, we are actually going to add some code that will set the Default Financial Type when the New Contributions page/form is opened using the buildForm hook, which was asked here: Set a default Financial Type when adding New Contribution?
Open up the abc123_thenameofmyextension.php and scroll all the way to the bottom of the file and then add the following code:
// Create a new function that will set the Default Financial Type on the New Contributions page/form
function abc123_thenameofmyextension_civicrm_buildForm($formName, &$form) {
if ($formName == 'CRM_Contribute_Form_Contribution') { // Check which page/form we are on.
if ($form->_action & CRM_Core_Action::ADD) { // Make sure we are adding a NEW contribution and not editing an existing contribution.
$form->setDefaults(['financial_type_id' => 1]); // Set the default financial to type this financial_type_id
}
}
}
Now save this file, enable the extension from the CiviCRM Extensions menu and check if your New Contribution now has a default financial type set.