<?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; filters</title>
	<atom:link href="http://danpolant.com/tag/filters/feed/" rel="self" type="application/rss+xml" />
	<link>http://danpolant.com</link>
	<description></description>
	<lastBuildDate>Tue, 01 Nov 2011 03:55:39 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<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>admin</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>
	</channel>
</rss>

