Tips For Writing Custom Code For Charitable

One reason Charitable is one of the best WordPress plugins to run campaigns and accept donations is it’s flexibility and ability to allow others to customize various key aspects of the experience – including emails, campaign details, and the donation form. Some of this flexibility exists in the “hooks and filters” that Charitable has in it’s code that are available to WordPress developers. Through these, a developer can customize a site to:

— Display a custom field on the donation field via the Donations Field API.
— Add new email tags or modify existing ones with the Email Fields API.
— Customizations to addons such as the Ambassador addon and the Annual Receipts addon.

Code for a WordPress plugin can be written in a number of ways and in different places: some code can be placed in the “functions.php” file of the active WordPress theme, some are written into a plugin (that can be activated and deactivated even if the user keeps the same theme), and (what is becoming increasing popular) code can be hosted in a code snippets plugin.

No matter where the code lives, there are best practices that WordPress developers and coders should be using to ensure the website it’s running in stays secure and doesn’t fall to errors. The following isn’t a complete nor in depth guide, but general reminders for those writing code OR accepting code from developers to insert into their website – especially if that code relies on a third party plugin (like Charitable). 

Plugin File Preferred

If possible try to place custom code in it’s OWN WordPress plugin. This makes it easier to keep the code in place if you switch your WordPress theme and it also allows you to easily turn the plugin containing the customizations on/off for debugging purposes. There are many posts and tutorials on the web about creating a simple WordPress plugin such as this one.

Don’t Assume, Always Check

It’s important for any custom code NOT TO ASSUME CHARITABLE IS ACTIVATED AND RUNNING ON YOUR WEBSITE. Why? Because if it attempts to call a function in Charitable’s core code – and the code isn’t there – an error can occur… an error that could bring down your website! Sometimes WordPress is able to put your site in “Recovery Mode” and send the site administrator an email to notify them about this error, but this isn’t always the case. Therefore, ANY code written for Charitable – regardless of it’s purpose or where the code is located – should “check” to make sure Charitable is activated and/or the function they are attempting to call exists.

Notice our first (bad!) example:

👎🏻 BAD EXAMPLE:

// Bad Example :-( Of Hooking Into Charitable

function charitable_add_donation_gateway_email_tag() {
    charitable()->donation_fields()->get_field( 'gateway_label' )->email_tag = true;
}
add_action( 'init', 'charitable_add_donation_gateway_email_tag' );

The purpose of this block of code is to make the “gateway label” something that can be added as a dynamic tag into Charitable emails. The developer above is “hooking” the function that does this WordPress’s “init” which is run every time a page is loaded on the website – no matter if it’s in the admin or the front end. Notice the function uses “charitable()”… but if this doesn’t exist PHP will throw a “fatal error” – and since the above code is loading on ANY and EVERY page load, the entire site will most likely crash without ability to log in as an admin, etc.

Again, it’s a good practice to never assume a function exists if that function is part of a plugin or theme (in other words if it’s not a part of WordPress core). Assuming the “charitable()” class exists means assuming the Charitable plugin will ALWAYS be there – but if the plugin is deactivated (by a human or by another plugin) or if the plugin is being updated… the function will not be found, and a (possible “fatal”) error would be encountered. Here’s the same function but with a simple addition:

👍🏻 GOOD EXAMPLE:

// Good Example :-) Of Hooking Into Charitable
// Checking To See If The Class Exists!

function charitable_add_donation_gateway_email_tag() {
    if ( class_exists('charitable') ) : // check for class
       charitable()->donation_fields()->get_field( 'gateway_label' )->email_tag = true;
    endif;
}
add_action( 'init', 'mysite_charitable_add_donation_gateway_email_tag' );

Notice the “if” statement that checks if the class of “charitable” exists – this is a way in PHP to safely check if the class exists (in this case it will only if Charitable is actively running on the website). If it doesn’t exist, then the code isn’t run and everyone is safe. It’s common for PHP developers to make checks like class_exists and function_exists for quick ways to ensure the code they are about to run won’t run into serious errors because something they are assuming exists doesn’t exist.

Use Unique Names For Functions

As noted in the above examples, never give your functions generic names like “update_email” because if another plugin uses that same name (current plugins or future ones you might install) then your website might suffer problems. When creating a function always start with “mywebsite_charitable_” (like the example above, replacing “mywebsite” with a reasonable length string of your website name) to ensure that you have likely created a custom name that isn’t repeated (you can also use the same “does this already exist logic” in the above step if you want to go the extra mile).

Enable WordPress Debugging and Debug Logs

For anything beyond a simple script, WordPress developers commonly like to document and log PHP warnings and errors to “catch” problems (especially if they aren’t apparently visible on the website). Charitable Support team recommends the base WP_DEBUG and WP_DEBUG_LOG functions as mentioned here in the WordPress documentation.

Using A Code Snippet Plugin?

Everything said above is applicable also to code inserted into code snippet plugins. If you have a problem with Charitable and know you are running custom code in a code snippet, you should be able to deactivate the code snippet plugin to see if this resolves the issue or helps you further troubleshoot.

Don’t Test On Production

Finally, it’s a good idea to test custom code on a staging or development environment and not live on your website. If you deactivate Charitable here as a test it’s a good “final” way to determine if your website might experience significant problems. This keeps your production environment safe and still able to accept donations. 


