Quantcast
Channel: New2WP » Rookie
Viewing all articles
Browse latest Browse all 11

15 More WordPress And jQuery Codes To Add To Your Collection

0
0

Want More?

This is an addition to the 15 snippets I posted a couple days ago for preparing your theme for WordPress 3.0. These are however, good for anytime, not just 3.0, and some not necessarily only usable in WordPress even..

Use jQuery To Switch CSS Stylesheets

If you create multiple Stylesheets, or themes for your site you can use jQuery to switch them. You could then create a link on the page that links to the Stylesheet file, and this script will switch it for you.

$('link[media='screen']').attr('href', 'Alternative.css');

Using Custom Fields To Display Pull Quotes

Using this code you can add pull-quotes to your blog posts using custom fields. You just set up a custom field with the key setting of 'pullquote', and then put your quote in the value field. The style of your <blockquote> is controlled in your CSS. You will have to set that up if you prefer something fancy.

<?php if(get_post_meta($post->ID, pullquote, true) != "") { ?>
   <blockquote cite="<?php echo get_permalink() ?>" class="ex-pullquote">
     <p><?php echo get_post_meta($post->ID, pullquote, true); ?></p>
   </blockquote>
 <?php } ?>

Equal Size Div Heights With jQuery

With this code you can easily make a group of divs, with a specific class or id attribute, have an equal size height. It will get the height of the tallest and add that height to the others. Simply enter the class or id of the divs in here: equalHeight($(".DivClassName li"));.

$(function() {
	function equalHeight(group) {
		var tallest = 0;
		group.each(function() {
			var thisHeight = $(this).height();
			if(thisHeight > tallest) {
				tallest = thisHeight;
			}
		});
		group.height(tallest);
	}
	equalHeight($(".DivClassName li"));
	equalHeight($(".anotherDivClassName"));
});

Google Analytics Top Content

This is a great way to show your sites most popular posts as according to Google. Using Google Analytics, you can get the metrics from Top Content from it and display it as a list on your site.

<?php
$start = date('Y-m-d', (time() - (60 * 60 * 24 * 30)));
$end = date('Y-m-d');
$showpages = 5;
$thispage = 1;
 
$login = new GADWidgetData();
$ga = new GALib($login->auth_token, $login->account_id, 60);
$pages = $ga->pages_for_date_period($start, $end);
 
echo "<ul>";
foreach($pages as $page) {
	$url = $page['value'];
	$title = $page['children']['value'];
	echo '<li><a href="' . $url . '">' . $title . '</a></li>';
	$thispage++;
	if($thispage > $showpages) break;
}
echo "</ul>";
?>

Show Recently Registered Members

Create an unordered list of all of your new members.

<ul>
	<?php $usernames = $wpdb->get_results("SELECT user_nicename, user_url FROM $wpdb->users ORDER BY ID DESC LIMIT 5");

		foreach ($usernames as $username) {
    			echo '<li><a href="'.$username->user_url.'">'.$username->user_nicename."</a></li>";
		}
	?>
</ul>

Displaying Random Posts In WordPress

Show your visitors posts in a random order, or set up a custom template for doing this by creating a function and adding query('random=true') to your loop query.

<?php
function query_random_posts($query) {
	return query_posts($query . '&random=true');
}
class RandomPosts {
	function orderby($orderby) {
		if (get_query_var('random') == 'true')
			return "RAND()";
		else
			return $orderby;
	}
	function register_query_var($vars) {
		$vars[] = 'random';
		return $vars;
	}
}
add_filter( 'posts_orderby', array('RandomPosts', 'orderby') );
add_filter( 'query_vars', array('RandomPosts', 'register_query_var') );
?>

<?php // add this in template file ?>
<?php query_posts('cat=11&showposts=11&random=true'); ?>

Adding More Avatar Options For Users

Give your members more choices to choose from when picking their avatars by adding your own custom avatar images.

// add more default avatars to options
if (!function_exists('fb_addgravatar')) {
	function fb_addgravatar($avatar_defaults) {

		$myavatar1 = get_bloginfo('template_directory').'/images/avatar-01.png';
		$avatar_defaults[$myavatar1] = 'dude';
 
		$myavatar2 = get_bloginfo('template_directory').'/images/avatar-02.png';
		$avatar_defaults[$myavatar2] = 'geek';
 
		$myavatar3 = get_bloginfo('template_directory').'/images/avatar-03.png';
		$avatar_defaults[$myavatar3] = 'cool';

		return $avatar_defaults;
	}
	add_filter('avatar_defaults', 'fb_addgravatar');
}

