Some useful wordpress functions

Here is a list of some useful WordPress functions.This may reduce your development time.

wp_mail()

Some time we need to send email to the user.We can use this wordpress function wp_mail() to send email.It works just like the php email function but have better functionality.

wp_loginout()

Displays a login link, or if a user is logged in, displays a logout link.You can even give the redirect url here.SO user will redirect to that url.

esc_url()

This function ejects URLs that do not have one of the provided whitelisted protocols (defaulting to http, https, ftp, ftps, mailto, news, irc, gopher, nntp, feed, and telnet), eliminates invalid characters, and removes dangerous characters.

is_page()

With this function you can check if that particular page is being load.Very handy if you want to load a script only a specific page

Disable Commenting on Posts Older Than 1 Month:

<?php

		function close_comments( $posts ) {

			if ( !is_single() ) { return $posts; }

			if ( time() - strtotime( $posts[0]->post_date_gmt ) > ( 30 * 24 * 60 * 60 ) ) {
			$posts[0]->comment_status = 'closed';
			$posts[0]->ping_status    = 'closed';
			}

		return $posts;
		}
		add_filter( 'the_posts', 'close_comments' );

		?>

Custom Excerpt Length:

function new_excerpt_length($length) {
return 100;
}
add_filter('excerpt_length', 'new_excerpt_length');
0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply