Cornflower Design Development

SharePoint 2013: Focus on Content

Posted within Development on by Cornflower Design

SharePoint 2013 has introduced a small Focus on Content icon to the ribbon bar which, for the MasterPages that support it, will show and hide portions of the page when clicked. This can be useful for hiding non-essential page elements.

When the icon is clicked, JavaScript will do two things:

  1. It creates a cookie, so the selection is remembered the next time the page is visited.
  2. It adds a new ms-fullscreenmode class to the page’s BODY tag.

So, via CSS, we can decide which elements of the page should be hidden via the Focus on Content functionality.

.ms-fullscreenmode #nonessentialcontent { display: none; }

How Many Users In An Active Directory Group?

Posted within Development on by Cornflower Design

This is a useful PowerShell snippet for determining how many users there are in an Active Directory group.

(Get-ADGroup "CN=MyGroup,DC=domain,DC=co,DC=uk" -Properties *).Member.Count

Oops! Twitter Feeds Now Working

Posted within Announcements Development on by Cornflower Design

I missed the June announcement from Twitter informing everyone that their API v1 was about to be retired – meaning that feeds on my homepage had been failing to update.

Along with the introduction of rate limits and ditching support for RSS as a response format, API v1.1 has implemented OAuth 2.0 for its authentication. Its not the most straight-forward thing to get your head around, but I found a useful post on CodeForest that helped me to update my own WordPress widgets.

CSS and Nested Border-Radii

Posted within Development on by Cornflower Design

Now, this is an issue I’ve stumbled across while putting together CSS for websites.

You have two elements, one nested within the other, and both have rounded borders. Unless you get the radius of the nested element just right, you’ll end up with a nasty looking ‘hump’ where there should be a nice smooth curve (See Chris Coyier’s post for a visual example).

Fortunately, Joshua Hibbert has blogged about the problem, documented the math­em­at­ical for­mu­las and even coded a tool for the job. Great!

Hiding SharePoint WebParts via Code

Posted within Administration Development on by Cornflower Design

Another note to myself…

When wanting to programmatically hide a WebPart, don’t try to set it’s Visible property to False. This will cause you lots of trouble and create errrors. The correct method is to set its Hidden property to True.

if (items.Count == 0)
{
   this.Hidden = true;
   return;
}

 

PowerShell Snippets for Microsoft Exchange

Posted within Development on by Cornflower Design

These are two very handy PowerShell snippets that I use again and again within our work environment.

Retreive List of All Group Members

Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
csvde -f C:\report.csv -s mydomain.co.uk -r "memberof=CN=Dept:MyTartgetGroup,OU=Domain Groups,DC=mydomain,DC=ac,DC=uk" -l "sAMAccountName,name, mail"

Retreive List of All Users with Permission to Email a Group

Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
Get-DistributionGroup "Dept:MyTargetGroup" | Select name,@{Name="AcceptMessagesOnlyFrom";Expression={[string]::join(";",($_.AcceptMessagesOnlyFrom | foreach {$_.Name}))}} | Export-Csv C:\report.csv

The Post Slug

Posted within Development on by Cornflower Design

While within The Loop, WordPress provides many functions to quickly access the properties of the Post – but it doesn’t include a built-in function to get the slug for the current Post (although, admittedly, you could simply use $post->post_name).  This functionality can be easily included by adding the following two mini-functions within the functions.php file.

function get_the_slug() {
	global $post;
	return $post->post_name;
}

function the_slug() {
	echo get_the_slug();
}

Update (26 August 2012)

To be absolutely correct, to avoid possible conflicts with other core WordPress or third-party functions, the two sample functions should really be prefixed with a unique identifier, e.g. cornflowerdesign_get_the_post(). I would also recommend wrapping the functions with a function_exists() check, like so.

if ( !function_exists( 'cornflowerdesign_get_the_slug' ) ) {
	function cornflowerdesign_get_the_slug() {
		global $post;
		return $post->post_name;
	}
}

Numeric Position of Post

Posted within Development on by Cornflower Design

Just a quickie… within a WordPress loop you can easily determine the current post position via $wp_query->current_post.

<?php
while ( have_posts() ): the_post();
the_title();
echo $wp_query->current_post;
endwhile;
?>

Selecting Randon Users

Posted within Development on by Cornflower Design

Unlike the get_posts() and get_pages() functions within WordPress, get_users() doesn’t provide a way to return a random selection of users (the results can only be sorted by ‘nicename’, ‘email’, ‘url’, ‘registered’, ‘display_name’, or ‘post_count’). For several projects, usually for sidebar widgets, theres been a need to return a randomly sorted sub-set of users, so I wrote the following custom function:

