Category Archives: wordpress hacks

Select default image size for gallery in wordpress

In wordpress image gallery, it uses the full image size by default for every gallery but you can select or set the another image size. Using following code you can set the default image size for your wordpress image gallery.

just copy and paste following code to your functions.php file:

<!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php
 remove_shortcode('gallery');
 add_shortcode('gallery', 'custom_size_gallery');

 function custom_size_gallery($attr) {
      $attr['size'] = 'medium';
      return gallery_shortcode($attr);
 }
?>
wordpress-footer-jquery

Load the jQuery in the wordpress footer

Many latest framework are loading all javascript in footer. They reduse the page loading the errors. But wordpress load the jquery and other javascript in header.
But using following code you can load your javascripts and jquery in footer area. You just need to copy and paste the following code into the functions.php file.

function wpai_add_jquery_in_footer( &$scripts) {
	if ( ! is_admin() )
		$scripts->add_data( 'jquery', 'group', 1 );
}
add_action( 'wp_default_scripts', 'wpai_add_jquery_in_footer' );

After doing the this you js scripts will load in footer. This js will load in footer for only normal users, Not for admin user.

wordpress-footer-jquery

 

How to count post views without wordpress plugin

There are many wordpress plugin which will give you the post views using custom tables. But here using following code you can track the post views of your wp site.

Using external plugin, you can get the views and report but it will add more sql quries and extra load to your site. So I suggest use following code for getting the post views.

You just need to copy and paste the following code into functions.php file first.


function getPostViews($postID){
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
        return "0 View";
    }
    return $count.' Views';
}
function setPostViews($postID) {
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        $count = 0;
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
    }else{
        $count++;
        update_post_meta($postID, $count_key, $count);
    }
}
// Remove issues with prefetching adding extra views
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);

After adding the above code for tracking the post views you need to add following code into single.php file. Note: add following code inside the wp post loop.

<?php
          setPostViews(get_the_ID());
?>

For showing the post views you need to put following code in single.php file. Note: add following code inside the wp post loop.

<?php
          echo getPostViews(get_the_ID());
?>

Ref taken from: http://wpsnipp.com/index.php/functions-php/track-post-views-without-a-plugin-using-post-meta/

Remove short words from the wordpress permalink URL

WordPress itself creates the permalink. WordPress permalinks are very important for SEO purpose. Short words are sometimes are not good for seo so you might need to remove the short words from permalink. Using the following code you can remove the short words from wordpress permalink URL.

You just need to add the following code functions.php file.

add_filter('sanitize_title', 'remove_short_words');
function remove_short_words($slug) {
 if (!is_admin()) return $slug;
 $slug = explode('-', $slug);
 foreach ($slug as $k => $word) {
 if (strlen($word) < 3) {
 unset($slug[$k]);
 }
 }
 return implode('-', $slug);
}

 

Source Link :wpsnipp.com

 

Share social media Bookmark Icons in WordPress theme without plugins-Javascript

Share social media Bookmark Icons in WordPress theme without plugins or Javascript

I used many types for social bookmark plugins for sharing the content. I tried a some best services like those provided by AddThis and ShareThis. All the scripts and social media bookmarks are adding the some extra javascript code to website. Some social media buttons will add the swf/ flash code to website which to too heavy. So Adding the multiple javascript files for each social media bookmark is not a good solution.

My suggestion is, use anchor links with parameters added to the href attribute (which is basically what you get anyway once those JavaScripts have done their work).

Following code are important in wordpress for sharing the content to social media sites:
<?php the_title(); ?> // It will return the Post/Article Title
<?php the_permalink(); ?> // It will return the Post/Article Permalink or URL
<?php the_excerpt(); ?> // It will return the Post/Article Description
<?php the_post_thumbnail(); ?> // It will return the Post/Article Thumbnail Image

Facebook Share