Remember that if you are unsure about the code you are adding to your theme, your own plugin, or code snippet plugin – consult with a developer. If you have some general questions feel free to reach out to our support team who will be happy to answer questions about any code you might be adding to your website to extend Charitable. We are here for you and to support you in extending your website to meet your needs!

We hope sharing these suggestions will give you the confidence and ensure your customizations to Charitable will continue to operate well for years to come.

Get free tips and resources right in your inbox, along with 60,000+ others

Join our Newsletter

We won’t spam you. We only send an email when we think it will genuinely help you. Unsubscribe at any time!

Featured Video:

Watch more videos on our YouTube channel.

What's New In Charitable

🔔 Subscribe to get our latest updates
📧 Subscribe to Emails

Email Subscription

Join our Newsletter

We won’t spam you. We only send an email when we think it will genuinely help you. Unsubscribe at any time!

Integration New

Add Image Galleries to Fundraising Campaigns With Envira Gallery

Showcase the impact of your mission like never before. We are excited to announce our brand-new integration with Envira Gallery, the best WordPress gallery plugin, designed to help you tell your story through powerful, high-performance visuals.

The Ultimate Storytelling Experience

A picture is worth a thousand words – and now, it’s worth even more for your fundraising. Connect your visual impact directly to your cause by creating stunning, responsive galleries that engage donors and drive contributions.

🖼️ Visual Impact: Easily create beautiful, fast-loading galleries to show your nonprofit’s work in action, from field reports to event highlights.

🔗 Seamless Connection: Link gallery images directly to your fundraising campaigns, making it effortless for inspired visitors to go from viewing a photo to making a donation.

📱 Perfectly Responsive: Whether your donors are on a phone, tablet, or desktop, your galleries will look professional and load lightning-fast, ensuring a smooth experience on every device.

Integration New

👉🏻 New Divi Integration In Charitable Pro

Bring the power of Charitable directly into your favorite page builder and maintain total creative control with our brand-new Divi integration.

The Ultimate Design Experience

No more switching back and forth or relying on complex shortcodes. Use dedicated Divi modules to build, style, and launch high-converting donation pages without ever leaving the Divi Builder.

⚡ Native Divi Modules: Effortlessly drag and drop your donation forms, progress bars, and campaign details exactly where you want them.

⚙️ Visual Customization: Tweak colors, fonts, and spacing using Divi’s familiar design settings to ensure your fundraiser matches your brand perfectly.

🚀 Live Visual Editing: See your changes in real-time. What you see in the builder is exactly what your donors will see, ensuring a seamless giving experience every time.

donation form New

👉🏻 New Campaign Selector For Donation Forms

Take your campaign management to the next level. Find the perfect fundraiser for any page and stay in your creative flow with our new Campaign Selector integration.

The Ultimate Selection Tool

No more hunting for IDs or creating one page for every donation form. Use the new Campaign Selector to allow users to switch to a campaign with no code.

⚡ Instant Search: Quickly find any campaign leaving your page or post.

⚙️ Editor Agnostic: Whether you’re using the Block Editor, Elementor, or WPBakery, selecting your campaigns is now a unified experience.

🚀 Real-Time Previews: See exactly which campaign you’ve selected instantly, ensuring your donors always see the right cause.

Integration New

WordPress Command Palette Integration

Take your fundraising workflow to the next level. Speed up your site management and stay in your creative flow with our new WordPress Command Palette integration.

Supercharge Your Workflow
Navigate your fundraising dashboard faster than ever.

The Ultimate Keyboard Shortcut Hit Cmd + K (or Ctrl + K) to launch the Command Palette and manage your campaigns instantly.

⚡ Instant Navigation: Jump directly to your Campaigns, Donations, or Settings from anywhere in the editor.

➕ Quick Create: Start a new fundraising campaign or add a manual donation with a single command.

Efficiency Redefined
The tools you need, exactly when you need them.

⚙️ Contextual Actions: See relevant Charitable commands based on whether you’re editing a page or viewing your reports.

🚀 Seamless Integration: Built directly into the WordPress core experience for a lightweight, native feel.

Improvement New Security

📣 New Security Features

We’ve introduced a suite of new security tools to give you total control over who accesses your forms, plus a new way to tidy up your database.

Advanced Security Suite

Layered protection: Cloudflare, ReCAPTCHA, IP Controls, and Rate Limiting.

We have overhauled our security settings to stop bots without blocking real donors.

  • 🤖 Flexible Protection: Choose between Google reCAPTCHA v3 or the privacy-first Cloudflare Turnstile to block bots invisible.

  • 🚦 Rate limiting: Stop spam floods by limiting how many submissions an IP address can make in a set timeframe.

  • 🛑 Total control: Use the new IP Blacklist to block bad actors instantly, or the IP Whitelist to let your team bypass checks during testing.

The Clean Donation Tool

Go from “Testing” to “Live” in seconds.

Finished setting up your site and need to get rid of all those test transactions?

  • 🧹 Sweep it clean: Bulk delete test donations and donor records with a single click.

  • 📉 Accurate reporting: Ensure your revenue stats are 100% accurate for launch day.

  • ⚙️ Reset sequences: Automatically resets sequential invoice numbering.