Always Show Spelling Of WordPress Correctly

This is a great hack to prevent spelling mistakes when you write the word 'Wordpress'. It is a similar hack to Antonio's post title capitalization code.

<?php
function writeWordPressCorrect($content) {
	$content = str_ireplace("wordpress","WordPress", $content);
	return $content;
}
add_filter('the_title', 'writeWordPressCorrect',1);
add_filter('the_content', 'writeWordPressCorrect',1);
?>

Get Declared Classes

<?php
class aCustomClass {
	// this is a custom class 
}
$classes = get_declared_classes();

foreach( $classes as $class ) {
	echo $class . '<br />';
}
?>

Remove Dashboard Menus

Customize your clients dashboard to only show menus they need to see, and remove things that they do not need to worry about or could potentially mess things up with.

function remove_menus () {
global $menu;
		$restricted = array(__('Dashboard'), __('Posts'), __('Media'), __('Links'), __('Pages'), __('Appearance'), __('Tools'), __('Users'), __('Settings'), __('Comments'), __('Plugins'));
		end ($menu);
		while (prev($menu)){
			$value = explode(' ',$menu[key($menu)][0]);
			if(in_array($value[0] != NULL?$value[0]:"" , $restricted)){unset($menu[key($menu)]);}
		}
}
add_action('admin_menu', 'remove_menus');

Add / Remove Dashboard Widgets

Customize your dashboard home page to your liking or for a clients site by removing widgets and adding your own to it.

add_action('wp_dashboard_setup', 'my_custom_dashboard_widgets');

function my_custom_dashboard_widgets() {
   global $wp_meta_boxes;

   unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_plugins']);
   unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_primary']);
   unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_secondary']);

   wp_add_dashboard_widget('custom_help_widget', 'Help and Support', 'custom_dashboard_help');
}

function custom_dashboard_help() {
   echo '<p>Welcome to your custom theme! Need help? Contact the us at <a href="http://yoursite.com/contact/">here</a>.</p>';
}

Using Unique Custom Single Templates

Set up customized single templates for showing posts in specific categories.

<?php $post = $wp_query->post;
if (in_category('1')) {
	include(TEMPLATEPATH.'/single-01.php');
} elseif (in_category('2')) {
	include(TEMPLATEPATH.'/single-02.php');
} else {
	include(TEMPLATEPATH.'/single-default.php');
} ?>

List Upcoming Scheduled Posts

<?php query_posts('showposts=9&post_status=future'); ?>
<?php if (have_posts()) : ?>
	<h3>Upcoming Scheduled Posts</h3>
	<ul>
	<?php while (have_posts()) : the_post(); ?>
		<li><?php the_title(); ?> &#8212; <?php the_time('l, F j, Y'); ?></li>
	<?php endwhile; ?>
	</ul>
<?php else: ?>
	<p>Nothing new in queue!</p>
<?php endif; ?>

Fix Exhausted Allowed Memory Issue

If you are one of those that have ever got the error message "Exhausted allowed memory bytes", this is a quick fix for increasing your memory. If this does not work, then you will have to edit the php.ini file. You could try this hack first before you go looking up how to do that.

// Add to wp-config.php
 define('WP_MEMORY_LIMIT', '64M');

Default Configurations In Theme

This is great for theme developers. You can reset your site to the default setup by using the following code.

function set_theme_defaults() {
    $o = array(
        'avatar_default'            => 'blank',
        'avatar_rating'             => 'G',
        'category_base'             => '/thema',
        'comment_max_links'         => 0,
        'comments_per_page'         => 0,
        'date_format'               => 'd.m.Y',
        'default_ping_status'       => 'closed',
        'default_post_edit_rows'    => 30,
        'links_updated_date_format' => 'j. F Y, H:i',
        'permalink_structure'       => '/%year%/%postname%/',
        'rss_language'              => 'de',
        'timezone_string'           => 'Etc/GMT-1',
        'use_smilies'               => 0,
    );
 
    foreach ( $o as $k => $v )
    {
        update_option($k, $v);
    }
 
    // Delete dummy post and comment.
    wp_delete_post(1, TRUE);
    wp_delete_comment(1);
 
    return;
}
register_activation_hook(__FILE__, 'set_theme_defaults');

That's it!

I hope these can be as helpful to you as they are for me. What are some of your favorite and most useful scripts? Share with us in the comments below.


Viewing all articles
Browse latest Browse all 11

Latest Images

Trending Articles





Latest Images