Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Different Blog Title per User Group using Genesis Child Theme WordPress

I am trying to use newly created code to filter the site's title and to show a different title per Role Scoper User Group. I already have the Role Scoper Function to grab the RS Scoper thanks to help here. But now I need to show a different blog site title per Role Scoper user group in my Genesis theme. This is the code I have so far in my child theme's function file:

/*Role Scoper User Group Grabber using Widget Logic*/

function is_usergroup_can($groupid) {

global $wpdb, $user_ID, $user_identity; // make several variables accessible using global for custom function
// $wpdb The $wpdb object can be used to read data from any table in the WordPress database,
$custom_query = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->user2group_rs WHERE user_id = '".$user_ID."'") );

if($custom_query->group_id == $groupid)
return 1;

}



/* Different Titles based on user group */

add_filter ('genesis_site_title', 'title_for_group_nine');
function title_for_group_nine (){
// print site_title of choice
if(user_group_can('9')) {
$uglinka = '<a href="http://domain.com">Site Title</a>';
return $uglinka;
}
elseif(user_group_can('10')) {
$uglinkb = '<a href="http://domain.com">Site Title</a>';
return $uglinkb;
}
else
echo "Bummer!";
}


When I ran the filter I funnily enough saw nothing except for the site title. I was logged on as administrator and I normally will see all.

Answers (1)

2011-08-18

Kannan C answers:

Hi Jasper,
Did you see, in the query the $user_ID cannot be set without the user got logged in. So you cannot get any output for the function is_usergroup_can($groupid) without log in.
The usergroup of Role scoper is working on the basis of user id. Without user id you cannot differentiate user groups.


rhand comments:

I see Kannan C. Well, what other options are there to filter the title on every page based on user group? The initial function works great with widget logic. I thought I could use a filter to replace the site title by one depending on what user is logged on connected to what user group. Each user group will have to have a different site title. Now a single is loaded from settings > site title. So I thought let's filter the database data grabbed before it is displayed on the browser screen. Really new to hooks and filters and still a juniot programmer for backend stuff so please help me out here.


Kannan C comments:

Again, the function can work 100% for logged in users. You can show different titles for different user groups only where a user is logged in. Then only the function can get the user id of the logged in user.
i found you missed the global variable in the above function.

function title_for_group_nine (){
$wpdb;
//your other stuff
}

this will work for logged in users.


Kannan C comments:

oh i apologize, you have the sql query on other function. So $wpdb will make no difference there.


Kannan C comments:

can you show me the contents of function genesis_site_title() of your theme?


rhand comments:

Checking now..


rhand comments:

In genesis/structure/header there is
add_action( 'genesis_site_title', 'genesis_seo_site_title' );
/**
* Echo the site title into the #header.
*
* Depending on the SEO option set by the user, this will
* either be wrapped in <h1> or <p> tags.
*
* @since unknown
*/
function genesis_seo_site_title() {

// Set what goes inside the wrapping tags
$inside = sprintf( '<a href="%s" title="%s">%s</a>', trailingslashit( home_url() ), esc_attr( get_bloginfo( 'name' ) ), get_bloginfo( 'name' ) );

// Determine which wrapping tags to use
$wrap = is_home() && 'title' == genesis_get_seo_option( 'home_h1_on' ) ? 'h1' : 'p';

// A little fallback, in case an SEO plugin is active
$wrap = is_home() && ! genesis_get_seo_option( 'home_h1_on' ) ? 'h1' : $wrap;

// Build the Title
$title = sprintf( '<%s id="title">%s</%s>', $wrap, $inside, $wrap );

// Echo (filtered)
echo apply_filters( 'genesis_seo_title', $title, $inside, $wrap );

}

add_action( 'genesis_site_description', 'genesis_seo_site_description' );
/**
* Echo the site description into the #header.
*
* Depending on the SEO option set by the user, this will
* either be wrapped in <h1> or <p> tags.
*
* @since unknown
*/
function genesis_seo_site_description() {

// Set what goes inside the wrapping tags
$inside = esc_html( get_bloginfo( 'description' ) );

// Determine which wrapping tags to use
$wrap = is_home() && genesis_get_seo_option( 'home_h1_on' ) == 'description' ? 'h1' : 'p';

// Build the Description
$description = $inside ? sprintf( '<%s id="description">%s</%s>', $wrap, $inside, $wrap ) : '';

// Return (filtered)
echo apply_filters( 'genesis_seo_description', $description, $inside, $wrap );

}

add_filter( 'wp_title', 'genesis_doctitle_wrap', 20 );
/**
* This function wraps the doctitle in <title></title> tags.
*
* @since unknown
*
* @param string $title
* @return string Plain text or HTML markup
*/
function genesis_doctitle_wrap( $title ) {

return is_feed() || is_admin() ? $title : sprintf( "<title>%s</title>\n", $title );

}

add_action( 'genesis_title', 'wp_title' );
add_filter( 'wp_title', 'genesis_default_title', 10, 3 );


In genesis/header.php there is:
<?php
/**
* WARNING: This file is part of the core Genesis framework. DO NOT edit
* this file under any circumstances. Please do all modifications
* in the form of a child theme.
*
* Handles the header structure.
*
* @package Genesis
*/
do_action( 'genesis_doctype' );
do_action( 'genesis_title' );
do_action( 'genesis_meta' );

