Edit

Template Coding Wordpress Various WP snippets

Show Links Menu w/Active Class

<nav>
<?php $args=array(
	'sort_order' => 'ASC',
	'sort_column' => 'post_date',
	'hierarchical' => 1,
	'exclude' => '',
	'include' => '',
	'meta_key' => '',
	'meta_value' => '',
	'authors' => '',
	'child_of' => 0,
	'parent' => -1,
	'exclude_tree' => '',
	'number' => '',
	'offset' => 0,
	'post_type' => 'page',
	'post_status' => 'publish'
);
$pages = get_pages($args);
$current_id = get_the_ID();
if( $pages ){
    echo( "<ul>\n" );
    foreach( $pages as $page){
        $link_page_id = ($page->ID);
        if( $current_id==$link_page_id ) echo( "<li class=\"active\">" );
        else echo( "<li>" );
        echo( "<a href=\"".get_page_link($page->ID)."\">".$page->post_title."</a>" );
        echo( "</li>\n" );
    }
    echo( "</ul>\n\n" );
}
?>
</nav>

Show Categories List

<?php
    echo( "<div id=\"topics\">\n" );
    echo( "<h3>Topics</h3>\n" );
    echo( "<ul>\n" );
    $categories_obj = get_categories( array('orderby'=>'id','hide_empty'=>'1') );
    foreach( $categories_obj as $cat ){
        echo( "<li>" );
        echo( "<a href=\"/category/".$cat->slug."\">".$cat->name."</a>" );
        $posts_in_cat = new WP_Query( 'cat='.$cat->term_id );
        if ( $posts_in_cat->have_posts() ){
            echo( "<ul>\n" );
            while ( $posts_in_cat->have_posts() ){
                $posts_in_cat->the_post();
                echo( "<li><a href=\"");
                the_permalink();
                echo( "\">" );
                the_title();
                echo( "</a></li>\n" );
            }
            echo( "</ul>\n" );
        }
        echo( "</li>\n" );
    }
    echo( "</ul>\n" );
    echo( "</div>\n\n" );
?>

Show Recent Posts List

<?php
    echo( "<h2>What's New</h2>\n" );
    echo( "<ul>\n" );
    $latest_blog_posts = new WP_Query( array('posts_per_page'=>5) );
    if ( $latest_blog_posts->have_posts() ) :
        while ( $latest_blog_posts->have_posts() ) :
            $latest_blog_posts->the_post();
            // Loop output goes here
            echo( "<li><a href=\"");
            the_permalink();
            echo( "\">" );
            the_title();
            echo( "</a></li>\n" );
        endwhile;
    endif;
    echo( "</ul>\n" );
}
?>

Show Recent Posts Content

<?php
    echo( "<?h2>Recent Posts<?/h2>\n" );
    $latest_blog_posts = new WP_Query( array( 'posts_per_page'=>5 ) );
    if ( $latest_blog_posts->have_posts() ){
        while ( $latest_blog_posts->have_posts() ){
            $latest_blog_posts->the_post();
            // Loop output goes here
            echo( "<?div class=\"entry\">\n" );
            echo( "<?h3>" );
            echo( "<?a href=\"" );
            the_permalink();
            echo( "\">" );
            the_title();
            echo( "<?/a>" );
            echo( "<?/h3>\n" );
            echo( "<?div class=\"time\">\n" );
            the_date();
            echo( "@" );
            the_time();
            echo( "<?/div>\n" );
            echo( "<?h4>in Categories<?/h4>" );
            the_category();
            the_content();
            echo( "<?/div>\n\n" );
        }
    }
?>

Show Date Menu

<?php
    echo("<h2>Archive</h2>\n");
    echo("<ul>\n");
    wp_get_archives();
    echo("</ul>\n");
?>

Show Tag List

<?php
    echo("<div id=\"tag-bar\">\n");
    echo("<ul>\n");
    $tags_obj = get_tags(array('orderby'=>'name','order'=>'ASC','hide_empty'=>'1'));
    foreach($tags_obj as $tag){
        echo("<li>");
        echo("<a href=\"/tag/".$tag->slug."\">".$tag->name."</a>");
        echo(" (".$tag->count.")");
        echo("</li>\n");
    }
    echo("</ul>\n");
    echo("</div>\n\n");
?>

Add ability to execute php in text widgets

<?php
add_filter('widget_text','php_text',99);
function php_text($text){
    if(strpos($text,'<'.'?')!==false){
        ob_start();
        eval('?'.'>'.$text);
        $text=ob_get_contents();
        ob_end_clean();
    }
    return $text;
}
?>

Removes the automatic addition of P and BR elements to content

<?php
remove_filter('the_content','wpautop');
remove_filter('the_excerpt','wpautop');
remove_filter('term_description','wpautop');
?>

Using email as username in account registration

<?php
if(isset($_POST['user_login'])&&isset($_POST['user_email'])){
    $_POST['user_login']=$_POST['user_email'];
}
?>

Storing user-selected password into database on registration

<?php
add_action('user_register','ts_register_extra_fields',100);
function ts_register_extra_fields($user_id){
    $userdata=array();
    $userdata['ID']=$user_id;
    if($_POST['user_password']!==''){
        $userdata['user_pass']=$_POST['user_password'];
    }
    $new_user_id=wp_update_user($userdata);
}
?>

Add ability to add featured images to pages and posts

<?php
add_theme_support('post-thumbnails');
?>

Add ability to execute shortcodes in text widgets

<?php
add_filter('widget_text','do_shortcode');
?>

Define menus

<?php
if(function_exists('register_nav_menus')){
    register_nav_menus(
        array(
            'main_menu'=>'Main Menu',
            'footer_menu'=>'Footer Menu',
        )
    );
}
?>

Define widget regions

<?php
if(function_exists('register_sidebar')){
    register_sidebar(array(
        'name'=>'Footer',
        'id'=>'footer',
        'before_widget'=>'<div>',
        'after_widget'=>'</div>',
        'before_title'=>'<h2>',
        'after_title'=>'</h2>',
    ));
    register_sidebar(array(
        'name'=>'Footer Right',
        'id'=>'footer_right',
        'before_widget'=>'<div>',
        'after_widget'=>'</div>',
        'before_title'=>'<h2>',
        'after_title'=>'</h2>',
    ));
    register_sidebar(array(
        'name'=>'Sidebar Right',
        'id'=>'sidebar_right',
        'before_widget'=>'<div>',
        'after_widget'=>'</div>',
        'before_title'=>'<h2>',
        'after_title'=>'</h2>',
    ));
}
?>