Control modules based on article author

(5 votes, average 3.40 out of 5)

In this recipe we detect if we are viewing a Joomla “article” page, and get hold of the author id. Based on this, we can make decisions on what modules show on the page – for example we could show particular banner ads for certain authors, on any page they have authored. UPDATED FOR JomGenius!

JomGenius version

The JomGenius version is suitable for MetaMod v2.4 and up, and MetaTemplate 1.5 and up

The simplest possible rule checks for a given list of user ids, and assigns module 101 if found:

// this will only be valid on article pages, so we don't need
// to specifically check the page type.
// We'll use module 101 for users 10 and 15.
 
if ( $content_genius->check("article_created_by = 10, 15") ) return 101;

In this one we use "info" to get the information from JomGenius, and compare against different values:

// this will only be valid on article pages, so we don't need
// to specifically check the page type
 
$created_by = $content_genius->info("article_created_by");
if ( $created_by == 10 ) return 101; /* module 101 for user #10 */
if ( $created_by == 15 ) return 102; /* module 102 for used #15 */

Raw PHP/Database version

if ( $option == "com_content" && $view == "article" ) {
 $article_id = (int)$id;
 $query = "SELECT created_by "
  . " FROM #__content WHERE id = '$article_id' AND state = 1";
 $db->setQuery( $query, 0, 1 );
 $row = $db->loadObject();
 
 if ( $row != null ) {
  $created_by = $row->created_by;
 
  /* now put your rules in here... */
  if ( $created_by == 10 ) return 101; /* module 101 for user #10 */
  if ( $created_by == 15 ) return 102; /* module 102 for user #15 */
 }
}