<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
    <channel>
        <title>Sample Code for Working with Custom Profile Fields in Module Development</title>
        <description> In a number of modules I have used custom profile fields to allow users to disable/enable features provided by the module.  I have been reusing the same base code to incorporate the custom profile fields and thought it would be nice to make that base code available to other module developers.  Feel free to use or abuse any of this code. :-)

This code allows mod devs to add custom profile fields without requiring template or profile work on the part of the forum admin. For the purpose of providing code with a working example, this code is set to create a custom profile field to allow the users to disable YOUR_FOO in YOUR_MOD

First, in your settings.php page you would use the following code:
[code=&amp;quot;php&amp;quot;]
//Add these lines before your form, to check if the necessary custom profile field have been created

foreach ($PHORUM[&amp;quot;PROFILE_FIELDS&amp;quot;] as $key =&amp;gt; $cstm_field) {
	if ($cstm_field[&amp;quot;name&amp;quot;] == &amp;quot;phorum_mod_YOUR_MOD_DISABLE_YOUR_FOO&amp;quot;) {
		if (isset($cstm_field[&amp;quot;deleted&amp;quot;]) &amp;amp;&amp;amp; $cstm_field[&amp;quot;deleted&amp;quot;] == TRUE) {
			$DISABLE_YOUR_FOO_status = 2;
		} else {
			$DISABLE_YOUR_FOO_status = 1;
		}
	}
}
if (!isset($DISABLE_YOUR_FOO_status)) {
	include_once(&amp;quot;./include/api/base.php&amp;quot;);
	include_once(&amp;quot;./include/api/custom_profile_fields.php&amp;quot;);
    phorum_api_custom_profile_field_configure(array (
    	&amp;#039;id&amp;#039;            =&amp;gt; NULL,
    	&amp;#039;name&amp;#039;          =&amp;gt; &amp;#039;phorum_mod_YOUR_MOD_DISABLE_YOUR_FOO&amp;#039;,
    	&amp;#039;length&amp;#039;        =&amp;gt; 1,
    	&amp;#039;html_disabled&amp;#039; =&amp;gt; TRUE,
    	&amp;#039;show_in_admin&amp;#039; =&amp;gt; TRUE,
	));
    $DISABLE_YOUR_FOO_status = 1;
}

//add these lines to your form:

if ($DISABLE_YOUR_FOO_status == 2) {
	$frm-&amp;gt;addmessage(&amp;quot;Please add the deleted custom profile field named \&amp;quot;phorum_mod_YOUR_MOD_DISABLE_YOUR_FOO\&amp;quot; if you would like to allow users to disable YOUR_FOO.&amp;quot;);
} else {
	$frm-&amp;gt;addrow(&amp;quot;Allow users to disable YOUR_FOO: &amp;quot;, $frm-&amp;gt;checkbox(&amp;quot;allow_DISABLE_YOUR_FOO&amp;quot;, &amp;quot;1&amp;quot;, &amp;quot;&amp;quot;, $PHORUM[&amp;quot;phorum_mod_YOUR_MOD&amp;quot;][&amp;quot;allow_DISABLE_YOUR_FOO&amp;quot;]));
}
[/code]

Next, in your /lang folder you would create a language file or add these lines for each language that you want to include in your mod (sample english.php shown):
[code=&amp;quot;php&amp;quot;]
&amp;lt;?php
$PHORUM[&amp;quot;DATA&amp;quot;][&amp;quot;LANG&amp;quot;][&amp;quot;phorum_mod_YOUR_MOD&amp;quot;][&amp;quot;DisableYOUR_MOD&amp;quot;] = &amp;quot;Disable YOUR_FOO:&amp;quot;;
$PHORUM[&amp;quot;DATA&amp;quot;][&amp;quot;LANG&amp;quot;][&amp;quot;phorum_mod_YOUR_MOD&amp;quot;][&amp;quot;DisableYOUR_MODYes&amp;quot;] = &amp;quot;Yes&amp;quot;;
$PHORUM[&amp;quot;DATA&amp;quot;][&amp;quot;LANG&amp;quot;][&amp;quot;phorum_mod_YOUR_MOD&amp;quot;][&amp;quot;DisableYOUR_MODNo&amp;quot;] = &amp;quot;No&amp;quot;;
?&amp;gt;
[/code]

