Tag Archives: WordPress

Function For Defining Default Post Thumbnail In WordPress

Open your theme’s function.php file and add the given function editing the thumbnail image URL (given in bold text) with your own image URL.

add_action( ‘save_post’, ‘wptuts_save_thumbnail’ );
function wptuts_save_thumbnail( $post_id ) {
$post_thumbnail = get_post_meta( $post_id, $key = ‘_thumbnail_id’, $single = true );
if ( !wp_is_post_revision( $post_id ) ) {
if ( empty( $post_thumbnail ) ) {
update_post_meta( $post_id, $meta_key = ‘_thumbnail_id’, $meta_value = ‘https://sangkrit.net/thumbnail.jpg’ );
}
}
}

How To Edit WordPress ‘Posts’ Label ?

You can edit WordPress post label to something Else. For example if you want to edit Posts (label) to Article so that anytime you or any other member visit you wp-admin (dashboard) he can see Article as a label on sidebar menu not posts (which is by default).

For doing this, open your theme’s function.php file and add the following code:

add_filter(‘gettext’, ‘change_post_to_article’);
add_filter(‘ngettext’, ‘change_post_to_article’);
function change_post_to_article($translated) {
$translated = str_ireplace(‘Post’, ‘Article’, $translated);
return $translated;
}

How To Add Custom User Roles In WordPress ?

Suppose you need to create a new custom post type, may be NEWS. So that you can allow you subscribers to post NEWS on your website. In the given example we will be creating a custom user role for NEWS and the users allotted this role would only be managing only NEWS section from WP-Admin (Dashboard).

Open your theme’s function.php file: site root >> wp-content >>themes >> function.php and add the given code block:

add_role( ‘news_user’, ‘News User’, array( ‘news’ ) );

This function generates new custom user role which can only manage
news section from Dashboard.

Although you may add some more function for new custom user role.

New custom user role can also be removed from WordPress by using the following function in your theme’s function.php file:

remove_role( ‘news_user’ );

From WordPress Function $wp_roles also you can add and remove custom user roles as show below:

global $wp_roles;
$wp_roles->add_cap( ‘news_user’, ‘manage_category’ );
$wp_roles->remove_cap( ‘news_user’, ‘view_post’ );

WordPress Hack To Set A Custom Default Avatar

  • Create a new custom Avatar. It should be 60px*60px PNG format.
  • Name it custom-avatar.png.
  • Upload this avatar image in your /wp-content/themes/yourtheme/images directory
  • Open you theme’s functions.php file
  • Copy the following given code and paste it in your functions.php file:

if ( !function_exists(‘custom_gravatars’) ) {
function custom_gravatars( $avatar_defaults ) {
$myavatar = get_template_directory_uri() . ‘/images/custom-avatar.png’;
$avatar_defaults[$myavatar] = ‘people’;
return $avatar_defaults;
}
add_filter( ‘avatar_defaults’, ‘custom_gravatars’ );
}

  • Open your Admin Section (Dashboard)
  • Visit Settings >> Reading section and select your newly uploaded avatar as default.

How To Display WordPress Featured Posts And Pages ?

Featured Page Widget

From this widget you can feature pages on your sidebar using an excerpt of the page and a text or image link to the page. Click here to know more features.


Download WordPress Featured Page Widget

Installation:

  1. Install and Activate the plugin
  2. Configure options from Dashboard >> Settings panel.
  3. Add widgets to your sidebar.
Featured Post Widget

It is a customizable multiwidget, that displays a single post in the widget area. You can decide, whether or not the post thumbnail is displayed, whether the post title is above or beneath the thumbnail and a couple of more things. You can style the widget individually.

Download WordPress Featured Post Widget

Installation:

  1. Install and Activate the plugin.
  2. Add and customize the widget.

Embeding SWF In WordPress Posts

You can easily embed SWF file inside your WordPress post either by using a plugin or by using WP hack.

Using WordPress Plugin:

  1. Open your Dasboard
  2. Visit Plugins >> Add New and Search for Easy Flash Embed plugin.
  3. Install and Activate it.

Use the following shortcode for embedding your SWF file in your posts:

[swf src=”http://www.example.com/file.swf” width=200 height=50]

Note: In the given shortcode replace the link address to your own SWF file address and adjust the height and width as per your needs.

Using WordPress Hack:

Here is your WP Hack. Use following code for embedding Flash directly into WordPress pages and posts etc:

<object id=”flashcontent”
classid=”clsid:D27CDB6E-AE6D-11cf-96B8-444553540000″
width=”550px”
height=”400px”>
<param name=”movie” value=”movie.swf” />
<!–[if !IE]>–>
<object type=”application/x-shockwave-flash”
data=”movie.swf”
width=”550px”
height=”400px”>
<!–<![endif]–>
<p>
Text written here will only be visible if the SWF fails to load.
</p>
<!–[if !IE]>–>
</object>
<!–<![endif]–>
</object>

Embedding SoundCloud In Your WordPress Posts

You can embed SoundCloud in your WordPress posts using WordPress feature oEmbed. It supports auto embedding until your URL breaks the line. WP oEmbed library supports Twitter, YouTube and many other popular services. SoundCloud is not yet supported by WordPress but still you have it by using oEmbed with the help of this function: wp_oembed_add_provider() 

Adding oEmbed for SoundCloud in WordPress:

Open your theme’s function.php file and add the following code:

function add_oembed_soundcloud(){
wp_oembed_add_provider( ‘http://soundcloud.com/*’, ‘http://soundcloud.com/oembed’ );
}
add_action(‘init’,’add_oembed_soundcloud’);

All done. Now you can simply paste SoundCloud URL on separate line and let oEmbed do auto-embedding.

Using WordPress Plugin:

You may also use this WordPress Plugin SoundCloud is Gold.

If you are running your blog on WordPress.Com then you may use the shortcode available for all WordPress.Com users because Sound Cloud is officially supported by WordPress.Com.

If you have a self hosted blog then you may use JetPack plugin and enable option for shortcode embeds. After doing this you can easily use the shortcode as shown here:

WordPress Hack To Remove Certain Categories From Being Displayed

This hack is useful for those who like to display certain category to chosen or registered users only. Add the following given code inside The Loop and choose the category (number) which you wish to remove from the display.

<?php
if ( have_posts() ) : query_posts($query_string .’&cat=-1,-2′); while ( have_posts() ) : the_post();
?>

Creating And Managing Tables Inside WordPress Posts

WordPress Visual Editor don’t provides you the option to create table inside your post content. Hence to create a table in WordPress you need to know HTML. Reading this tutorial you can create tables in your WordPress posts even if your are not good in HTML.

  1. Go to plugins menu.
  2. Install Easy Table Plugin and Actvate it.
  3. Move to Dashboard >> Settings >> Easy Table.

Here you will get various table options and previews. Now to start up with your first table use the following given format in your WP post:

[table] Domain,Company,Rank
Google.com,Google,1,
Facebook.com,Facebook Inc,2
[/table]

This is an example. You can create as many formats you want and each format should be enclosed in [table] and [/table] shortcode. Given format creates three columns: Domain, Company and Rank with three rows:

Domain

Company

Rank

Google.com

Google

1

Facebook.com

Facebook Inc.

1

 

 

How To Add Automatically Resized Images In Your Blog Posts

If you post large number of images in your blog and resize each image  to similar frame after you upload it. Then you may add an automatic task that will resize you images automatically:

  1. Copy the script from here.
  2. Create a new folder ‘script’ in your site root from cPanel.
  3. Upload this script in script folder and name it timthumb.php
  4. Use the following syntax for adding automatically resized image in your blog post:

<img src=”/scripts/timthumb.php?src=/images/whatever.jpg&amp;h=150&amp;w=150&amp;zc=1″ alt=”” />

WordPres Spam Words And Moderation

Here are words widely used in spam comments. Adding them to your moderation list protects your site from spam trackback and comments. Just copy-paste this list in your wp-admin/options-discussions.php  (if using WordPress)

What is Moderation ? WordPress Comment moderation allows you to prevent comments to appear on your webiste before your approval. It can be useful for addressing Spam Comment.

How Moderation Works ? A new comment goes through many WordPress tests before appearing below your post. If comment fails one of the test conducted by WordPress then it goes to spam list, where you can approve or trash that particular comment. That’s how comment moderation works.

Akismet is a better option available for WordPress users for blocking Spam Comments.

Following is spam word list (released by WordPress) to add in your WordPress wp-admin/options-discussions.php

-online
4u
adipex
advicer
baccarrat
blackjack
bllogspot
booker
byob
car-rental-e-site
car-rentals-e-site
carisoprodol
casino
casinos
chatroom
cialis
coolcoolhu
coolhu
credit-card-debt
credit-report-4u
cwas
cyclen
cyclobenzaprine
dating-e-site
day-trading
debt-consolidation
debt-consolidation-consultant
discreetordering
duty-free
dutyfree
equityloans
fioricet
flowers-leading-site
freenet-shopping
freenet
gambling-
hair-loss
health-insurancedeals-4u
homeequityloans
homefinance
holdem
holdempoker
holdemsoftware
holdemtexasturbowilson
hotel-dealse-site
hotele-site
hotelse-site
incest
insurance-quotesdeals-4u
insurancedeals-4u
jrcreations
levitra
macinstruct
mortgage-4-u
mortgagequotes
online-gambling
onlinegambling-4u
ottawavalleyag
ownsthis
palm-texas-holdem-game
paxil
penis
pharmacy
phentermine
poker-chip
poze
pussy
rental-car-e-site
ringtones
roulette
shemale
shoes
slot-machine
texas-holdem
thorcarlson
top-site
top-e-site
tramadol
trim-spa
ultram
valeofglamorganconservatives
viagra
vioxx
xanax
zolus

Move Across WordPress Draft, Pending And Publish Post Statuses

Don’t use register_post_status before init. Register Post Status is a function used to create and modify post status based on given parameters. register_post_status() function is located in wp-includes/post.php It accepts following two parameters:

  • $post_status means a string for the post status name
  • $args means an array of arguments

<?php register_post_status( $post_status, $args ); ?>

Example:
(Following example registers “Unread” post status:

function my_custom_post_status(){
register_post_status( ‘unread’, array(
‘label’ => _x( ‘Unread’, ‘post’ ),
‘public’ => true,
‘exclude_from_search’ => false,
‘show_in_admin_all_list’ => true,
‘show_in_admin_status_list’ => true,
‘label_count’ => _n_noop( ‘Unread <span class=”count”>(%s)</span>’, ‘Unread <span class=”count”>(%s)</span>’ ),
) );
}
add_action( ‘init’, ‘my_custom_post_status’ );

Where:

  • label is a descriptive name for the post status marked for translation.
  • public – Set true to show posts of this status in the front end. Defaults to false.
  • exclude_from_search – Set true to exclude posts with this post status in search results. Defaults to false.
  • show_in_admin_all_list – Defines whether to include posts in the edit listing for their post type.
  • show_in_admin_status_list – Shows in the list of statuses with post counts. Example: Published (12)
  • label_count – The text to display on admin screen or you won’t see status count.

Show Related Posts In Your WordPress Blog Without Using Any Plugin

Copy and Paste the given code within the loop. It may engage your blog visitors to stay for a longer time:

<?php
//for use in the loop, list 5 post titles related to first tag on current post
$tags = wp_get_post_tags($post->ID);
if ($tags) {
echo ‘Related Posts’;
$first_tag = $tags[0]->term_id;
$args=array(
‘tag__in’ => array($first_tag),
‘post__not_in’ => array($post->ID),
‘showposts’=>5,
‘caller_get_posts’=>1
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><a href=”<?php the_permalink() ?>” rel=”bookmark” title=”Permanent Link to <?php the_title_attribute(); ?>”><?php the_title(); ?></a></p>
<?php
endwhile;
}
}
?>

This code displays posts related to current post. It is based on tags. You may change the number of posts it displays, just change ‘showposts’=>5 with your number.

Display Popular Posts In Your WordPress Blog’s Sidebar Without Using Any Plugin

You can highlight most popular content of your WordPress site in your sidebar. Copy-Paste the following code in your theme’s sidebar.php file:

<h2>Popular Posts</h2>
<ul>
<?php $result = $wpdb->get_results(“SELECT comment_count,ID,post_title
FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 5″);
foreach ($result as $post) {
setup_postdata($post);
$postid = $post->ID;
$title = $post->post_title;
$commentcount = $post->comment_count;
if ($commentcount != 0) { ?>
<li><a href=”<?php echo get_permalink($postid); ?>” title=”<?php echo
$title ?>”>
<?php echo $title ?></a> {<?php echo $commentcount ?>}</li>
<?php } } ?>
</ul>

It displays most popular posts on your site’s sidebar.

For changing the number of posts to display in your sidebar:

Look-up for this line in the above code block: FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 5″); and Replace 5 with the number of posts you like to display.

Easiest Way for Displaying both Popular and Unpopular Posts in your Sidebar:

Now easiest way to do this (display popular posts) and many other similar things like displaying both popular and unpopular posts, knowing the number of post hits is to use Most and Least Read Posts Widget Plugin

Download from the link, activate it and start using it by placing its widgets in your sidebar. It gives you two different widgets one shows most read posts and the other shows least read posts and also it counts your post hits both on sidebar and in your dashboard All Posts list.

Add ‘Pin It’ Button To Images In Your WordPress Blog

Without using Plugin: Add this code to your theme’s footer.php file before the </body> tag. Get Pinterest’s javascript at Goodies.

<script type=”text/javascript” src=”//assets.pinterest.com/js/pinit.js”></script>

Add this code to your theme’s single.php file wherever you want Pin It button to be visible:

<a href=”http://pinterest.com/pin/create/button/?url=<?php the_permalink(); ?>&media=<?php echo catch_that_image() ?>&description=<?php the_title(); ?>” class=”pin-it-button” count-layout=”horizontal”><img border=”0″ src=”//assets.pinterest.com/images/PinExt.png” title=”Pin It” /></a>

Add this code in your theme’s functions.php file:

function catch_that_image() {
global $post, $posts;
$first_img = ”;
ob_start();
ob_end_clean();
$output = preg_match_all(‘/<img.+src=[‘”]([^'”]+)[‘”].*>/i’, $post->post_content, $matches);
$first_img = $matches [1] [0];
if(empty($first_img)){ //Defines a default image
$first_img = “/images/default.jpg”;
}
return $first_img;
}

Or You may use a WordPress plugin and that would be more easy for you. Use this plugin Pinterest Pin It Button For Images. Either search it on WP Plugin search or click the link and upload the folder to the /wp-content/plugins/ directory, Activate from ‘Plugins’ menu and Enjoy.

Hide Post Images Without Removing Thumbnail Image In Category Archive

Given code hides post images but keeps thumbnail image in Category archive and homepage. Add following code block in your theme’s function.php:

function wpi_image_content_filter($content){

if (is_home() || is_front_page() || is_category()){
$content = preg_replace(“/<img[^>]+>/i”, “”, $content);
}

return $content;
}
add_filter(‘the_content’,’wpi_image_content_filter’,11);

WordPress’s VIP Team Launched New Plugin That Controls Bulk User Management On Multisite Network

VIP team launched new plugin which is capable enough to control and manage all users across many sites in a multisite network install. It can check all network users and their roles on different sites sites and helps you to edit users, user roles on different sites at a time without visiting their dashboard from All Sites Super Admin Menu.