<a href="http://www.facebook.com/sharer.php?s=100&amp;p[title]=<?php the_title(); ?>&amp;p[summary]=<?php the_excerpt(); ?>&amp;p[url]=<?php the_permalink(); ?>&amp;&p[images][0]=<?php the_post_thumbnail(); ?>"><img src="http://images.wordpressapi.com/facebook_32.png" title="Facebook" alt="Facebook" /></a>

Twitter Share

<a href="https://twitter.com/share" class="twitter-share-button" data-text="some description goes here.some description goes here.some description goes here.some description goes here.some description goes here." data-url="http://wordpressapi.com/create-install-script-drupal-module-create-tables/" data-related="anywhere:The Javascript API" data-via="purab">Tweet</a>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>

Google Plus

<a href="https://plus.google.com/share?url=http://54.214.47.229/test2.html?desc=foobar" onclick="javascript:window.open(this.href,
 '', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=600,width=600');return false;"><img
 src="https://www.gstatic.com/images/icons/gplus-64.png" alt="Share on Google+"/></a>

Linked In Share

<a rel="nofollow" target="_blank" href="http://www.linkedin.com/shareArticle?mini=true&amp;url=<?php the_permalink() ?>&amp;title=<?php echo urlencode(the_title('','', false)) ?>&amp;source=<?php bloginfo('pingback_url'); ?>&amp;ary=<?php the_excerpt(); ?>" title="LinkedIn">
<img src="http://images.wordpressapi.com/linkedin_32.png" title="LinkedIn" alt="LinkedIn" /></a>

Delicious Bookmark

<a rel="nofollow" target="_blank" href="http://del.icio.us/post?url=<?php the_permalink() ?>&amp;title=<?php echo urlencode(the_title('','', false)) ?>" title="del.icio.us"><img src="http://images.wordpressapi.com/delicious_32.png" title="del.icio.us" alt="del.icio.us" /></a>

Stumbleupon Share

<a rel="nofollow" target="_blank" href="http://www.stumbleupon.com/submit?url=<?php the_permalink() ?>&amp;title=<?php echo urlencode(the_title('','', false)) ?>" title="StumbleUpon"><img src="http://images.wordpressapi.com/stumbleupon_32.png" title="StumbleUpon" alt="StumbleUpon" /></a>

Digg Share

<a rel="nofollow" target="_blank" href="http://digg.com/submit?phase=2&amp;url=<?php the_permalink() ?>&amp;title=<?php echo urlencode(the_title('','', false)) ?>" title="Digg"><img src="http://images.wordpressapi.com/digg_32.png" title="Digg" alt="Digg" /></a>

Pin It

<a href="//www.pinterest.com/pin/create/button/?url=http%3A%2F%2Fwww.flickr.com%2Fphotos%2Fkentbrew%2F6851755809%2F&media=http%3A%2F%2Ffarm8.staticflickr.com%2F7027%2F6851755809_df5b2051c9_z.jpg&description=Next%20stop%3A%20Pinterest" data-pin-do="buttonPin" data-pin-config="above"><img src="//assets.pinterest.com/images/pidgets/pinit_fg_en_rect_gray_20.png" /></a>

Mixx Share

<a rel="nofollow" target="_blank" href="http://www.mixx.com/submit?page_url=<?php the_permalink() ?>&amp;title=<?php echo urlencode(the_title('','', false)) ?>" title="Mixx"><img src="http://images.wordpressapi.com/mixx_32.png" title="Mixx" alt="Mixx" />

Reddit Share

<a rel="nofollow" target="_blank" href="http://reddit.com/submit?url=<?php the_permalink() ?>&amp;title=<?php echo urlencode(the_title('','', false)) ?>" title="Reddit"><img src="http://images.wordpressapi.com/reddit_32.png" title="Reddit" alt="Reddit" /></a>

Mail to

<a rel="nofollow" target="_blank" href="mailto:?subject=<?php echo urlencode(the_title('','', false)) ?>&amp;body=<?php the_permalink() ?>" title="E-mail this story to a friend!">
<img src="http://images.wordpressapi.com/email_32.png" title="E-mail this story to a friend!" alt="E-mail this story to a friend!" /></a>

RSS feed

<a rel="nofollow" target="_blank" href="http://wordpressapi.com/feed" title="RSS">
<img src="http://images.wordpressapi.com/rss_32.png" title="RSS" alt="RSS" /></a>

I used following URL as reference.
How to Add the social Bookmark Icons in WordPress theme without wordpress plugins

Delete users by date wordpress Mysql

Delete users by date wordpress Mysql

Some days before I enabled the my site registration as subscribers. In last seven days there are four thousand subscribers has been registered on my site. After reviewing the users and there server request logs I got to know about that was spam users which was registered to my site. WordPress is always easy target for spammers and hackers. I tried to use some code for remove the wordpress users but then later I used following Mysql commands for removing he wordpress users.

When you create or register the user in wordpress that time some tables are filled by wordpress. Some entries will insert into wp_usermeta table and wp_users table. If you want to delete the bulk users then you need to delete user entries from wp_usermeta table first. You can use following SQL command for deleting the user meta entries from wp_usermeta table.


DELETE wp_usermeta FROM wp_usermeta, wp_users WHERE wp_users.user_registered > '2013-11' AND wp_users.ID = wp_usermeta.user_id;

After deleting the user meta entries from table then remove users from wp_users table using following command


DELETE FROM `wp_users` WHERE `user_registered` > '2013-11';

Note: Before deleting the users from wordpress database, take the Database backup. Take the worpdress backup and restore on local box. Make sure your DB backup file is perfect.

Do not delete users from wp_users at the first. First delete user meta from wp_usermeta then execute the next command.

 

Remove category slug wordpress from URL

Remove category slug wordpress from URL

Wp Developers are always looking for remove the category or blog slug from wordpress URL.  We can very easily remove the blog word from wordpress blog URL. Many clients want to remove the category word from URL. There are some nice wordpress plugins which will help you to remove the category word from wordpress URL.

I am using wordpress for long time. When wordpress 3.0 launched with multisite functionality we started using wordpress 3.0. Removing blog and category slug is very important for many clients.

Earlier also I written following article. But unfortunatly following code is not working now in newer release of wordpress.

http://wordpressapi.com/remove-blog-word-permalink/

Following wordpress plugins are useful:

WP No Category Base

As the name suggests this plugin will completely remove the mandatory ‘Category Base’ from your category permalinks ( e.g. myblog.com/category/my-category/ to myblog.com/my-category/ ).

The plugin requires no setup or modifying core wordpress files and will not break any links. It will also take care of redirecting your old category links to the new ones.

Manual method

Just add following code into your .htaccess file which you find in wordpress root folder.
RewriteRule ^category/(.+)$ http://www.site.com/$1 [R=301,L]

My suggestion:

Do not use any plugin or code for removing the category slug. If your client forces you to remove the category word then use wordpress permalink setting section. You can change the category slug from there. Following Note is given by wordpress.

Remove category slug wordpress

Remove category slug wordpress

“If you like, you may enter custom structures for your category and tag URLs here. For example, using topics as your category base would make your category links like http://example.org/topics/uncategorized/. If you leave these blank the defaults will be used.”

You can keep any slug name instead of category word.

Block Proxy Servers to Access the Wordpress Site

Block Proxy Servers to Access the WordPress Site

WordPress is always very target for hackers. Hackers always use the proxy servers to access the wordpress websites. Hackers attack the wordpress sites using the proxy IP addresses and servers. Here Using small trick you can prevent the proxy server attack. This is very easy trick which can be used in every wordpress project. All proxy visits are not bad requests so First check the site access logs. If you are thinking Your site attacked by Hackers and proxy servers then only use the following code.

You just need to add following code into your .htaccess file. You can open your .htaccess apache configuration file from wordpress root folder.

