21 Fresh WordPress Code Snippets and Detailed Tutorials

   

You know wordpress is the most popular content management system for years and basic wordpress functions may be enough for people who are blogging using wordpress platform but if you want your wordpress blog stand out from the crowd you can change many things to make it more functional.Today we want to share useful wordpress tricks and hacks with you.I’m sure some of them will definetely come in handy for your blog.



Show WordPress Child Pages Alongside Parent Page

This goes in your page template or sidebar template where you want your submenu to appear. It is outside the loop.

post_parent);
if($post->post_parent)
$children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0"); else
$children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");

if ($children && is_page()) { ?>
<div class="subnav">
<ul>
	<li><a href="<?php echo get_permalink($post->post_parent) ?>"></a></li>
</ul>
</div>

Source

How To Move the Admin Bar to the Bottom

Copy and paste this code and place it on your functions.php

// move admin bar to bottom
function fb_move_admin_bar() { ?>
    <!--
        body {
            margin-top: -28px;
            padding-bottom: 28px;
        }
        body.admin-bar <a href="http://search.twitter.com/search?q=%23wphead" class="tweet-hashtag">#wphead</a> {
            padding-top: 0;
        }
        body.admin-bar <a href="http://search.twitter.com/search?q=%23footer" class="tweet-hashtag">#footer</a> {
            padding-bottom: 28px;
        }
        <a href="http://search.twitter.com/search?q=%23wpadminbar" class="tweet-hashtag">#wpadminbar</a> {
            top: auto !important;
            bottom: 0;
        }
        <a href="http://search.twitter.com/search?q=%23wpadminbar" class="tweet-hashtag">#wpadminbar</a> .quicklinks .menupop ul {
            bottom: 28px;
        }

-->
// on backend area
add_action( 'admin_head', 'fb_move_admin_bar' );
// on frontend area
add_action( 'wp_head', 'fb_move_admin_bar' );

Source

How To Make Your WordPress Post Look Nice When Shared On Facebook (Updated)

The code allows you to set the “Featured Image” as the the image that Facebook shows when your link is shared. Secondly, it allows you to set a default image, in case your post does not make use of a “featured image”.You can use your logo for this default, by saving it in your themes directory, in a /images/ directory with the file name as default_icon.jpg. Or you can alter the image directory in the code below.

ID,'_thumbnail_id',false);
	$thumb = wp_get_attachment_image_src($thumb[0], false);
	$thumb = $thumb[0];
	$default_img = get_bloginfo('stylesheet_directory').'/images/default_icon.jpg';

	?>

	" />

Source

How To Add More Button on Visual Mode WordPress Editor

Open functions.php on your current theme using your favorite editor and add this code

function add_more_buttons($buttons) {
  $buttons[] = 'hr';
  $buttons[] = 'del';
  $buttons[] = 'sub';
  $buttons[] = 'sup';
  $buttons[] = 'fontselect';
  $buttons[] = 'fontsizeselect';
  $buttons[] = 'cleanup';
  $buttons[] = 'styleselect';
  return $buttons;
}
add_filter("mce_buttons_3", "add_more_buttons");

Source

How To Automatically Add FTP Detail on WordPress

Open your wp-config.php and add this code

