Blog: Wordpress functions

Increase WordPress upload size limit

Wordpress functions

I found this useful to increase the WordPress upload size limit. Not only will it increase the upload size limit, but it increases the memory restriction for resizing images too. So if you have a large image for which WordPress can’t create the thumbnail for, try this.

Place this code in /wp-admin/php.ini:

memory_limit = 256M
upload_max_filesize = "64M"
post_max_size = "64M"
max_execution_time = "500"
max_input_time = "500"


WordPress: Find if your post is in a particular category

Wordpress functions

Put this in your theme’s functions.php file:
function is_in_cats($catID){
 global $post;

 foreach((get_the_category($post->ID)) as $cat) {
 if( in_array($catID, get_category_array()) ){
 return true;
 }
 }
 return false;
}
Usage:

if(is_in_cats(5)){

echo "Post is in category 5";

} else {

echo "Post is not in category 5";

}


WordPress: Change the excerpt length in the loop / on-the-fly

Wordpress functions

This function allows you to display a custom except length which you can change at various points in your theme’s template file. On a website I needed the first post to have a longer except than the subsequent ones. So I used the regular the_excerpt() function to display my first (longer) excerpt, then for subsequent posts used this new function to display a shorter one. Place this in your theme’s functions.php file:
function custom_excerpt($length){
 global $post;
 $content = strip_tags($post->post_content);
 preg_match('/^\s*+(?:\S++\s*+){1,'.$length.'}/', $content, $matches);
 echo "<p>" . $matches[0] . "</p>";
}
Use it like this, changing the argument for the number of words you want:
custom_excerpt(20);

WordPress: Server migration (404s, broken images, broken links etc)

Wordpress functions

Moving your WordPress website to a different server? You need to do the following:

  1. Download all files from your old server via FTP. Don’t forget /wp-content/uploads/ and /wp-content/plugins/ – you may have uploaded files or updated plugins.
  2. Upload the files to the new server via FTP
  3. Export the MySQL database from the old server as a file
  4. Import export file into a new MySQL database on the new server
  5. Amend /wp-config.php with the new database login details

With this done, your new website will probably show a 404 error page, and your image URLs and hyperlinks will still reference the old server address. What to do? You need to run these 4 SQL commands on your new database, replacing www.olddomain.com and www.newdomain.com in each instance:

UPDATE wp_options SET option_value = replace(option_value, 'http://www.olddomain.com','http://www.newdomain.com');

UPDATE wp_posts SET guid = replace(guid, 'http://www.olddomain.com','http://www.newdomain.com');

UPDATE wp_posts SET post_content = replace(post_content, 'http://www.olddomain.com','http://www.newdomain.com');

UPDATE wp_postmeta SET meta_value = replace(meta_value, 'http://www.olddomain.com','http://www.newdomain.com');


WordPress: Exclude categories from the_category()

Wordpress functions

Put this in functions.php:

function the_excluded_category($excludedcats = array()){
	$count = 0;
	$categories = get_the_category();
	foreach($categories as $category) {
		$count++;
		if ( !in_array($category->cat_ID, $excludedcats) ) {
			echo '<a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "Cortos de %s" ), $category->name ) . '" ' . '>' . $category->name.'</a>';

			if( $count != count($categories)-1 ){
				echo ", ";
			}

		}
	}
}

Usage:
Call it in your template file like this:


<?php the_excluded_category(array(1,328,338,339)); ?>


WordPress: Return a list of page ID’s from array of page objects

Wordpress functions

Insert this in your theme’s functions.php file:


function create_id_list($pages, $explode = false){
 $pagelist = array();
 foreach ($pages as $pagg){
 $pagelist[] = $pagg->ID;
 }
 if($explode == true){
 return implode(",", $pagelist);
 } else {
 return $pagelist;
 }
}

Usage:


$mypages = get_pages('child_of='.$post->ID.'&sort_column=post_date&sort_order=desc');

$idlist = create_id_list($mypages); // Returns an array
$idlistcsv = create_id_list($mypages, true); // Returns a comma-separated list


WordPress: Get array of post’s categories

Wordpress functions

Insert this in your functions.php  file:


// Returns an array of the current post's categories
function get_category_array(){
 global $post;
 $cat_array = array();

 foreach((get_the_category()) as $category) {
 $cat_array .= $category->cat_ID;
 }
 return $cat_array;
}


WordPress: Redirect to first child page

Wordpress functions

Add this to whichever of your theme’s template files you want to redirect:


// Redirect to first child page. Place this in the loop.
function redirect_to_first_child(){
 $pagekids = get_pages("child_of=".$post->ID."&sort_column=menu_order");
 if ($pagekids) {
 $firstchild = $pagekids[0];
 wp_redirect(get_permalink($firstchild->ID));
 }
}


WordPress: Does a page have children? With has_children()

Wordpress functions

Add this to your functions.php file:


function has_children(){
 global $post;

 if( ($post->post_parent == 0 && wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0'))
 ||
 ($post->post_parent > 0 && wp_list_pages('title_li=&child_of='.$post->post_parent.'&echo=0'))
 ){
 return true;
 } else {
 return false;
 }
}


WordPress: Change the link text after the excerpt

Wordpress functions

Add this to your functions.php file:


function new_excerpt_more($more){
global $post;
return " [...]<br/> <a href='".get_permalink() ."#post-".$post->ID."' class='read-more'>Read More ></a>";
}
add_filter('excerpt_more', 'new_excerpt_more');

You can change the return code to change the text added to the link, or change this for an image for instance.