# BLOCK PROXY VISITS
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP:VIA}                 !^$ [OR]
RewriteCond %{HTTP:FORWARDED}           !^$ [OR]
RewriteCond %{HTTP:USERAGENT_VIA}       !^$ [OR]
RewriteCond %{HTTP:X_FORWARDED_FOR}     !^$ [OR]
RewriteCond %{HTTP:PROXY_CONNECTION}    !^$ [OR]
RewriteCond %{HTTP:XPROXY_CONNECTION}   !^$ [OR]
RewriteCond %{HTTP:HTTP_PC_REMOTE_ADDR} !^$ [OR]
RewriteCond %{HTTP:HTTP_CLIENT_IP}      !^$
RewriteRule .* - [F]
</IfModule>

After adding above code you must check your wordpress site. Check the your every pages of your site. Check your sites pages on atleast on three PCs so you will sure about your site and above code.

Open your header.php file which you find in your wordpress theme folder.

Take backup header.php file first.

<?php if(@fsockopen($_SERVER['REMOTE_ADDR'], 80, $errstr, $errno, 1)) die("Proxy access not allowed"); ?>

Put above code in header.php file and check your site is working fine. There are so many articles written on same topic so you can check those also before making any changes.

Block Proxy Servers to Access the WordPress Site

Block Proxy Servers to Access the WordPress Site

wordpress change permalink day and name to post name without losing traffic

wordpress change permalink day and name to post name without losing traffic

Earlier I used date and name permalink structure for my blogs. Recently I got information about SEO URL tricks and I made decision to change permalink structure to Post name.

wordpress change permalink day and name to post name without losing traffic

wordpress change permalink day and name to post name without losing traffic

So I changed permalink from:

http://wordpressapi.com/%year%/%monthnum%/%day%/%postname%/

to:

http://wordpressapi.com/%postname%/

It was very simple to above changes through wp admin->settings->permalink.

But with this produced many issues. Like google search result will go to old site URLs and My blog links are posted on other multiple blogs. That links will be broken and redirect to 404. Also I interlinked my other posts with other posts. They will go to 404 page.

If your blog pages will not found then google site ranking will go down.

By default in wordpress links look like /index.php?p=1234 which are not very useful for visitors or search engines.

In this walkthrough we’ll guide you through changing that to something useful like /post-title/.

Here I will tell you without loosing trafic or SEO how can you change the wordpress permalink day and name to post name

First go to wp admin->settings->permalink section and change setting to post name

If you are using any caching plugin (w3-cache or wp super chache) then remove all cache from dashboard.

Last edit .htaccess file and put following code there. Change domain name to your site name.

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RedirectMatch permanent ^/[0-9]{4}/[0-9]{2}/[0-9]{2}/([a-z0-9-/]+) http://digcms.com/$1
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

When would you want to use the other permalink settings?

  • If you’re a news site, the Day and name or Month and name option lets visitors know when the content was published.
  • Google News requires you to have a minimum of 3 numbers in your article URLs. The date-based options achieve this.
  • If you have a lot of content on your website covering a wide range of topics, using categories in the URL can provide context. For example, /shows/big-bang-theory/ and /science/big-bang-theory/ are two very different things. This works best when you choose only 1 category for your posts.

Useful article

http://yoast.com/wordpress-seo-url-permalink/

how to change the author url in wordpress without wordpress plugin

how to change the author url in wordpress without wordpress plugin

There is default username in wordpress. admin is default username in wordpress but many times user want to use other wordpress user url for admin user. You can easily do this by using following wordpress hook.

You just need to place following code in functions.php file which is your theme folder.

add_action('init', 'change_wordpress_author_url');
function change_wordpress_author_url() {
global $wp_rewrite;
$author_slug = 'new_author_url';
$wp_rewrite->author_base = $author_slug;
}

You need to choose your author url. Just replace the “new_author_url” word and put in functions.php file.

how to change the author url in wordpress without wordpress plugin

how to change the author url in wordpress without wordpress plugin