<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Dan Polant :: Web Interaction Designer &#187; how to</title>
	<atom:link href="http://danpolant.com/category/how-to/feed/" rel="self" type="application/rss+xml" />
	<link>http://danpolant.com</link>
	<description></description>
	<lastBuildDate>Fri, 04 May 2012 04:24:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Dynamically creating views in Drupal</title>
		<link>http://danpolant.com/dynamically-creating-views-in-drupal/</link>
		<comments>http://danpolant.com/dynamically-creating-views-in-drupal/#comments</comments>
		<pubDate>Wed, 05 Oct 2011 00:09:34 +0000</pubDate>
		<dc:creator>dan</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[drupal]]></category>
		<category><![CDATA[how to]]></category>

		<guid isPermaLink="false">http://danpolant.com/?p=547</guid>
		<description><![CDATA[The Views module can be broken down into three main parts: the query builder, the display builder and the user interface. Usually, we use the interface to control the other two parts, but sometimes situations call for query and display logic that the interface cannot provide.
A good introduction to dynamic views is the function views_embed_view(). [...]]]></description>
			<content:encoded><![CDATA[<p>The Views module can be broken down into three main parts: the query builder, the display builder and the user interface. Usually, we use the interface to control the other two parts, but sometimes situations call for query and display logic that the interface cannot provide.</p>
<p>A good introduction to dynamic views is the function <code><a href="http://drupalcontrib.org/api/drupal/contributions--views--views.module/function/views_embed_view/6">views_embed_view()</a></code>. Using this, one can render the finished display of a view stored in the database or in code. It returns rendered HTML:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="re0">$display</span> = views_embed_view<span class="br0">&#40;</span><span class="st0">&quot;name_of_view&quot;</span>, <span class="st0">&quot;display_id&quot;</span>, <span class="re0">$arg1</span>, <span class="re0">$arg2</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>Often this function is a good alternative to using views blocks, for situations where the view display should be more tightly integrated in a certain area. By using <code>func_get_args()</code>, <code>views_embed_view</code> lets you pass arguments into the view that would normally have to be in the URL. </p>
<p>However, there are situations where views_embed_view just won&#8217;t cut it. Here are some examples:</p>
<p>1) The user needs be able to choose what filters/fields exist in the view.<br />
2) Groupings need to be dynamically generated based on user input</p>
<p>The functionality listed above cannot be achieved merely by passing arguments into the view. Rather, one must employ methods of the View object ([views module]/includes/view.inc) in order to build up elements of the view from the foundation.</p>
<p>This approach starts with the function <code><a href="http://drupalcontrib.org/api/drupal/contributions--views--views.module/function/views_get_view/6">views_get_view()</a></code>. This function loads a view from any storage, but does not execute or render it. </p>
<p>Now comes the most important function to master, <code>View::add_item()</code>. The code is in views/includes/view.inc. The following snippet demonstrates how to add a node id filter to a view named &#8220;example.&#8221;</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="re0">$view</span> = views_get_view<span class="br0">&#40;</span><span class="st0">&#8216;example&#8217;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="re0">$options</span> = <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; <span class="st0">&#8216;value&#8217;</span> =&gt; <span class="nu0">5</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="re0">$view</span>-&gt;<span class="me1">add_item</span><span class="br0">&#40;</span><span class="st0">&#8216;default&#8217;</span>, <span class="st0">&#8216;filter&#8217;</span>, <span class="st0">&#8216;node&#8217;</span>, <span class="st0">&#8216;nid&#8217;</span>, <span class="re0">$options</span>, <span class="st0">&#8216;nid_identifier&#8217;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>Here is a breakdown of arguments found in <code>View::add_item</code>:</p>
<p>1) display id<br />
2) type &#8211; can be filter, relationship, field, sort<br />
3) table &#8211; can also be table alias<br />
4) options &#8211; see below<br />
5) identifier &#8211; an internal reference to this filter, useful for establishing predictable dynamic keys for fields/filters/relationships that may get added twice. If you don&#8217;t pass anything for this argument, Views will create a unique id using an incremental variable if necessary.</p>
<p>Here is a breakdown of the <code>$options</code> argument, at least that of it which I have used:</p>
<p>1) value &#8211; meaningful only to filters. Specifies the conditions that will pass the filter. This can be an array in order to imply the IN operator<br />
2) operator &#8211; may assume any of the standard db operators. It is not necessary to specify IN if you provide an array as value.<br />
3) relationship &#8211; specifies a relationship for this item to use. Remember that doing this will require that you change the table alias to reflect the relationship table alias.<br />
4) group &#8211; filters only. Specifies a group for this filter to be a part of. Numeric or string values will work.</p>
<p>The majority of the work can be done with the <code>add_item</code> function. If I were to choose one more important method to learn within this subject it would be <code>set_option()</code>. Suppose you have defined several filters as being part of groups 0 and 1. Here is how you would use <code>set_option</code> to tell Views what to do with those groupings:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="re0">$groups</span> = <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="nu0">0</span> =&gt; <span class="st0">&#8216;OR&#8217;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="nu0">1</span> =&gt; <span class="st0">&#8216;AND&#8217;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="re0">$view</span>-&gt;<span class="me1">display_handler</span>-&gt;<span class="me1">set_option</span><span class="br0">&#40;</span><span class="st0">&#8216;filter_groups&#8217;</span>, <span class="re0">$groups</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>This snippet would create two conditional groups, 0 conjoined by OR and 1 conjoined by AND.</p>
<p>Set_option is a method of the views handler, not the view. It can be used to set any of the other characteristics defined the in the options element of the handler. It can do the same things as <code>add_item</code> but <code>add_item</code> performs additional procedures that help things come out the way you want (in most cases) and is definitely more useful for adding filters, relationships, fields and sorts.</p>
<p>Once you have your view prepared the way you want it, you need to execute and probably render it. The following code will do just that:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="re0">$view</span>-&gt;<span class="me1">init_display</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="re0">$view</span>-&gt;<span class="me1">pre_execute</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="re0">$view</span>-&gt;<span class="me1">execute</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// at this point, your view will have results added</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="re0">$view</span>-&gt;<span class="me1">post_execute</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; </div>
</li>
<li class="li2">
<div class="de2">&nbsp; <span class="co1">// this is the final output &nbsp;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="re0">$output</span> = <span class="re0">$view</span>-&gt;<span class="me1">display_handler</span>-&gt;<span class="me1">preview</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>There is a lot more you can do with dynamic views than I have outlined here. Generally, the best way to learn more about this is to first use the UI to build a view that contains the elements and characteristics that you want to dynamically add in your function. Then look at the export of that view. You will gain a good understanding of what the $view object and its display handler need to look like in code. Having a PHP debugger is also immensely helpful, but short of that, just use <code>dpm</code> liberally.</p>
]]></content:encoded>
			<wfw:commentRss>http://danpolant.com/dynamically-creating-views-in-drupal/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to: Force download of links in Magento</title>
		<link>http://danpolant.com/how-to-force-download-of-links-in-magento/</link>
		<comments>http://danpolant.com/how-to-force-download-of-links-in-magento/#comments</comments>
		<pubDate>Tue, 08 Jun 2010 19:02:34 +0000</pubDate>
		<dc:creator>dan</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[how to]]></category>
		<category><![CDATA[.htaccess]]></category>
		<category><![CDATA[Magento]]></category>

		<guid isPermaLink="false">http://danpolant.com/?p=413</guid>
		<description><![CDATA[It's an easy fix. I'll tell you about the hack way to do it, if you like doing it the right way have fun creating 7-8 files.]]></description>
			<content:encoded><![CDATA[<p>Real quick: find DownloadController.php and look at lines 78 &#8211; 81:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"> &nbsp; &nbsp; &nbsp; &nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">if</span> <span class="br0">&#40;</span><span class="re0">$contentDisposition</span> = <span class="re0">$helper</span>-&gt;<span class="me1">getContentDisposition</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">getResponse</span><span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-&gt;<span class="me1">setHeader</span><span class="br0">&#40;</span><span class="st0">&#8216;Content-Disposition&#8217;</span>, <span class="re0">$contentDisposition</span> . <span class="st0">&#8216;; filename=&#8217;</span>.<span class="re0">$fileName</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Change line 80 (line 3 in this excerpt) to:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">-&gt;<span class="me1">setHeader</span><span class="br0">&#40;</span><span class="st0">&#8216;Content-Disposition&#8217;</span>, <span class="st0">&#8216;attachment&#8217;</span> . <span class="st0">&#8216;; filename=&#8217;</span>.<span class="re0">$fileName</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>That should make it work. As far as I know, the .htaccess solutions to this problem don&#8217;t work in Magento, because Magento encrypts the filename and extension, and the path does not represent a real location. </p>
<p>Note: I usually don&#8217;t go around hacking the core files of a CMS, but sometimes I make an exception with deep extensions within Magento because it just takes so darn long to follow the variables through to their various sources in the code. Not that I don&#8217;t know how to do it &#8230;</p>
<p>By the way, if you&#8217;re not using Magento and just have regular paths to your files, put this in your .htaccess:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">AddType application/octet-stream .mov .mp3 .zip</div>
</li>
</ol>
</div>
<p><a href="http://www.askapache.com/htaccess/htaccess.html">This</a> is one of the best .htaccess resources I&#8217;ve seen and it explains by example.</p>
<p>You can of course change the list of extension if you want. Hope this helps.</p>
]]></content:encoded>
			<wfw:commentRss>http://danpolant.com/how-to-force-download-of-links-in-magento/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>How to: Make sure Buddypress loads first</title>
		<link>http://danpolant.com/how-to-make-sure-buddypress-loads-first/</link>
		<comments>http://danpolant.com/how-to-make-sure-buddypress-loads-first/#comments</comments>
		<pubDate>Mon, 18 Jan 2010 22:18:31 +0000</pubDate>
		<dc:creator>dan</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[how to]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[buddypress]]></category>
		<category><![CDATA[class]]></category>

		<guid isPermaLink="false">http://danpolant.com/?p=259</guid>
		<description><![CDATA[Often, it doesn&#8217;t matter whether your Buddypress plugin loads before or after Buddypress. However, if you are using groups API, or extending other Buddypress plugins, your class extensions require that Buddypress be loaded beforehand. Functions hooked into actions that occur before Buddypress loads that use Buddypress functions will cause problems as well if the load [...]]]></description>
			<content:encoded><![CDATA[<p>Often, it doesn&#8217;t matter whether your Buddypress plugin loads before or after Buddypress. However, if you are using groups API, or extending other Buddypress plugins, your class extensions require that Buddypress be loaded beforehand. Functions hooked into actions that occur before Buddypress loads that use Buddypress functions will cause problems as well if the load order is wrong.</p>
<p>WordPress loads plugins in alphabetical order. So the simplest (and most janky) solution is to name your plugin something that comes after &#8220;buddypress&#8221; in the alphabet. But there are better methods that you should use instead.</p>
<h4>For versions before BP 1.2</h4>
<p>The goal here is to check whether Buddypress has been loaded at the point when your plugin loads, and if it has not, load it manually. The manual load will prevent the normal load from happening later, so you won&#8217;t get double what you need.</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="co1">//hack required to load BP first</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">function</span> bpgc_load_buddypress<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//buddypress is loaded</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span> <a href="http://www.php.net/function_exists"><span class="kw3">function_exists</span></a><span class="br0">&#40;</span> <span class="st0">&#8216;bp_core_setup_globals&#8217;</span> <span class="br0">&#41;</span> <span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">false</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// Get the list of active sitewide plugins </span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$active_sitewide_plugins</span> = maybe_unserialize<span class="br0">&#40;</span> get_site_option<span class="br0">&#40;</span> <span class="st0">&#8216;active_sitewide_plugins&#8217;</span> <span class="br0">&#41;</span> <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$bp_activated</span> = <span class="re0">$active_sitewide_plugins</span><span class="br0">&#91;</span><span class="st0">&#8216;buddypress/bp-loader.php&#8217;</span><span class="br0">&#93;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//bp is not activated</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span> !<span class="re0">$bp_activated</span> <span class="br0">&#41;</span><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">false</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//bp is activated but not yet loaded</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span> <span class="re0">$bp_activated</span> <span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">true</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">false</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">//load bp if its not activated</span></div>
</li>
<li class="li2">
<div class="de2"><span class="kw1">if</span> <span class="br0">&#40;</span> bpgc_load_buddypress<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">require_once</span><span class="br0">&#40;</span> WP_PLUGIN_DIR . <span class="st0">&#8216;/buddypress/bp-loader.php&#8217;</span> <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<h4>If you are using BP 1.2+</h4>
<p>Fortunately, they made this much easier to do in BP 1.2. For my plugin, all I needed to do was:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">function</span> bpgc_bp_loaded<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">require</span> <span class="br0">&#40;</span> WP_PLUGIN_DIR . <span class="st0">&quot;/BP-Group-Control/bpgc-classes.php&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">add_action<span class="br0">&#40;</span><span class="st0">&#8216;bp_init&#8217;</span>, <span class="st0">&#8216;bpgc_bp_loaded&#8217;</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>&#8230; in order to load my classes only once Buddypress has finished loading. Probably the safest way to ensure that your plugin completely loads after Buddypress is to create a loader.php file that contains this action and loads all of your other files. </p>
]]></content:encoded>
			<wfw:commentRss>http://danpolant.com/how-to-make-sure-buddypress-loads-first/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to: use email addresses as usernames in Buddypress</title>
		<link>http://danpolant.com/how-to-use-email-addresses-as-usernames-in-buddypress/</link>
		<comments>http://danpolant.com/how-to-use-email-addresses-as-usernames-in-buddypress/#comments</comments>
		<pubDate>Mon, 11 Jan 2010 01:13:43 +0000</pubDate>
		<dc:creator>dan</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[how to]]></category>
		<category><![CDATA[buddypress]]></category>
		<category><![CDATA[email address]]></category>

		<guid isPermaLink="false">http://danpolant.com/?p=202</guid>
		<description><![CDATA[Just spent about 5 hours on this one, enjoy &#8230;
I&#8217;ve seen this question come up on a lot of forums &#8211; people want to use email addresses as usernames in Buddypress. It makes sense &#8211; emails are easy to remember. But Buddypress has made this difficult to do. Andy Peatling added the following constant to [...]]]></description>
			<content:encoded><![CDATA[<p>Just spent about 5 hours on this one, enjoy &#8230;</p>
<p>I&#8217;ve seen this question come up on a lot of forums &#8211; people want to use email addresses as usernames in Buddypress. It makes sense &#8211; emails are easy to remember. But Buddypress has made this difficult to do. Andy Peatling added the following constant to the core:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><a href="http://www.php.net/define"><span class="kw3">define</span></a><span class="br0">&#40;</span> <span class="st0">&#8216;BP_ENABLE_USERNAME_COMPATIBILITY_MODE&#8217;</span>, <span class="kw2">true</span> <span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>in order to deal with part of this problem. But this did not solve the problem completely. See <a href="http://codex.buddypress.org/how-to-guides/changing-internal-configuration-settings/">this article</a> for how to use this. This changed allowed users to navigate to an @-containing username&#8217;s profile, but that user could not do things like create groups or edit his profile.</p>
<p>It turns out there&#8217;s another step to getting this to work. You have to override <code>wp_sanitize_redirect</code> in wp-includes/pluggable.php and add &#8216;@&#8217; to the list of OK characters, like this:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">function</span> wp_sanitize_redirect<span class="br0">&#40;</span><span class="re0">$location</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$location</span> = <a href="http://www.php.net/preg_replace"><span class="kw3">preg_replace</span></a><span class="br0">&#40;</span><span class="st0">&#8216;|[^a-z0-9-~+_.?#=&amp;;,/:%!@]|i&#8217;</span>, <span class="st0">&#8221;</span>, <span class="re0">$location</span><span class="br0">&#41;</span>; <span class="co1">//this line needs to have an @</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$location</span> = wp_kses_no_null<span class="br0">&#40;</span><span class="re0">$location</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// remove %0d and %0a from location</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$strip</span> = <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="st0">&#8216;%0d&#8217;</span>, <span class="st0">&#8216;%0a&#8217;</span>, <span class="st0">&#8216;%0D&#8217;</span>, <span class="st0">&#8216;%0A&#8217;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$location</span> = _deep_replace<span class="br0">&#40;</span><span class="re0">$strip</span>, <span class="re0">$location</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="re0">$location</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Now it should work. </p>
]]></content:encoded>
			<wfw:commentRss>http://danpolant.com/how-to-use-email-addresses-as-usernames-in-buddypress/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How to: alter the content of activity items</title>
		<link>http://danpolant.com/how-to-alter-the-content-of-activity-items/</link>
		<comments>http://danpolant.com/how-to-alter-the-content-of-activity-items/#comments</comments>
		<pubDate>Thu, 19 Nov 2009 18:58:41 +0000</pubDate>
		<dc:creator>dan</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[how to]]></category>
		<category><![CDATA[buddypress]]></category>
		<category><![CDATA[filters]]></category>

		<guid isPermaLink="false">http://danpolant.com/?p=185</guid>
		<description><![CDATA[It puzzled me at first when I discovered that activity items are formatted as html before they are stored in the database. This reduces the workload upon retrieval but makes it almost impossible to edit the content of old entries.
Fortunately it is quite easy to change the way activity items are stored in the database. [...]]]></description>
			<content:encoded><![CDATA[<p>It puzzled me at first when I discovered that activity items are formatted as html before they are stored in the database. This reduces the workload upon retrieval but makes it almost impossible to edit the content of old entries.</p>
<p>Fortunately it is quite easy to change the way activity items are stored in the database. I&#8217;m going to use blog post activity items as an example, but this process is the same for just about every kind of activity item.</p>
<p>The first objective is to find the filter function that you want to hook into. For blog posts, look to line 385 of bp-blogs.php, inside function <code>bp_blogs_record_post()</code>. In general, to find these record functions, search the component core file for the string &#8216;record.&#8217; This will point you to both the utility function that hands the array off to <code>bp_activity_add()</code>, and to the function that a specific user task initiates to record an activity.</p>
<p>The filter we are interested in is bp_blogs_activity_new_post, in the segment:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">bp_blogs_record_activity<span class="br0">&#40;</span> <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;user_id&#8217;</span> =&gt; <span class="br0">&#40;</span>int<span class="br0">&#41;</span><span class="re0">$post</span>-&gt;<span class="me1">post_author</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;content&#8217;</span> =&gt; apply_filters<span class="br0">&#40;</span> <span class="st0">&#8216;bp_blogs_activity_new_post&#8217;</span>, <span class="re0">$activity_content</span>, &amp;amp;<span class="re0">$post</span>, <span class="re0">$post_permalink</span> <span class="br0">&#41;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;primary_link&#8217;</span> =&gt; apply_filters<span class="br0">&#40;</span> <span class="st0">&#8216;bp_blogs_activity_new_post_primary_link&#8217;</span>, <span class="re0">$post_permalink</span>, <span class="re0">$post_id</span> <span class="br0">&#41;</span>,</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;component_action&#8217;</span> =&gt; <span class="st0">&#8216;new_blog_post&#8217;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;item_id&#8217;</span> =&gt; <span class="re0">$recorded_post_id</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;recorded_time&#8217;</span> =&gt; <a href="http://www.php.net/strtotime"><span class="kw3">strtotime</span></a><span class="br0">&#40;</span> <span class="re0">$post</span>-&gt;<span class="me1">post_date</span> <span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span> <span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>Now, in our plugin, or in custom.php, add the following:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">add_filter<span class="br0">&#40;</span><span class="st0">&#8216;bp_blogs_activity_new_post&#8217;</span>, <span class="st0">&#8216;my_process&#8217;</span>, <span class="nu0">2</span>, <span class="nu0">3</span> <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">function</span> my_process<span class="br0">&#40;</span><span class="re0">$activity_content</span>, <span class="re0">$post</span>, <span class="re0">$post_permalink</span><span class="br0">&#41;</span><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$activity_content</span> = <a href="http://www.php.net/sprintf"><span class="kw3">sprintf</span></a><span class="br0">&#40;</span> __<span class="br0">&#40;</span> <span class="st0">&#8216;%s wrote a new blog post: %s&#8217;</span>, <span class="st0">&#8216;buddypress&#8217;</span> <span class="br0">&#41;</span>, bp_core_get_userlink<span class="br0">&#40;</span> <span class="br0">&#40;</span>int<span class="br0">&#41;</span><span class="re0">$post</span>-&gt;<span class="me1">post_author</span> <span class="br0">&#41;</span>, <span class="st0">&#8216;&lt;a href=&quot;&#8217;</span> . <span class="re0">$post_permalink</span> . <span class="st0">&#8216;&quot;&gt;&#8217;</span> . <span class="re0">$post</span>-&gt;<span class="me1">post_title</span> . <span class="st0">&#8216;&lt;/a&gt;&#8217;</span> <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$activity_content</span> .= <span class="st0">&quot;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="st0">&lt;blockquote&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="st0">&quot;</span> . bp_create_excerpt<span class="br0">&#40;</span> <span class="re0">$post</span>-&gt;<span class="me1">post_content</span>, <span class="nu0">25</span> <span class="br0">&#41;</span> . <span class="st0">&quot;&lt;/blockquote&gt;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="st0">&quot;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="re0">$activity_content</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<h4>Skip to the next heading if you understand filters, or don&#8217;t care!</h4>
<p>I will take a minute to explain filters, because they are a useful but somewhat obscure concept in WordPress. Filters are like actions, in that they provide an access point into code running in the WordPress framework. Filters are different from actions in that they return a value. It helps to think of <code>apply_filters()</code> as being analogous to <code>do_action()</code>. The first argument of <code>apply_filters()</code> is the tag that an <code>add_filter()</code> action hooks into with its first argument. This is not unlike the relationship between <code>add_action()</code> and <code>do_action()</code>.</p>
<p>The second to nth arguments in <code>apply_filters()</code> allow variables to be passed into your custom filter. Try and understand this diagram:</p>
<div id="attachment_187" class="wp-caption aligncenter" style="width: 514px"><img class="size-full wp-image-187 " title="wp-filters" src="http://danpolant.com/wp-content/uploads/2009/11/wp-filters.png" alt="wp-filters" width="504" height="289" /><p class="wp-caption-text">Information flow between apply_filters, add_filter, and custom filter function</p></div>
<h4>Back to the task!</h4>
<p>It would be nice if we could edit the individual components of <code>$activity_content</code>, but alas, it doesn&#8217;t have its own filter. That&#8217;s why some copying is necessary from the core file &#8211; but usually this code is pretty short, as you can see. It&#8217;s necessary to pass <code>$post, $post_permalink</code> into your filter function so that you can use these values in your activity content variable. </p>
<p>After returning the variable with your formatted feed, guess what, you&#8217;re done! <code>apply_filters()</code> will return the value of your function&#8217;s return. </p>
]]></content:encoded>
			<wfw:commentRss>http://danpolant.com/how-to-alter-the-content-of-activity-items/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to: Get Ajax working in Buddypress</title>
		<link>http://danpolant.com/how-to-get-ajax-working-in-buddypress/</link>
		<comments>http://danpolant.com/how-to-get-ajax-working-in-buddypress/#comments</comments>
		<pubDate>Tue, 17 Nov 2009 06:17:17 +0000</pubDate>
		<dc:creator>dan</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[how to]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[buddypress]]></category>

		<guid isPermaLink="false">http://danpolant.com/?p=169</guid>
		<description><![CDATA[Ajax can really streamline a site&#8217;s user experience. The WordPress/WPMU back-end owes much of its ease of use to the presence of many dynamic menus and feedback notices. No one has had time to implement much of this for Buddypress, but I&#8217;m going to show you how to put use Ajax in your Buddypress plugins.
Its [...]]]></description>
			<content:encoded><![CDATA[<p>Ajax can really streamline a site&#8217;s user experience. The WordPress/WPMU back-end owes much of its ease of use to the presence of many dynamic menus and feedback notices. No one has had time to implement much of this for Buddypress, but I&#8217;m going to show you how to put use Ajax in your Buddypress plugins.</p>
<p>Its not too hard, but a big barrier to getting it to work is keeping all of the connections straight. When you have something of this nature wrong it is hard to debug. </p>
<p>Lets make a button that loads a hello world via ajax. Start with whatever input fields you need, and put a <code>wp_nonce_field()</code> underneath. Its a good idea to put all of this inside a form with an action, because you want people to be able to use your plugin even if they can&#8217;t run Javascript for some silly reason!</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&lt;form action=<span class="st0">&quot;somewhere.com/yeah&quot;</span> id=<span class="st0">&quot;my-form&quot;</span>&gt;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &lt;label <span class="kw1">for</span>=<span class="st0">&quot;username&quot;</span>&gt;Name: &lt;/label&gt;&lt;input type=<span class="st0">&quot;text&quot;</span> name=<span class="st0">&quot;name&quot;</span>&gt;&lt;/input&gt;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &lt;label <span class="kw1">for</span>=<span class="st0">&quot;email&quot;</span>&gt;Email: &lt;/label&gt;&lt;input type=<span class="st0">&quot;text&quot;</span> name=<span class="st0">&quot;email&quot;</span>&gt;&lt;/input&gt;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; <span class="kw2">&lt;?php</span> wp_nonce_field<span class="br0">&#40;</span> <span class="st0">&#8216;save&#8217;</span>, <span class="st0">&#8216;_wpnonce-save&#8217;</span> <span class="br0">&#41;</span>; <span class="kw2">?&gt;</span> &nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &lt;input type=<span class="st0">&quot;submit&quot;</span> name=<span class="st0">&quot;save&quot;</span> id=<span class="st0">&quot;save&quot;</span> value = <span class="st0">&quot;Add this member&quot;</span>/&gt;</div>
</li>
<li class="li1">
<div class="de1">&lt;span <span class="kw2">class</span>=<span class="st0">&quot;ajax-loader&quot;</span>&gt;&lt;/span&gt; <span class="co1">//for fun, buddypress knows what this is</span></div>
</li>
<li class="li1">
<div class="de1">&lt;/form&gt;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p><a href="http://codex.wordpress.org/Function_Reference/wp_enqueue_script">Enqueue</a> a javacript file that has something like this:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">jQuery<span class="br0">&#40;</span>document<span class="br0">&#41;</span>.<span class="me1">ready</span><span class="br0">&#40;</span><span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; jQuery<span class="br0">&#40;</span><span class="st0">&#8216;#my-form input#save&#8217;</span><span class="br0">&#41;</span>.<span class="me1">click</span><span class="br0">&#40;</span><span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jQuery<span class="br0">&#40;</span><span class="st0">&#8216;.ajax-loader&#8217;</span><span class="br0">&#41;</span>.<span class="me1">toggle</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//don&#8217;t let them click it a bunch of times in a row</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jQuery<span class="br0">&#40;</span><span class="st0">&#8216;#save&#8217;</span><span class="br0">&#41;</span>.<span class="me1">attr</span><span class="br0">&#40;</span><span class="br0">&#123;</span><span class="st0">&#8216;disabled&#8217;</span>: <span class="kw2">true</span><span class="br0">&#125;</span><span class="br0">&#41;</span>; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jQuery.<span class="me1">post</span><span class="br0">&#40;</span> ajaxurl, <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; action: <span class="st0">&#8216;my_save&#8217;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;cookie&#8217;</span>: encodeURIComponent<span class="br0">&#40;</span>document.<span class="me1">cookie</span><span class="br0">&#41;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;_wpnonce&#8217;</span>: jQuery<span class="br0">&#40;</span><span class="st0">&quot;input#_wpnonce-save&quot;</span><span class="br0">&#41;</span>.<span class="me1">val</span><span class="br0">&#40;</span><span class="br0">&#41;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;username&#8217;</span>: jQuery<span class="br0">&#40;</span><span class="st0">&quot;input[name='username']&quot;</span><span class="br0">&#41;</span>.<span class="me1">val</span><span class="br0">&#40;</span><span class="br0">&#41;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;name&#8217;</span>: jQuery<span class="br0">&#40;</span><span class="st0">&quot;input[name='name']&quot;</span><span class="br0">&#41;</span>.<span class="me1">val</span><span class="br0">&#40;</span><span class="br0">&#41;</span>,</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">function</span><span class="br0">&#40;</span>response<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jQuery<span class="br0">&#40;</span><span class="st0">&#8216;.ajax-loader&#8217;</span><span class="br0">&#41;</span>.<span class="me1">toggle</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jQuery<span class="br0">&#40;</span><span class="st0">&#8216;#save&#8217;</span><span class="br0">&#41;</span>.<span class="me1">attr</span><span class="br0">&#40;</span><span class="br0">&#123;</span><span class="st0">&#8216;disabled&#8217;</span>: <span class="kw2">false</span><span class="br0">&#125;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jQuery<span class="br0">&#40;</span><span class="st0">&#8216;#my-form&#8217;</span><span class="br0">&#41;</span>.<span class="me1">append</span><span class="br0">&#40;</span>response<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="br0">&#125;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>We&#8217;re doing jQuery here. Basically this hi-jacks the submit button and causes javascript to run instead of the usual post to the action page. Read up on <a href="http://jquery.com">jQuery</a> if this code doesn&#8217;t make any sense.</p>
<p>But let me explain a few things about the <code>jQuery.post()</code>. <code>action</code>, <code>_wpnonce</code> and <code>cookie</code> are <code>$_REQUEST</code> variables that Buddypress understands &#8211; you must name these properties exactly as you see here. Additionally, their values must correspond to other values set in different places of the Ajax process. I&#8217;m going to communicate these necessary equalities in a series of rules. The first one is:</p>
<h4 style="font-style:normal;">_wpnonce&#8221; in the javascript file = id of your nonce_field (the second parameter of wp_nonce_field)</h4>
<p>Now its time to write our target php function. </p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">function</span> my_save_function<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; check_ajax_referer<span class="br0">&#40;</span><span class="st0">&#8216;save&#8217;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="st0">&quot;Sorry, I didn&#8217;t save anything&quot;</span>;</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">add_action<span class="br0">&#40;</span><span class="st0">&#8216;wp_ajax_my_save&#8217;</span>, <span class="st0">&#8216;my_save_function&#8217;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>Here&#8217;s the second rule:</p>
<h4 style="font-style:normal;">Bind your target function to an action named after the &#8220;action&#8221; property in your Ajax Javascript, and remember to add &#8220;wp_ajax_&#8221; to the beginning</h4>
<p>. . . And the third:</p>
<h4 style="font-style:normal;">Use the first argument (the action) of your wp_nonce_field() on the Ajax initiating template/screen function as the argument for check_admin_referer()</h4>
<p><code>check_ajax_referer</code> is a security thing &#8211; you need to do this. At the end of all of this, the message should appear dynamically at the end of your form when you press the button. Keeping the equalities straight is key.</p>
]]></content:encoded>
			<wfw:commentRss>http://danpolant.com/how-to-get-ajax-working-in-buddypress/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>How to: Make an element a widget sidebar</title>
		<link>http://danpolant.com/how-to-make-an-element-a-widget-sidebar/</link>
		<comments>http://danpolant.com/how-to-make-an-element-a-widget-sidebar/#comments</comments>
		<pubDate>Mon, 05 Oct 2009 03:25:48 +0000</pubDate>
		<dc:creator>dan</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[how to]]></category>
		<category><![CDATA[buddypress]]></category>
		<category><![CDATA[sidebar]]></category>
		<category><![CDATA[widget]]></category>

		<guid isPermaLink="false">http://danpolant.com/?p=80</guid>
		<description><![CDATA[Widgets are a great way to let people include something cool a bunch of times in their site. The problem is, widget areas are almost always predefined in themes, and usually confined to sidebar areas. So here&#8217;s how to make any html element into a widget container.
What exactly does this mean? It means that after [...]]]></description>
			<content:encoded><![CDATA[<p>Widgets are a great way to let people include something cool a bunch of times in their site. The problem is, widget areas are almost always predefined in themes, and usually confined to sidebar areas. So here&#8217;s how to make any html element into a widget container.</p>
<p>What exactly does this mean? It means that after we&#8217;re done, you will have something on the right of your appearance->widgets screen like:</p>
<div id="attachment_81" class="wp-caption aligncenter" style="width: 189px"><img class="size-full wp-image-81" title="New widget area" src="http://danpolant.com/wp-content/uploads/2009/10/widget-backend.PNG" alt="&quot;Groups Sidebar&quot; is not a default Buddypress widget" width="179" height="85" /><p class="wp-caption-text">&quot;Groups Sidebar&quot; is not a default widget</p></div>
<h4>There&#8217;s nothing special about code that runs inside a widget.</h4>
<p> A widget is similar to any other piece of template code, except that widget output is defined by the return value of a <a href="http://codex.wordpress.org/WordPress_Widgets_Api#Developing_Widgets_on_2.8.2B">special function and class</a>. When you put your custom code into the specified parts of this function, WordPress will treat that code as something that can be run and displayed in a specific widget area, depending on where you drag your widget in /wp-admin/widgets.php.  </p>
<p>Usually you want to define a widget area in your theme file. Lets say we want to add a widget area to the groups directory page (/directories/groups/index.php) in Buddypress. First find a place where you want to put your widget area div. For my example, I&#8217;m going to make a new div in /directories/groups/index.php. You can make a new one like I&#8217;m doing, or use an existing element in the template. I&#8217;ll put my new element right about here:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &lt;div id=<span class="st0">&quot;sidebar&quot;</span> <span class="kw2">class</span>=<span class="st0">&quot;directory-sidebar&quot;</span>&gt;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">&lt;?php</span> do_action<span class="br0">&#40;</span> <span class="st0">&#8216;bp_before_directory_groups_search&#8217;</span> <span class="br0">&#41;</span> <span class="kw2">?&gt;</span>&nbsp; &nbsp; &nbsp; &nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;div id=<span class="st0">&quot;groups-directory-search&quot;</span> <span class="kw2">class</span>=<span class="st0">&quot;directory-widget&quot;</span>&gt;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;h3&gt;&lt;?php _e<span class="br0">&#40;</span> <span class="st0">&#8216;Find Groups&#8217;</span>, <span class="st0">&#8216;buddypress&#8217;</span> <span class="br0">&#41;</span> ?&gt;&lt;/h3&gt;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">&lt;?php</span> bp_directory_groups_search_form<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw2">?&gt;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">&lt;?php</span> do_action<span class="br0">&#40;</span> <span class="st0">&#8216;bp_directory_groups_search&#8217;</span> <span class="br0">&#41;</span> <span class="kw2">?&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/div&gt;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">&lt;?php</span> do_action<span class="br0">&#40;</span> <span class="st0">&#8216;bp_after_directory_groups_search&#8217;</span> <span class="br0">&#41;</span> <span class="kw2">?&gt;</span>&nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &lt;div id=<span class="st0">&quot;groups-sidebar&quot;</span>&gt;&lt;!&#8211; my <span class="kw2">new</span> widget area will be element &#8211;&gt;</div>
</li>
</ol>
</div>
<p>This will make the area below the group search box widget friendly. Add the following lines of code underneath, like this:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"> &nbsp; &nbsp; &nbsp; &nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &lt;div id=<span class="st0">&quot;groups-sidebar&quot;</span>&gt;&lt;!&#8211; my <span class="kw2">new</span> widget area starts here &#8211;&gt;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">&lt;?php</span> <span class="kw1">if</span> <span class="br0">&#40;</span> !<a href="http://www.php.net/function_exists"><span class="kw3">function_exists</span></a><span class="br0">&#40;</span><span class="st0">&#8216;dynamic_sidebar&#8217;</span><span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; || !dynamic_sidebar<span class="br0">&#40;</span><span class="st0">&#8216;groups-sidebar&#8217;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span> : <span class="kw2">?&gt;</span></div>
</li>
</ol>
</div>
<p><small>see <a href="http://codex.wordpress.org/WordPress_Widgets_Api/dynamic_sidebar">http://codex.wordpress.org/WordPress_Widgets_Api/dynamic_sidebar</a></small></p>
<p>Basically this is saying &#8220;if nothing has called dynamic sidebar on the element with id &#8216;groups-sidebar,&#8217; execute the code that comes after this. This is how you add default sidebar content to display when users have not added any widgets</p>
<p>There is one more step to defining a widget area. You might want to do this in a simple plugin:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">add_action<span class="br0">&#40;</span><span class="st0">&#8216;init&#8217;</span>, <span class="st0">&#8216;custom_sidebar&#8217;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">function</span> custom_sidebar<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="re0">$args</span> = <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;name&#8217;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=&gt; <a href="http://www.php.net/sprintf"><span class="kw3">sprintf</span></a><span class="br0">&#40;</span>__<span class="br0">&#40;</span><span class="st0">&#8216;Sidebar %d&#8217;</span><span class="br0">&#41;</span>, <span class="re0">$i</span> <span class="br0">&#41;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;id&#8217;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=&gt; <span class="st0">&#8216;sidebar-$i&#8217;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;before_widget&#8217;</span> =&gt; <span class="st0">&#8216;&lt;li id=<span class="es0">\&#8221;</span>%1$s<span class="es0">\&#8221;</span> class=<span class="es0">\&#8221;</span>widget %2$s<span class="es0">\&#8221;</span>&gt;&#8217;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;after_widget&#8217;</span> &nbsp;=&gt; <span class="st0">&#8216;&lt;/li&gt;&#8217;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;before_title&#8217;</span> &nbsp;=&gt; <span class="st0">&#8216;&lt;h2 class=<span class="es0">\&#8221;</span>widgettitle<span class="es0">\&#8221;</span>&gt;&#8217;</span>,</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;after_title&#8217;</span> &nbsp; =&gt; <span class="st0">&#8216;&lt;/h2&gt;&#8217;</span> <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; register_sidebar<span class="br0">&#40;</span><span class="re0">$args</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p><small>source: <a href="http://codex.wordpress.org/Function_Reference/register_sidebar">http://codex.wordpress.org/Function_Reference/register_sidebar</a></small></p>
<p>You can modify these tags, but WordPress suggests that you leave the sprintf stuff alone for the sake of consistency. </p>
<h4>Your widget area should now show up in wp-admin/widgets.php</h4>
<p>It should display a properly formatted widget where ever you have it located. I found this process especially useful while editing Buddypress, because it seems like the groups and members pages should really be made widget friendly. </p>
<p>I suggest you read the documentation in <a href="http://codex.wordpress.org/WordPress_Widgets_Api">http://codex.wordpress.org/WordPress_Widgets_Api</a> for more information &#8211; this is basically how I learned to do this. </p>
]]></content:encoded>
			<wfw:commentRss>http://danpolant.com/how-to-make-an-element-a-widget-sidebar/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to: create groups in Buddypress with PHP</title>
		<link>http://danpolant.com/how-to-create-groups-in-buddypress-with-php/</link>
		<comments>http://danpolant.com/how-to-create-groups-in-buddypress-with-php/#comments</comments>
		<pubDate>Wed, 30 Sep 2009 03:26:12 +0000</pubDate>
		<dc:creator>dan</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[how to]]></category>
		<category><![CDATA[buddypress]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[create]]></category>
		<category><![CDATA[extends]]></category>
		<category><![CDATA[group]]></category>

		<guid isPermaLink="false">http://danpolant.com/?p=40</guid>
		<description><![CDATA[Ever want to make a buddypress function that creates a group? Here&#8217;s how:
You can do this without writing any SQL. Buddypress uses a class called BP_Groups_Group to handle its group creation. BP_Groups_Group is defined in /groups/bp-groups-classes if you want to see it. I highly recommend taking a look at this, as you will discover other [...]]]></description>
			<content:encoded><![CDATA[<p>Ever want to make a buddypress function that creates a group? Here&#8217;s how:</p>
<p>You can do this without writing any SQL. Buddypress uses a class called <code>BP_Groups_Group</code> to handle its group creation. BP_Groups_Group is defined in /groups/bp-groups-classes if you want to see it. I highly recommend taking a look at this, as you will discover other class methods that do other group-related things.</p>
<p>Say you want a plugin that upon activation creates a certain group. First add an action to the activation hook:</p>
<p><code>register_activation_hook(__file__, 'create_a_group');</code></p>
<p>Next, we need to call a function that instantiates the class BP_Groups_Group. Lets do this:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">function</span> create_a_group<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="re0">$new_group</span> = BP_Groups_Group;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$new_group</span>-&gt;<span class="me1">creator_id</span> = <span class="nu0">1</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$new_group</span>-&gt;<span class="me1">name</span> = <span class="st0">&#8216;test&#8217;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$new_group</span>-&gt;<span class="me1">slug</span> = <span class="st0">&#8216;test&#8217;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$new_group</span>-&gt;<span class="me1">description</span> = <span class="st0">&#8216;nothing&#8217;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$new_group</span>-&gt;<span class="me1">news</span> = <span class="st0">&#8216;whatever&#8217;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$new_group</span>-&gt;<span class="me1">status</span> = <span class="st0">&#8216;public&#8217;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$new_group</span>-&gt;<span class="me1">is_invitation_only</span> = <span class="nu0">1</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$new_group</span>-&gt;<span class="me1">enable_wire</span> = <span class="nu0">1</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$new_group</span>-&gt;<span class="me1">enable_forum</span> = <span class="nu0">1</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$new_group</span>-&gt;<span class="me1">enable_photos</span> = <span class="nu0">1</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$new_group</span>-&gt;<span class="me1">photos_admin_only</span> = <span class="nu0">1</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$new_group</span>-&gt;<span class="me1">date_created</span> = current_time<span class="br0">&#40;</span><span class="st0">&#8216;mysql&#8217;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$new_group</span>-&gt;<span class="me1">total_member_count</span> = <span class="nu0">1</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$new_group</span>-&gt;<span class="me1">avatar_thumb</span> = <span class="st0">&#8216;some kind of path&#8217;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$new_group</span>-&gt;<span class="me1">avatar_full</span> = <span class="st0">&#8216;some kind of path&#8217;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="re0">$new_group</span> -&gt; <span class="me1">save</span><span class="br0">&#40;</span><span class="br0">&#41;</span>; <span class="co1">//this does the database insert</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>The whole point of doing it this way is so you that can use the <code>save()</code> method. Check this function out on line 90 of bp-groups-classes.php. It does all the SQL for you, and to reproduce this would be very tedious.</p>
<p><code>create_a_group()</code> will dump a row into wp_bp_groups. But if you go to your site and look at the groups directory, it won&#8217;t show up. Why? Because Buddypress checks for certain kinds of metadata before it displays a group in the main directory. So add this:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">function</span> create_a_group<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="re0">$new_group</span> = <span class="kw2">new</span> BP_Groups_Group;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$new_group</span>-&gt;<span class="me1">creator_id</span> = <span class="nu0">1</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$new_group</span>-&gt;<span class="me1">name</span> = <span class="st0">&#8216;test&#8217;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$new_group</span>-&gt;<span class="me1">slug</span> = <span class="st0">&#8216;test&#8217;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$new_group</span>-&gt;<span class="me1">description</span> = <span class="st0">&#8216;nothing&#8217;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$new_group</span>-&gt;<span class="me1">news</span> = <span class="st0">&#8216;whatever&#8217;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$new_group</span>-&gt;<span class="me1">status</span> = <span class="st0">&#8216;public&#8217;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$new_group</span>-&gt;<span class="me1">is_invitation_only</span> = <span class="nu0">1</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$new_group</span>-&gt;<span class="me1">enable_wire</span> = <span class="nu0">1</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$new_group</span>-&gt;<span class="me1">enable_forum</span> = <span class="nu0">1</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$new_group</span>-&gt;<span class="me1">enable_photos</span> = <span class="nu0">1</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$new_group</span>-&gt;<span class="me1">photos_admin_only</span> = <span class="nu0">1</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$new_group</span>-&gt;<span class="me1">date_created</span> = current_time<span class="br0">&#40;</span><span class="st0">&#8216;mysql&#8217;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$new_group</span>-&gt;<span class="me1">total_member_count</span> = <span class="nu0">1</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$new_group</span>-&gt;<span class="me1">avatar_thumb</span> = <span class="st0">&#8216;some kind of path&#8217;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$new_group</span>-&gt;<span class="me1">avatar_full</span> = <span class="st0">&#8216;some kind of path&#8217;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="re0">$new_group</span> -&gt; <span class="me1">save</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; groups_update_groupmeta<span class="br0">&#40;</span> <span class="re0">$id</span>, <span class="st0">&#8216;total_member_count&#8217;</span>, <span class="nu0">1</span> <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; groups_update_groupmeta<span class="br0">&#40;</span> <span class="re0">$id</span>, <span class="st0">&#8216;last_activity&#8217;</span>, <a href="http://www.php.net/time"><span class="kw3">time</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; groups_update_groupmeta<span class="br0">&#40;</span> <span class="re0">$id</span>, <span class="st0">&#8216;theme&#8217;</span>, <span class="st0">&#8216;buddypress&#8217;</span> <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; groups_update_groupmeta<span class="br0">&#40;</span> <span class="re0">$id</span>, <span class="st0">&#8216;stylesheet&#8217;</span>, <span class="st0">&#8216;buddypress&#8217;</span> <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>This is how Buddypress updates group metadata. Your new group should show up.</p>
<p>It is worth mentioning that the other database operations in Buddypress work exactly the same way: first you instantiate a database class, populate the object with data, then you call save(). But look at the code, there are tons of other methods for these kinds of classes that will help you do things without writing redundant SQL.</p>
<p>Basically, what you are doing here mirrors what Buddypress does in step one (case 1) of <code>groups_create_group()</code> on line 1451 of bp-groups.php. The reason you can&#8217;t just use this function is that you need your own data, not the canned query that comes with the plain old <code>populate()</code>.</p>
]]></content:encoded>
			<wfw:commentRss>http://danpolant.com/how-to-create-groups-in-buddypress-with-php/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
	</channel>
</rss>

