Gravity Forms to Zoho CRM Plugin update

The Gravity forms WordPress plugin that I’ve worked on for a while now has been updated today.

The new version makes it a lot easier to setup the transfer of data between Gravity Forms and Zoho CRM.

Main features of the update:

  • Ability to test the API connection to Zoho CRM
  • Support for all the default fields in Zoho CRM
  • Ability to add custom fields in Zoho and then send data to those

For more information on this plugin visit the Help For WordPress site here.

Even better Custom WordPress Loop

It’s a new year, time for a new improved custom WordPress loop!

Here’s a little code snippet that you can use to place a loop almost anywhere inside a theme, plugin or a framework like Thesis.

First function does the hard work and return an two dimensional array with each post, custom post etc.. easily accessible.

And designers will like this, the code is separate from the styling side, the second function calls the first and it is there that you can handle each post and add styles and other HTML.

function dmaCustomLoop( $args ){
        global $post;
        $output = ""; // empty the string
        // The Query
        $the_query = new WP_Query( $args );
 		$the_result = array();
        // The Loop
        while ( $the_query->have_posts() ) : $the_query->the_post();

            $permalink = get_permalink();
            $title = get_the_title();
            $theexcerpt = get_the_excerpt();
			$thecontent = get_the_content();

           	$brisbane_multi = get_post_meta($post->ID, 'brisbane_multi', true);

			array_push($the_result, array("permalink" => $permalink, "title" => $title, "theexcerpt" =>$theexcerpt , "thecontent" => $thecontent) );

        endwhile;
        wp_reset_postdata();

        return $the_result;
}

So place the above in your theme functions.php or inside custom_functions if you’re using Thesis. Then were you would like to call the function use this.

$args = array(
		    //'posts_per_page' => 2,
		    //'cat' => 11,
		    'post_type' => 'positiontype',
		    'orderby' => 'asc'
		);
		$result =  dmaCustomLoop( $args );

		// loop through the results
		foreach($result as $value){
			print "my perma is " . $value['permalink'];
			print "my title is " . $value['title'];
			print "my excerpt is " . $value['theexcerpt'];
			print "my content is " . $value['thecontent'];
		}
		unset($value);

For more information on the arguments you can send to WP_Query check out this page on the WordPress Codex.

How to change the order of posts in the WordPress loop

This is not rocket science but it took me a while to find the answer, so here it is.

Situation: On some WordPress archive pages you’d like to order the posts by title rather than the default date order.

The following function will do just that, it will merge the WordPress loop query with two new parameters ‘orderby’ and ‘order’ – leaving the rest of the query as is.

Here I’ve hooked it to a thesis hook, but you can hook it anywhere as long as its before your loop starts.

The other part of this is the conditional statement. In this example I’ve got is_tax(‘buscategory’), relevant for a particular job, but you can use any WordPress conditional tags.

function change_post_order(){
		if (is_tax( 'buscategory' ) || is_search()){
			global $wp_query;
			$args = array_merge( $wp_query->query, array( 'orderby' => 'title','order' => 'ASC' ) );
			query_posts( $args );
	}
}
add_action('thesis_hook_before_content','change_post_order');

WordPress loop that searches custom fields

From time to time it’s required to create a loop that will respond with posts, pages or custom posts that have a certain custom field. This code is a quick example.

It’s looking for post types that are ‘pages’ and a custom field that is ‘scroller’ with a value being ‘true’. For all the parameter you can use including other operators like ‘like’ etc.. see this page on the WordPress codex.

function dma_scroller_setup(){
		$args = array(
		'post_type' => 'page',
		'meta_query' => array(
			array(
				'key' => 'scroller',
				'value' => 'true',
				'compare' => '='
			)
		)
	 );
	$the_query = new WP_Query( $args );
	// The Loop
	while ( $the_query->have_posts() ) : $the_query->the_post();
		echo '<li>';
		the_title();
		echo '</li>';
	endwhile;

	// Reset Post Data
	wp_reset_postdata();

WordPress multisite on Rackspace Cloud Sites

Chances are if you’ve found this post, you’ve been googling around to see if it’s possible to run a WordPress multisite installation on the Rackspace Cloud Sites. Obviously if it’s a sub-folder install you can as there is nothing tricky to that. (this is where you’re sites run in  a folder like domain.com/site1/)

Where it can be harder is when you want to have users with fully qualified domain names each pointing to their own site, within your multisite install.

In short: Yes it can be done.

Primarily I’m posting this to dispel a lot of what I’ve read on many forums saying that it can’t be done. The trick to doing it us WPMU’s domain mapping plugin.

A bit of background. Usually you need an environment where the main hostname is glued to a dedicated IP address. The test suggested in most places is visiting the IP address (e.g. http://123.123.123.123) by itself and you should get to the main site. What this is demonstrating is that Apache is configured correctly to support multisite.

You won’t get this kind of setup to work on Rackspace cloud sites due to the fancy way they have their environment setup, even if you order a dedicated IP address.

The trick is to firstly configure WordPress for a sub-folder install. Test that and confirm that it’s working.

Then use the WPMU domain mapping plugin (located here), walk through the setup of that plugin.

Final piece of the puzzle is to create each site, then in the Rackspace Cloud administration you have to set up the domain name that you are mapping as an alias of the main domain name. Sure the catch here is that you have to have the DNS pointing to their DNS servers but if you can live with that, it works.

It looks like this in the control panel when you’re done. (click to view larger)

Good luck! Post a question here if you have one, I’ll help out if I can.

WordPress and htaccess

Here are some sample lines to add to your .htaccess file to help WordPress run a little more smooth. Each line is commented so is pretty self explanatory.

#increase the memory limit available to PHP
php_value memory_limit 128M 

# give PHP scripts longer to execute
php_value max_execution_time 400 

#set the uplaod file size to 20M
php_value post_max_size 20M
php_value upload_max_filesize 20M

#while writing code display errors, turn this one off when your live
php_value display_errors on

This should go below the code that WordPress automatically generates that looks like this.

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

Custom taxonomy drop down

Here is a quick example that will help you create a drop down menu, populated with all of the terms from a custom taxonomy.

This code is mostly from this thread on WordPress.com forums, I’ve just changed a few things for my situation.

Here’s the function that does most of the hard work.

function get_terms_dropdown($taxonomies, $args){
	$myterms = get_terms($taxonomies, $args);
	$output ="<select name='buscategory'>";
	$output .="<option value='#'>Please select a business category</option>";
	foreach($myterms as $term){
		$root_url = get_bloginfo('url');
		$term_taxonomy=$term->taxonomy;
		$term_slug=$term->slug;
		$term_name =$term->name;
		$link = $term_slug;
		$output .="<option value='".$link."'>".$term_name."</option>";
	}
	$output .="</select>";
return $output;
}

Then place this code where you would like to call the drop down menu, this could be using a hook or in your template files.

<form action="<?php bloginfo('url'); ?>" method="get">
<p>Or search for a service from the drop down menu:</p>
<?php
$taxonomies = array('buscategory');
$args = array('orderby'=>'name','hide_empty'=>true);
$select = get_terms_dropdown($taxonomies, $args);

$select = preg_replace("#<select([^>]*)>#", "<select$1 onchange='return this.form.submit()'>", $select);
echo $select;
?>
	<noscript><div><input type="submit" value="Go" /></div></noscript>
	</form>
	</div>

You’ll need to update the name of the taxonomy for this to work, you’ll see in the above example I’ve used ‘buscategory’ it needs to be update in both code blocks.

Quick WordPress custom loop

Update: There is a new improved version of a custom WordPress loop here

This is a short example of how to quickly produce a custom loop in WordPress.

I usually use the thesis theme frame work so this code would usually live in the custom_functions.php file. For other themes this can go in the functions.inc.php file.

Firstly here is the function to format the output of the posts.

This function also supports the thesis post images. So if you’re using this theme and use post images they will be displayed, if you’re not you can delete lines 18-24 (the if statement)

function cnCustomLoops( $args ){
		global $post;
		$output = ""; // empty the string
		// The Query
		$the_query = new WP_Query( $args );

		// The Loop
		while ( $the_query->have_posts() ) : $the_query->the_post();

			$permalink = get_permalink();
			$title = get_the_title();
			$theexcerpt = get_the_excerpt();

			$output .= "<div class='format_text entry-content'>";
			$output .= "<h3>";
			$output .= "<a href='" . $permalink . "'>" . $title . "</a>";
			$output .= "</h3>";
			if (get_post_meta($post->ID, 'thesis_post_image', true) != ""){

				$image = get_post_meta($post->ID, 'thesis_post_image', true);
				$url = "http://url-to-your-blog/wp-content/themes/thesis_181/lib/scripts/thumb.php?src=" . $image .  "&w=150&zc=1&q=100";

				$output .= "<img src='" . $url . "' alt='" . $title . "'";
			}
			$output .= "<p>";
			$output .= $theexcerpt;
			$output .= "</p>";
			$output .= "<a class=\"read-more-button\" href='" . $permalink . "'>Read More</a>";
			$output .= "</div>";
		endwhile;
		wp_reset_postdata();

		return $output;
}

Then you need to code to fire the loop

function sampleLoop(){
$args = array(
	'posts_per_page' => 2,
	'cat' => 11,
	'post_type' => 'post',
	'orderby' => 'rand'
);
echo cnCustomLoops( $args );

}

You can use this code to support custom post types, see the line ‘post_type’, change this to the name of your custom post type.

For full details on the parameters to place in the $args, check out this page on the WordPress Codex

You can use this code in lots of places, I commonly use it in a footer area so that you can display posts and post images of the more recent posts from a particular category. You can also use thesis hooks to display a loop in another part of the page.

Enjoy!

Add footer widgets areas in Thesis

This tip is to add widget areas to a theme when using the Thesis WordPress theme. What this will do is add three widget areas that you can then use the standard wordpress widgets to populate and control the content, in the WordPress Admin interface.

Firstly the code registers the widgets with WordPress.

Then use a thesis hook, in this case thesis_hook_after_content_box to display the widgets.

As usual with thesis, this all belongs in the custom_functions.php file.

//ADD EXTRA WIDGET AREAS TO BOTTOM OF ALL PAGES
add_action('thesis_hook_after_content_box', 'footerWidgets');

function footerWidgets(){
	?>
	<div class="footer-widget-container">
		<div class="foot-widget clear-group">
			<ul>
			<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Footer1') ){ ?>
			<?php } ?>
			</ul>
		</div>
		<div class="foot-widget">
			<ul>
			<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Footer2') ){ ?>
			<?php } ?>
			</ul>
		</div>
		<div class="foot-widget end">
			<ul>
			<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Footer3') ){ ?>
			<?php } ?>
			</ul>
		</div>
	</div>
	<?php
}
// Register three new footer widgets
register_sidebar(array('name'=>'Footer1', 'before_title'=>'<h3>', 'after_title'=>'</h3>'));
register_sidebar(array('name'=>'Footer2', 'before_title'=>'<h3>', 'after_title'=>'</h3>'));
register_sidebar(array('name'=>'Footer3', 'before_title'=>'<h3>', 'after_title'=>'</h3>'));

WordPress search: only show post and not pages

This tip is a quick one but something asked for regularly. By default the WordPress will show both posts and pages in the results.

If you’d rather just show posts and you use the Thesis framework, just place the code below in your custom_functions.php file and you’re done!

<?php
function SearchFilter($query) {
if ($query->is_search) {
$query->set('post_type', 'post');
}
return $query;
}
add_filter('pre_get_posts','SearchFilter');
?>

Here’s an updated version of this that fixes the search when you’re in the administration screen, basically leaving that to it’s default behaviour when your logged in.

The other thing I’ve added to this is the ability to place a hidden form field in a search form then use this if statement to test if we want to search just with in a custom post type, in this case one called organisations.

function SearchFilter($query) {
if ($query->is_search && !is_admin()) {

	// are we wanting to search Custom posts?
	if ($_GET['bsearch'] =="yes"){
			$query->set('post_type', 'organisations');
	}
	else{
			//default behavour we want is to search posts only
			$query->set('post_type', 'post');
	}

}
return $query;
}

add_filter('pre_get_posts','SearchFilter');