Next, you would add this function to your mod to show the custom profile field in the Control Center. In this sample, the custom profile field will be shown on the Forum Settings page:
[code=&amp;quot;php&amp;quot;]
function phorum_mod_YOUR_MOD_tpl_cc_usersettings($profile)
{
	global $PHORUM;
	
	//show the option to allow users to disable YOUR_FOO on the forum settings page
	if (!empty($profile[&amp;quot;PANEL&amp;quot;]) &amp;amp;&amp;amp; $profile[&amp;quot;PANEL&amp;quot;] == &amp;quot;forum&amp;quot;) {

		//only if the option to allow users to disable YOUR_FOO has been enabled
		if (!empty($PHORUM[&amp;quot;phorum_mod_YOUR_MOD&amp;quot;][&amp;quot;allow_DISABLE_YOUR_FOO&amp;quot;])) {
			foreach ($PHORUM[&amp;quot;PROFILE_FIELDS&amp;quot;] as $key =&amp;gt; $cstm_field) {
				if ($cstm_field[&amp;quot;name&amp;quot;] == &amp;quot;phorum_mod_YOUR_MOD_DISABLE_YOUR_FOO&amp;quot;) {
					if (!empty($cstm_field[&amp;quot;deleted&amp;quot;]) &amp;amp;&amp;amp; $cstm_field[&amp;quot;deleted&amp;quot;] == TRUE) {
						$DISABLE_YOUR_FOO_status = 2;
					} else {
						$DISABLE_YOUR_FOO_status = 1;
					}
				}
			}
			//only if the custom profile field has been installed and not deleted
			if ($DISABLE_YOUR_FOO_status == 1) {
				$currval = isset($profile[&amp;quot;phorum_mod_YOUR_MOD_DISABLE_YOUR_FOO&amp;quot;]);
				print &amp;quot;&amp;lt;dt&amp;gt;&amp;quot;.$PHORUM[&amp;quot;DATA&amp;quot;][&amp;quot;LANG&amp;quot;][&amp;quot;phorum_mod_YOUR_MOD&amp;quot;][&amp;quot;DisableYourFoo&amp;quot;];
				?&amp;gt;&amp;lt;/dt&amp;gt;
					&amp;lt;dd&amp;gt;&amp;lt;select name=&amp;quot;phorum_mod_YOUR_MOD_DISABLE_YOUR_FOO&amp;quot;&amp;gt;
						&amp;lt;option value=&amp;quot;1&amp;quot;&amp;lt;?php
						if ($PHORUM[&amp;quot;DATA&amp;quot;][&amp;quot;PROFILE&amp;quot;][&amp;quot;phorum_mod_YOUR_MOD_DISABLE_YOUR_FOO&amp;quot;] == 1) {
							?&amp;gt; selected=&amp;quot;selected&amp;quot;&amp;lt;?php
						}
				print &amp;quot;&amp;gt;&amp;quot;.$PHORUM[&amp;quot;DATA&amp;quot;][&amp;quot;LANG&amp;quot;][&amp;quot;phorum_mod_YOUR_MOD&amp;quot;][&amp;quot;DisableYourFooYes&amp;quot;];
				?&amp;gt;&amp;lt;/option&amp;gt;
				&amp;lt;option value=&amp;quot;0&amp;quot;&amp;lt;?php
				if ($PHORUM[&amp;quot;DATA&amp;quot;][&amp;quot;PROFILE&amp;quot;][&amp;quot;phorum_mod_YOUR_MOD_DISABLE_YOUR_FOO&amp;quot;] == 0) {
					?&amp;gt; selected=&amp;quot;selected&amp;quot;&amp;lt;?php
				}
				print &amp;quot;&amp;gt;&amp;quot;.$PHORUM[&amp;quot;DATA&amp;quot;][&amp;quot;LANG&amp;quot;][&amp;quot;phorum_mod_YOUR_MOD&amp;quot;][&amp;quot;DisableYourFooNo&amp;quot;];
				?&amp;gt;&amp;lt;/option&amp;gt;&amp;lt;/select&amp;gt;&amp;lt;/dd&amp;gt;
				&amp;lt;?php
			}
		}
	}

	return $profile;
	
}
[/code]

