WordPress Template Functions & Plugins

Posted: January 4, 2012 in Wordpress

Here is the fourth and last part of a tutorial on how to develop WordPress templates, it explains useful functions and useful plugins.

To access other parts of the tutorial see the WordPress Template Tutorial post.

Useful Functions

Here are some general utility functions, will sure come handy:

wp_head(); // Attach header
wp_footer(); // Attach footer
get_sidebar(); // Attach sidebar

get_option('home'); // Returns home url
get_bloginfo('url');
get_bloginfo('template_url');
$GLOBALS[$lang_home] = split("\?", get_option('home'));  // $lang_home[0] is like bloginfo('url') but without url variables

get_cat_name($cat_id);
get_cat_ID($cat_name);
single_term_title("", false); // Returns the CURRENT category title
get_category_link($cat_id);
get_category_parents($cat_id, FALSE, ' - ', FALSE); // arguments: (category), (display link), (separator), (nice name)  

// get_page id from name
function get_page_id($page_name){
	global $wpdb;
	$page_name = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = '".$page_name."'");
	return $page_name;
}

// print categories sorted by id
$subtitle = '';
$cats = get_the_category();
for($i=0;$i<count($cats);$i++){
	$subtitlearr[$cats[$i]->cat_ID]=$cats[$i]->cat_name;
}
ksort($subtitlearr);
$i=0;
foreach ($subtitlearr as $key => $value) {
	$i++;
	$subtitle .= $value;
	if($i!=(count($subtitlearr))){$subtitle .=" - ";}
}

// current cat also on posts
$var = get_the_category();
$cat = $var[0]->cat_ID;
$var=wp_list_categories('current_category='.$cat);

// get_the_content_with_formatting() for storing and editing the_content() inside a variable
function get_the_content_with_formatting ($more_link_text = '(more...)', $stripteaser = 0, $more_file = '') {
	$content = get_the_content($more_link_text, $stripteaser, $more_file);
	$content = apply_filters('the_content', $content);
	$content = str_replace(']]>', ']]&gt;', $content);
	return $content;
}

// WPML functions
ICL_LANGUAGE_CODE // Actual language code
icl_object_id(3, 'category', false, 'en'); // arguments: (the ID of the post, page, tag or category), (type – ‘post’, ‘page’, ‘post_tag’ or ‘category’), (return_original_if_missing), (language code to return id)

// get page meta for WPML, useful if you are storing some general data into pages
$t_lang = icl_object_id('268',  'page', false);
$desc = get_post_meta($t_lang, 'desc-machines', true);
echo $desc;

// Translate redirect with WPML, useful to put inside index.php if you want redirection only in index
$browser_lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
$ref = split("\?", get_bloginfo('url'));
$ref2 = split("\?", $_SERVER['HTTP_REFERER']);
if(is_home()){
	if (($browser_lang != ICL_LANGUAGE_CODE) && (!stristr($ref2[0], $ref[0])) ) {
		$languages = icl_get_languages('skip_missing=1');
		foreach($languages as $l){
			if ($browser_lang == $l['language_code']) { /* native_name */
				header('Location:'.$ref[0].'?lang='.$l['language_code']); exit;
			}
		}
	}
}

// Find ID of Top-Most Parent Post
if ($post->post_parent)	{
	$ancestors=get_post_ancestors($post->ID);
	$root=count($ancestors)-1;
	$parent = $ancestors[$root];
} else {
	$parent = $post->ID;
}

// Breadcrumbs
echo '<ul>';
if (!is_home()) {
	echo '<li><a href="';
	echo get_option('home');
	echo '">';
	echo 'Home';
	echo "</a></li>";
	if (is_category() || is_single()) {
		echo '<li>';
		the_category(' </li><li> ');
		if (is_single()) {
			echo "</li><li>";
			the_title();
			echo '</li>';
		}
	} elseif (is_page()) {
		echo '<li>';
		echo the_title();
		echo '</li>';
	}
}
elseif (is_tag()) {single_tag_title();}
elseif (is_day()) {echo"<li>Archive for "; the_time('F jS, Y'); echo'</li>';}
elseif (is_month()) {echo"<li>Archive for "; the_time('F, Y'); echo'</li>';}
elseif (is_year()) {echo"<li>Archive for "; the_time('Y'); echo'</li>';}
elseif (is_author()) {echo"<li>Author Archive"; echo'</li>';}
elseif (isset($_GET['paged']) && !empty($_GET['paged'])) {echo "<li>Blog Archives"; echo'</li>';}
elseif (is_search()) {echo"<li>Search Results"; echo'</li>';}
echo '</ul>';
Useful Plugins

Here are the must have plugins:

  • WPML Multilingual: WPML combines multilingual content authoring with powerful translation management. It powers corporate sites and is simple enough for bloggers.
  • Magic Fields: Magic Fields is a feature rich WordPress CMS plugin.
  • WP Minify: This plugin uses the Minify engine to combine and compress JS and CSS files to improve page load time.
  • WP Super Cache: A very fast caching engine for WordPress that produces static html files.
  • WP htaccess Control: Interface to customize the permalinks (author, category, archives and pagination) and htaccess file generated by WordPress.

Courtesy

Source: http://www.minimit.com/articles/wordpress-template-functions-plugins

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s