WordPress: Cool Tips & Tricks

Below are some of cool WordPress tips & tricks that can prove very handy and help you add some awesomeness to your WordPress based website with comparatively less effort and quickly.

Include options to associate Social channels to Author profile

Author profile on Admin screen on WordPress does not include any options to add social channel information.
Step 1: Add below code to functions.php file under template folder:

function social_contactmethods( $contactmethods ) {
	// Add Twitter
	$contactmethods['twitter'] = 'Twitter';
	//Add Facebook
	$contactmethods['facebook'] = 'Facebook';
	//Add Facebook
	$contactmethods['linkedin'] = 'LinkedIn';
	return $contactmethods;
}
add_filter('user_contactmethods','social_contactmethods',10,1);

This will provide you controls on User Profile page where you can add/update the links to these social channels.
SocialOptions
Step 2: Update your page template with below code to use the information on public website:

/***********************************************
the_author_meta('facebook');
the_author_meta('twitter');
the_author_meta('linkedin');
***********************************************/

//Above PHP code can be use to construct below HTML component
<ul id="socialProfile">
	<?php if (get_the_author_meta('facebook')) { ?>
		<li class="fb"><a href="<?php the_author_meta('facebook'); ?>" title="View user profile on Facebook" target="_blank">Facebook</a></li>
	<?php } ?>
	<?php if (get_the_author_meta('twitter')) { ?>
		<li class="tw"><a href="<?php the_author_meta('twitter'); ?>" title="View user profile on Twitter" target="_blank">Twitter</a></li>
	<?php } ?>
	<?php if (get_the_author_meta('linkedin')) { ?>
		<li class="ln"><a href="<?php the_author_meta('linkedin'); ?>" title="View user profile on LinkedIn" target="_blank">LinkedIn</a></li>
	<?php } ?>
</ul>

Customize WordPress homepage to show page contents as well as blog simultaneously

WordPress allows option either to have blog posts on home screen or a static page (without blog posts) as your homepage. But, in case you want both the options on homepage itself, open home.php and use below code:

<?php
if ( 'page' == get_option('show_on_front') && get_option('page_for_posts') && is_home() ) : the_post();
   $page_for_posts_id = get_option('page_for_posts');
   setup_postdata(get_page($page_for_posts_id));
 ?>
   <div class="homeContent" id="post-<?php the_ID(); ?>">
       <?php the_content(); ?><
   </div>
<?php
  rewind_posts();
  endif;
?>

Know result count of Search query

<p><?php $allsearch = &new wpus_Query("s=$s&showposts=-1"); $key = wpus_specialchars($s, 1); $count = $allsearch->post_count; _e(' Found '); echo $count . ' '; _e(' results'); wpus_reset_query(); ?></p>

Modify Excerpt Length and More Tags

Open your functions.php file and add the following codes in there:

// Changing excerpt length
function new_excerpt_length($length) {
return 100;
}
add_filter('excerpt_length', 'new_excerpt_length');

// Changing excerpt more
function new_excerpt_more($more) {
return '…';
}
add_filter('excerpt_more', 'new_excerpt_more');

Add page/post slug as id on Body Tag

This method can prove to be critical solution where specific behavior needs to be implemented on a particular page using same shared template. All JS & CSS based implementation can be done based on that ID (i.e. page slug).

<body <?php body_class(); ?> id="<?php echo get_query_var('name'); ?>">

Importing page content on a template

The approach provides very useful where a section is used across various pages and so we can maintain that section as separate page and import it in the page template. This way any change to the sectioned page is reflected throughout website and we won’t need to make changes everywhere.

<?php
        //Replace "page-slug" below with the actual page slug with needs to be imported on that template
	$page = get_posts( array( 'name' => 'page-slug','post_type' => 'page' ) );
	if ( $page ){	echo $page[0]->post_content; }
?>

Limit the number of search/category results per page

Add below code to functions.php file under template folder:

function limit_posts_per_archive_page() {
	if ( is_category() )
		$limit = 8;
	elseif ( is_search() )
		$limit = 10;
	else
		$limit = get_option('posts_per_page');

	set_query_var('posts_per_archive_page', $limit);
}
add_filter('pre_get_posts', 'limit_posts_per_archive_page');

Redirecting to the post directly if search results returns single post

Add below code to functions.php file under template folder:

add_action('template_redirect', 'redirect_single_post');
function redirect_single_post() {
    if (is_search()) {
        global $wpus_query;
        if ($wpus_query->post_count == 1 && $wpus_query->max_num_pages == 1) {
            wpus_redirect( get_permalink( $wpus_query->posts['0']->ID ) );
            exit;
        }
    }
}

Enjoy playing with WordPress!!

(Visited 20 times, 1 visits today)