wp_head(); /** we need this for plugins **/
?>
</head>
<body <?php body_class(); ?>>
<?php
do_action( 'genesis_before' );
?>
<div id="wrap">
<?php
do_action( 'genesis_before_header' );
do_action( 'genesis_header' );
do_action( 'genesis_after_header' );

echo '<div id="inner">';
genesis_structural_wrap( 'inner' );


Kannan C comments:

can you clear me one thing? are you looking for Title to show for logged in users or without login?


rhand comments:

The website is private so only registered users see content
For all logged in users under group a I need title a, under group b I need title be, and so on. For that I thought a filter using one of [[LINK href="http://dev.studiopress.com/hook-reference"]]Genesis' hooks[[/LINK]] that deals with the site title inside each page. Now only one title is loaded from the backend for all logged in users...


rhand comments:

Perhaps one of these filters should be used:
<blockquote>Header Section Filters

genesis_seo_title
Default value: $title, $inside, $wrap
Applied to the output of the genesis_seo_site_title function which depending on the SEO option set by the user will either wrap the title in <h1> or <p> tags.

genesis_seo_description
Default value: $description, $inside, $wrap
Applied to the output of the genesis_seo_site_description function which depending on the SEO option set by the user will either wrap the description in <h1> or <p> tags.

genesis_pre_load_favicon
Default value: false

genesis_header_scripts
Default value: genesis_get_option('header_scripts')
Applied to the output of the header scripts.
</blockquote>
Still reading and trying stuff..


Kannan C comments:

i don't how genesis framework works. but upon seeing the code, let's try this way
find

add_action( 'genesis_title', 'wp_title' );

to

add_action( 'genesis_title', 'title_for_group_nine ' );

no need for add_filter. this is a quick fix but you may loose genesis title settings


rhand comments:

add_action ('genesis_title', 'title_for_group_nine');

function title_for_group_nine (){
global $wpdb;
// print site_title of choice
if(user_group_can('9')) {
$uglinka = '<a href="http://domain.com">Title</a>';
return $uglinka;
}
elseif(user_group_can('10')) {
$uglinkb = 'a href="http://domain.com">Title</a>';
return $uglinkb;
}
else
echo "Bummer!";
}

caused:
<blockquote>Fatal error: Call to undefined function user_group_can() in /home/user/domain.com/studentzone/wp-content/themes/enterprise/functions.php on line 99</blockquote>
which has if(user_group_can('9')) {
$uglinka = '<a href="http://domain.com">Title</a>';

I guess we need to make that function global first?


Kannan C comments:

you have typo in the function name

if(is_user_group_can('9')) { not if(user_group_can) {


rhand comments:

Fatal error: Call to undefined function is_user_group_can() in /home/user/domain.com/www/studentzone/wp-content/themes/enterprise/functions.php on line 99

/* Different Titles based on user group */

add_action ('genesis_title', 'title_for_group_nine');

function title_for_group_nine (){
global $wpdb;
// print site_title of choice
if(is_user_group_can('9')) {
$uglinka = '<a href="http://domain.com">Title</a>';
return $uglinka;
}
elseif(is_user_group_can('10')) {
$uglinkb = 'a href="http://domain.com">Title</a>';
return $uglinkb;
}
else
echo "Bummer!";
}


Kannan C comments:

are you sure you have the function is_user_group_can() in the functions.php file?
it is hard for me to figure out the error without checking the theme files..


rhand comments:

Whole Child theme functions.php @ http://pastebin.mozilla.org/1303199 including the is_user_group_can function.


Kannan C comments:

Man! again you made a typo, replace this with yours

function title_for_group_nine (){
global $wpdb;
// print site_title of choice
if(is_usergroup_can('9')) {
$uglinka = '<a href="http://domain.com">Title</a>';
echo $uglinka;
}
elseif(is_usergroup_can('10')) {
$uglinkb = 'a href="http://domain.com">Title</a>';
return $uglinkb;
}
else
echo "Bummer!";
}


rhand comments:

Sorry about that. I guess I will have to give this a break tonight. Will try once more..


rhand comments:

Thanks for all the work so far. Code now loads without errors. But when I log in using one of the two users belonging to group 9 I still see the same title and not the new one. Code used at the moment:

/* Different Titles based on user group */

function title_for_group_nine (){

global $wpdb;

// print site_title of choice

if(is_usergroup_can('9')) {

$uglinka = '<a href="http://domain.com">Title 9</a>';

echo $uglinka;

}

elseif(is_usergroup_can('10')) {

$uglinkb = 'a href="http://domain.com">Title 10</a>';

return $uglinkb;

}

else

echo "Bummer!";

}


Kannan C comments:

Check that user also belongs to other group. echo the result of all your if conditions.


rhand comments:


/* Different Titles based on user group */

add_action( 'genesis_seo_title', 'new_title_per_group');

function new_title_per_group(){

global $wpdb;

// print site_title of choice

if(is_usergroup_can('9')) {

$uglinka = '<h1 id="title"><a href="http://domain.com">Title 9</a></h1>';

echo $uglinka;

}

elseif(is_usergroup_can('10')) {

$uglinkb = '<h1 id="title"><a href="http://domain.com">Title 10</a></h1>';

return $uglinkb;

}

else

echo '<h1 id="title"><a href="http://domain.com">Student Zone</a></h1>';

}


Works. Just need to rework all so the same style and HTML is applied as before. All good now. Thanks for hanging in there with me!