if ( !function_exists( 'get_random_users' ) ) {
	function get_random_users( $args = array() ) {
		$random_users = array();
		$number = 10;

		$defaults = array( 'number' => $number, 'exclude_administrators' => FALSE );
		$args = wp_parse_args( $args, $defaults );
		$number = $args['number'];

		unset( $args['number'] );

		if ( $args['exclude_administrators'] == TRUE ) {
			$administrators = get_users( array( 'role' => 'administrator' ) );

			if ( $administrators && !is_wp_error( $administrators ) ) {
				$exclude = array();

				foreach( $administrators as $user ) {
					$exclude[] = $user->ID;
				}

				$args['exclude'] = $exclude;
			}

		}

		$users = get_users( $args );

		if ( $users && !is_wp_error( $users ) ) {
			$total_users = count( $users );
			$number = ( $total_users > $number ) ? $number: $total_users;

			if ( $total_users == 1 ) {
				$random_users = $users;
			} elseif ( $total_users > 1 ) {
				shuffle( $users );
				$random_users = array_slice( $users, 0, $number );
			}

		}

		return $random_users;
	}
}

Essentially it works and accepts the same parameters as get_users(), but I have included an option that allows you to exclude Administrators from the returned array. I’m sure the code can be improved – and I probably will do when I get the chance, but for now it does the job.

Alternatively, the following two lines of code, added into the wp-includes/user.php file at line 446, would remove the need for the above function and allow ‘rand’ to be a valid value for the ‘orderby’ parameter.

} elseif ( 'rand' == $qv['orderby'] ) {
	$orderby = 'RAND()';

But I would never condone editing the core WordPress files.

Querying WordPress Taxonomies

Posted within Development on by Cornflower Design

To produce a mega-menu for a recent WordPress project, I had a requirement to show all the terms within one taxonomy that were associated with Posts that a specific term from a second taxonomy was also associated with. Tricky. It took a little while to get my head around the problem, but I came up with a solution that did just what was needed.

if ( !function_exists( 'get_associated_terms' ) ) {
	function get_associated_terms( $taxonomy_slug, $term_id = 0, $post_type = 'post' ) {
		global $wpdb;

		$sql = "SELECT DISTINCT $wpdb->terms.term_id, $wpdb->terms.name, $wpdb->terms.slug, $wpdb->terms.term_group, $wpdb->term_taxonomy.term_taxonomy_id, $wpdb->term_taxonomy.taxonomy, $wpdb->term_taxonomy.description, $wpdb->term_taxonomy.parent, $wpdb->term_taxonomy.count
			FROM $wpdb->terms
			INNER JOIN $wpdb->term_taxonomy ON $wpdb->terms.term_id = $wpdb->term_taxonomy.term_id
			INNER JOIN $wpdb->term_relationships ON $wpdb->term_taxonomy.term_taxonomy_id = $wpdb->term_relationships.term_taxonomy_id
			INNER JOIN $wpdb->posts ON $wpdb->term_relationships.object_id = $wpdb->posts.ID
			INNER JOIN $wpdb->term_relationships tr2 ON $wpdb->posts.ID = tr2.object_id
			INNER JOIN $wpdb->term_taxonomy tt2 ON tr2.term_taxonomy_id = tt2.term_taxonomy_id
			WHERE $wpdb->posts.post_status = 'publish' AND $wpdb->posts.post_type = %s AND $wpdb->term_taxonomy.taxonomy = %s AND tt2.term_id = %d
			ORDER BY $wpdb->terms.name";

		$safe_sql = $wpdb->prepare( $sql, $post_type, $taxonomy_slug, $term_id );
		$results = $wpdb->get_results( $safe_sql, OBJECT );

		return $results;
	}
}

Once added to the WordPress theme functions.php file or a plugin, its used in the same way you’d use one of the built-in functions, like get_terms(), except my function accepts fewer parameters. It returns an Object that can be looped through in the normal way.

$terms = get_associated_terms( 'tax1', 256 );

foreach($terms as $term) {
	echo '<a href="/tax1/' . $term->slug . '" class="' . $term->taxonomy . '">' . $term->name . '</a>';
}

The first parameter in the function is the name of the taxonomy you wish to return. The second parameter is the ID of a term within your starting taxonomy. Running my example would return an Object containing all terms within the ‘tax1′ taxonomy which are assigned to Posts that also have term 256 assigned to them.