To make use of this code, edit your info.txt file to include these lines as needed:

// add this line to show the custom profile field in the Control Center
hook: tpl_cc_usersettings|phorum_mod_YOUR_MOD_tpl_cc_usersettings

// add this line if not already in use
hook: lang|

Finally, here is one example of a way to make use of the custom profile field in your other mod functions:
[code=&amp;quot;php&amp;quot;]
function phorum_mod_YOUR_MOD_YOUR_HOOK() {

	//do not YOUR_FOO if the user has disabled YOUR_FOO with admin permission
	if (!empty($PHORUM[&amp;quot;phorum_mod_YOUR_MOD&amp;quot;][&amp;quot;allow_DISABLE_YOUR_FOO&amp;quot;]) 
		&amp;amp;&amp;amp; !empty($PHORUM[&amp;quot;user&amp;quot;][&amp;quot;phorum_mod_YOUR_MOD_DISABLE_YOUR_FOO&amp;quot;])) return;
[/code]

Obviously a number of things could be changed here.  In the sample provided, I am using a simple Yes/No field which stores 1 for Yes and 0 for No.  This code can be modified to store text or other data as well.

I hope this does help fellow mod devs working with custom profile fields and if anyone has questions on this code, please feel free to ask.</description>
        <link>https://www.phorum.org/support/read.php?62,133963,133963#msg-133963</link>
        <lastBuildDate>Sat, 08 Aug 2026 07:21:36 -0500</lastBuildDate>
        <generator>Phorum 6.0.4</generator>
        <item>
            <guid>https://www.phorum.org/support/read.php?62,133963,133963#msg-133963</guid>
            <title>Sample Code for Working with Custom Profile Fields in Module Development</title>
            <link>https://www.phorum.org/support/read.php?62,133963,133963#msg-133963</link>
            <description><![CDATA[ In a number of modules I have used custom profile fields to allow users to disable/enable features provided by the module.  I have been reusing the same base code to incorporate the custom profile fields and thought it would be nice to make that base code available to other module developers.  Feel free to use or abuse any of this code. :-)<br />
<br />
This code allows mod devs to add custom profile fields without requiring template or profile work on the part of the forum admin. For the purpose of providing code with a working example, this code is set to create a custom profile field to allow the users to disable YOUR_FOO in YOUR_MOD<br />
<br />
First, in your <b>settings.php</b> page you would use the following code:<br />
[code=&quot;php&quot;]<br />
//Add these lines before your form, to check if the necessary custom profile field have been created<br />
<br />
foreach ($PHORUM[&quot;PROFILE_FIELDS&quot;] as $key =&gt; $cstm_field) {<br />
	if ($cstm_field[&quot;name&quot;] == &quot;phorum_mod_YOUR_MOD_DISABLE_YOUR_FOO&quot;) {<br />
		if (isset($cstm_field[&quot;deleted&quot;]) &amp;&amp; $cstm_field[&quot;deleted&quot;] == TRUE) {<br />
			$DISABLE_YOUR_FOO_status = 2;<br />
		} else {<br />
			$DISABLE_YOUR_FOO_status = 1;<br />
		}<br />
	}<br />
}<br />
if (!isset($DISABLE_YOUR_FOO_status)) {<br />
	include_once(&quot;./include/api/base.php&quot;);<br />
	include_once(&quot;./include/api/custom_profile_fields.php&quot;);<br />
    phorum_api_custom_profile_field_configure(array (<br />
    	&#039;id&#039;            =&gt; NULL,<br />
    	&#039;name&#039;          =&gt; &#039;phorum_mod_YOUR_MOD_DISABLE_YOUR_FOO&#039;,<br />
    	&#039;length&#039;        =&gt; 1,<br />
    	&#039;html_disabled&#039; =&gt; TRUE,<br />
    	&#039;show_in_admin&#039; =&gt; TRUE,<br />
	));<br />
    $DISABLE_YOUR_FOO_status = 1;<br />
}<br />
<br />
//add these lines to your form:<br />
<br />
if ($DISABLE_YOUR_FOO_status == 2) {<br />
	$frm-&gt;addmessage(&quot;Please add the deleted custom profile field named \&quot;phorum_mod_YOUR_MOD_DISABLE_YOUR_FOO\&quot; if you would like to allow users to disable YOUR_FOO.&quot;);<br />
} else {<br />
	$frm-&gt;addrow(&quot;Allow users to disable YOUR_FOO: &quot;, $frm-&gt;checkbox(&quot;allow_DISABLE_YOUR_FOO&quot;, &quot;1&quot;, &quot;&quot;, $PHORUM[&quot;phorum_mod_YOUR_MOD&quot;][&quot;allow_DISABLE_YOUR_FOO&quot;]));<br />
}<br />
[/code]<br />
<br />
Next, in your /lang folder you would create a language file or add these lines for each language that you want to include in your mod (sample <b>english.php</b> shown):<br />
[code=&quot;php&quot;]<br />
&lt;?php<br />
$PHORUM[&quot;DATA&quot;][&quot;LANG&quot;][&quot;phorum_mod_YOUR_MOD&quot;][&quot;DisableYOUR_MOD&quot;] = &quot;Disable YOUR_FOO:&quot;;<br />
$PHORUM[&quot;DATA&quot;][&quot;LANG&quot;][&quot;phorum_mod_YOUR_MOD&quot;][&quot;DisableYOUR_MODYes&quot;] = &quot;Yes&quot;;<br />
$PHORUM[&quot;DATA&quot;][&quot;LANG&quot;][&quot;phorum_mod_YOUR_MOD&quot;][&quot;DisableYOUR_MODNo&quot;] = &quot;No&quot;;<br />
?&gt;<br />
[/code]<br />
<br />
Next, you would add this function to your mod to show the custom profile field in the Control Center. In this sample, the custom profile field will be shown on the Forum Settings page:<br />
[code=&quot;php&quot;]<br />
function phorum_mod_YOUR_MOD_tpl_cc_usersettings($profile)<br />
{<br />
	global $PHORUM;<br />
	<br />
	//show the option to allow users to disable YOUR_FOO on the forum settings page<br />
	if (!empty($profile[&quot;PANEL&quot;]) &amp;&amp; $profile[&quot;PANEL&quot;] == &quot;forum&quot;) {<br />
<br />
		//only if the option to allow users to disable YOUR_FOO has been enabled<br />
		if (!empty($PHORUM[&quot;phorum_mod_YOUR_MOD&quot;][&quot;allow_DISABLE_YOUR_FOO&quot;])) {<br />
			foreach ($PHORUM[&quot;PROFILE_FIELDS&quot;] as $key =&gt; $cstm_field) {<br />
				if ($cstm_field[&quot;name&quot;] == &quot;phorum_mod_YOUR_MOD_DISABLE_YOUR_FOO&quot;) {<br />
					if (!empty($cstm_field[&quot;deleted&quot;]) &amp;&amp; $cstm_field[&quot;deleted&quot;] == TRUE) {<br />
						$DISABLE_YOUR_FOO_status = 2;<br />
					} else {<br />
						$DISABLE_YOUR_FOO_status = 1;<br />
					}<br />
				}<br />
			}<br />
			//only if the custom profile field has been installed and not deleted<br />
			if ($DISABLE_YOUR_FOO_status == 1) {<br />
				$currval = isset($profile[&quot;phorum_mod_YOUR_MOD_DISABLE_YOUR_FOO&quot;]);<br />
				print &quot;&lt;dt&gt;&quot;.$PHORUM[&quot;DATA&quot;][&quot;LANG&quot;][&quot;phorum_mod_YOUR_MOD&quot;][&quot;DisableYourFoo&quot;];<br />
				?&gt;&lt;/dt&gt;<br />
					&lt;dd&gt;&lt;select name=&quot;phorum_mod_YOUR_MOD_DISABLE_YOUR_FOO&quot;&gt;<br />
						&lt;option value=&quot;1&quot;&lt;?php<br />
						if ($PHORUM[&quot;DATA&quot;][&quot;PROFILE&quot;][&quot;phorum_mod_YOUR_MOD_DISABLE_YOUR_FOO&quot;] == 1) {<br />
							?&gt; selected=&quot;selected&quot;&lt;?php<br />
						}<br />
				print &quot;&gt;&quot;.$PHORUM[&quot;DATA&quot;][&quot;LANG&quot;][&quot;phorum_mod_YOUR_MOD&quot;][&quot;DisableYourFooYes&quot;];<br />
				?&gt;&lt;/option&gt;<br />
				&lt;option value=&quot;0&quot;&lt;?php<br />
				if ($PHORUM[&quot;DATA&quot;][&quot;PROFILE&quot;][&quot;phorum_mod_YOUR_MOD_DISABLE_YOUR_FOO&quot;] == 0) {<br />
					?&gt; selected=&quot;selected&quot;&lt;?php<br />
				}<br />
				print &quot;&gt;&quot;.$PHORUM[&quot;DATA&quot;][&quot;LANG&quot;][&quot;phorum_mod_YOUR_MOD&quot;][&quot;DisableYourFooNo&quot;];<br />
				?&gt;&lt;/option&gt;&lt;/select&gt;&lt;/dd&gt;<br />
				&lt;?php<br />
			}<br />
		}<br />
	}<br />
<br />
	return $profile;<br />
	<br />
}<br />
[/code]<br />
<br />
To make use of this code, edit your <b>info.txt</b> file to include these lines as needed:<br />
<pre class="bbcode">
// add this line to show the custom profile field in the Control Center
hook: tpl_cc_usersettings|phorum_mod_YOUR_MOD_tpl_cc_usersettings

// add this line if not already in use
hook: lang|</pre>
<br />
Finally, here is one example of a way to make use of the custom profile field in your other mod functions:<br />
[code=&quot;php&quot;]<br />
function phorum_mod_YOUR_MOD_YOUR_HOOK() {<br />
<br />
	//do not YOUR_FOO if the user has disabled YOUR_FOO with admin permission<br />
	if (!empty($PHORUM[&quot;phorum_mod_YOUR_MOD&quot;][&quot;allow_DISABLE_YOUR_FOO&quot;]) <br />
		&amp;&amp; !empty($PHORUM[&quot;user&quot;][&quot;phorum_mod_YOUR_MOD_DISABLE_YOUR_FOO&quot;])) return;<br />
[/code]<br />
<br />
Obviously a number of things could be changed here.  In the sample provided, I am using a simple Yes/No field which stores 1 for Yes and 0 for No.  This code can be modified to store text or other data as well.<br />
<br />
I hope this does help fellow mod devs working with custom profile fields and if anyone has questions on this code, please feel free to ask.]]></description>
            <dc:creator>Joe Curia</dc:creator>
            <category>v5/v6 Modules/Add-Ons</category>
            <pubDate>Fri, 17 Oct 2008 11:16:48 -0500</pubDate>
        </item>
    </channel>
</rss>