define('FTP_HOST', 'Your_FTP_Hosting');
define('FTP_USER', 'Your_FTP_Username');
define('FTP_PASS', 'Your_FTP_Password);
//If you use SSL connection, set this to true
define('FTP_SSL', false);

Source

How to Change WordPress Editor Font

Paste the following code to your function.php file

add_action( 'admin_head-post.php', 'devpress_fix_html_editor_font' );
add_action( 'admin_head-post-new.php', 'devpress_fix_html_editor_font' );

function devpress_fix_html_editor_font() { ?>
<!--
#editorcontainer #content, #wp_mce_fullscreen { font-family: Georgia, "Times New Roman", "Bitstream Charter", Times, serif; }
-->

Source

How to Remove the “read more” Jump

Paste the below code in your functions.php file

function wdc_no_more_jumping($post) {
     return '<a class="read-more" href="'.get_permalink($post->ID).'">'.'Continue Reading'.'</a>';
}
add_filter('excerpt_more', 'wdc_no_more_jumping');

Source

Use Shortcodes in Widgets

Paste the following code to your functions.php file

// shortcode in widgets

      if ( !is_admin() ){

          add_filter('widget_text', 'do_shortcode', 11);

      }

Source

How to Add a New link to the WP Admin Bar and Remove an Existing Link.

Simply add this code to your functions.php file

remove_menu('comments');
    // or we can remove a submenu, like New Link.
    $wp_admin_bar->remove_menu('new-link', 'new-content');
    // we can add a submenu item too
    $wp_admin_bar->add_menu( array(
        'parent' => 'new-content',
        'id' => 'new_media',
        'title' => __('Media'),
        'href' => admin_url( 'media-new.php')
    ) );
}
// and we hook our function via
add_action( 'wp_before_admin_bar_render', 'mytheme_admin_bar_render' );
?>

Here are some IDs for the default links WP 3.1 adds, found in /wp-includes/admin-bar.php:

my-account / my-account-with-avatar : the first link, to your account. Note that the ID here changes depending on if you have Avatars enabled or not.
my-blogs : the ‘My Sites’ menu if the user has more than one site
get-shortlink : provides a Shortlink to that page
edit : link to Edit [content-type]
new-content : the ‘Add New’ dropdown
comments : the ‘Comments’ dropdown
appearance : the ‘Appearance’ dropdown
updates : the ‘Updates’ dropdown

Source

How to Disable Self Trackbacks

Paste the following code to your functions.php file

function disable_self_ping( &$links ) {
    foreach ( $links as $l => $link )
        if ( 0 === strpos( $link, get_option( 'home' ) ) )
            unset($links[$l]);
}
add_action( 'pre_ping', 'disable_self_ping' );

Source

How to Display Most Commented Posts of Specific Year Without Plugin

Paste the following code in the sidebar.php or where ever you want

<ul>
get_results("SELECT comment_count,ID,post_title, post_date FROM $wpdb->posts WHERE post_date BETWEEN 'YEAR-MM-DD (start)' AND 'YEAR-MM-DD (end)' ORDER BY comment_count DESC LIMIT 0 , 10");

foreach ($result as $topten) {
    $postid = $topten->ID;
    $title = $topten->post_title;
    $commentcount = $topten->comment_count;
    if ($commentcount != 0) {
    ?>

</ul>

Source

How to Display Tags as Dropdown

Add the below code to your function.php you will turn your standard tag cloud as a cool dropdown menu.

 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45,
		'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC',
		'exclude' => '', 'include' => ''
	);
	$args = wp_parse_args( $args, $defaults );

	$tags = get_tags( array_merge($args, array('orderby' => 'count', 'order' => 'DESC')) ); // Always query top tags

	if ( empty($tags) )
		return;

	$return = dropdown_generate_tag_cloud( $tags, $args ); // Here's where those top tags get sorted according to $args
	if ( is_wp_error( $return ) )
		return false;
	else
		echo apply_filters( 'dropdown_tag_cloud', $return, $args );
}

function dropdown_generate_tag_cloud( $tags, $args = '' ) {
	global $wp_rewrite;
	$defaults = array(
		'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45,
		'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC'
	);
	$args = wp_parse_args( $args, $defaults );
	extract($args);

	if ( !$tags )
		return;
	$counts = $tag_links = array();
	foreach ( (array) $tags as $tag ) {
		$counts[$tag->name] = $tag->count;
		$tag_links[$tag->name] = get_tag_link( $tag->term_id );
		if ( is_wp_error( $tag_links[$tag->name] ) )
			return $tag_links[$tag->name];
		$tag_ids[$tag->name] = $tag->term_id;
	}

	$min_count = min($counts);
	$spread = max($counts) - $min_count;
	if ( $spread <= 0 )
		$spread = 1;
	$font_spread = $largest - $smallest;
	if ( $font_spread <= 0 ) 		$font_spread = 1; 	$font_step = $font_spread / $spread; 	// SQL cannot save you; this is a second (potentially different) sort on a subset of data. 	if ( 'name' == $orderby ) 		uksort($counts, 'strnatcasecmp'); 	else 		asort($counts); 	if ( 'DESC' == $order ) 		$counts = array_reverse( $counts, true ); 	$a = array(); 	$rel = ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() ) ? ' rel="tag"' : '';

	foreach ( $counts as $tag => $count ) {
		$tag_id = $tag_ids[$tag];
		$tag_link = clean_url($tag_links[$tag]);
		$tag = str_replace(' ', ' ', wp_specialchars( $tag ));
		$a[] = "\t$tag ($count)";
	}

	switch ( $format ) :
	case 'array' :
		$return =& $a;
		break;
	case 'list' :
		$return = "
<ul class="wp-tag-cloud">\n\t
	<li>";
		$return .= join("</li>
\n\t
	<li>", $a);
		$return .= "</li>
\n</ul>
\n";
		break;
	default :
		$return = join("\n", $a);
		break;
	endswitch;

	return apply_filters( 'dropdown_generate_tag_cloud', $return, $tags, $args );
}
?>

Source

Very Detailed WordPress Tutorials

How to Load JavaScript in the WordPress Admin

How to Add Images with Captions

Custom Columns for Custom Post Types

Custom Background Fix for Theme Developers

Add Trash Button to WordPress Admin Bar

Exclude Posts and Pages in WordPress Search

Linking to All Image Sizes in WordPress

How to Add Twitter official “Follow Me” Button

Fun Edit Posts Link

 

4 Comments

DMCA.com Protection Status