{"id":301,"date":"2014-11-12T09:30:07","date_gmt":"2014-11-12T07:30:07","guid":{"rendered":"http:\/\/board.resabi.com\/?p=301"},"modified":"2014-11-12T09:30:07","modified_gmt":"2014-11-12T07:30:07","slug":"useful-wordpress-tricks-hacks-snippets","status":"publish","type":"post","link":"https:\/\/resabi.com\/board\/2014\/11\/12\/useful-wordpress-tricks-hacks-snippets\/","title":{"rendered":"6 useful WordPress tricks, hacks &#038; snippets"},"content":{"rendered":"<p>What you can do with <a title=\"10 Best Professional WordPress Themes for Magazines and News Portals\" href=\"http:\/\/board.resabi.com\/category\/wordpress\" target=\"_blank\">WordPress<\/a> is limited only by your imagination and your ability to write or find the right piece of code. Today I&#8217;d like to share with you a couple of very useful snippets and tricks that can help you create custom functionality for your WordPress based website.<\/p>\n<h2>Insert ads between posts<\/h2>\n<p>You&#8217;ve probably seen some websites place ads after, say, the 3rd post. Here&#8217;s how you can do it with just two lines of code.<\/p>\n<p>Find the following line of code in your loop (blog posts, category, tag&#8230;)<\/p>\n<pre class=\"lang:default decode:true\">&lt;?php endwhile; ?&gt;<\/pre>\n<p>Just <strong>before<\/strong> this line, place the following code:<\/p>\n<pre class=\"lang:default decode:true\">&lt;?php if(!$show_ads){ ?&gt;\nInsert Code Here\n&lt;?php $show_ads = 3; } ?&gt;<\/pre>\n<p class=\"lang:default decode:true\">Replace <strong>Insert Code Here<\/strong> with your ad code or whatever you wish to appear after the 3rd post.<\/p>\n<p class=\"lang:default decode:true\">Of course, if you prefer to put it after a 5th post or other, just change the number after <strong>$show_ads<\/strong>.<\/p>\n<p class=\"lang:default decode:true\">Via <a href=\"http:\/\/wphacks.com\/how-to-insert-ads-only-after-the-first-post\/\" target=\"_blank\">WPHacks<\/a><\/p>\n<p class=\"lang:default decode:true\" style=\"text-align: center;\">%CODE1%<\/p>\n<h2>Limit WP post revisions easily<\/h2>\n<p>Whenever you save a change in a WordPress post or page, the system saves a version of that post. This can easily bring a lot of junk in the database, especially if you tend to make many corrections or save frequently as you prepare a post.<\/p>\n<p>There are several plugins for removing revisions from the database, but there&#8217;s also a very easy way to limit the number of saved revisions for each post\/page.<\/p>\n<p>1 line of wp_config code is all you need:<\/p>\n<pre class=\"lang:php decode:true \">define( 'WP_POST_REVISIONS', 3 );<\/pre>\n<p>WP_POST_REVISIONS:<\/p>\n<ul>\n<li>true (default), -1: store every revision<\/li>\n<li>false, 0: do not store any revisions (except the one autosave per post)<\/li>\n<li>(int) &gt; 0: store that many revisions (+1 autosave) per post. Old revisions are automatically deleted.<\/li>\n<\/ul>\n<p>More info at <a href=\"http:\/\/codex.wordpress.org\/Revisions\" target=\"_blank\">WP codex<\/a><\/p>\n<h2>Custom template for each category<\/h2>\n<p>WordPress Category templates are very easy to use, view the <a title=\"WordPress Codex Category Templates\" href=\"http:\/\/codex.wordpress.org\/Category_Templates\" target=\"_blank\">WordPress Codex \u2013 Category Templates<\/a> and you will see that hierarchy for a category templates is:<\/p>\n<ul>\n<li>1. category-6.php<\/li>\n<li>2. category.php<\/li>\n<li>3. archive.php<\/li>\n<li>4. index.php<\/li>\n<\/ul>\n<p>This basically just means that WordPress will first look for a template labeled category-6, if there is none, it will then look for category.php and so forth all the way down to index.php.<\/p>\n<p>More info at <a href=\"http:\/\/gabediaz.com\/blog\/custom-category-page-and-post-templates-for-wordpress\/\" target=\"_blank\">GabeDiaz<\/a>, via <a href=\"http:\/\/resapiens.com\/wordpress-custom-template-for-each-category\/\" target=\"_blank\">reSapiens<\/a><\/p>\n<h2>Change the page title attribute<\/h2>\n<p>If you want to change your pages title template, and don&#8217;t want to use a <a title=\"12 Essential free WordPress plugins that will make your life easier\" href=\"http:\/\/board.resabi.com\/essential-wordpress-plugins-will-make-life-lot-easier\/\" target=\"_blank\">SEO plugin<\/a> for that, here&#8217;s a simple code for you. It takes the title of the blog for the home page, or the category, post or page title respectively.<\/p>\n<pre class=\"lang:default decode:true\">&lt;title&gt;&lt;?php if (is_home () ) { bloginfo(\u2018name\u2019); }\nelseif ( is_category() ) { single_cat_title(); echo \u2018 - \u2018 ; bloginfo(\u2018name\u2019); }\nelseif (is_single() ) { single_post_title();}\nelseif (is_page() ) { single_post_title();}\nelse { wp_title(\u2018\u2019,true); } ?&gt;&lt;\/title&gt;<\/pre>\n<p><code>Via <a href=\"http:\/\/wphacks.com\/how-to-optimize-your-wordpress-title\/\" target=\"_blank\">WPHacks<\/a><\/code><\/p>\n<h2>Change the name\/path of the wp-content folder<\/h2>\n<p>If you&#8217;ve ever wanted to rename the wp-content folder (eg. http:\/\/mysite.com\/stuff), here&#8217;s a relatively easy way to do it. You might have wanted to rename it so it&#8217;s harder to tell the site is based on WordPress, or maybe for security reasons. Now you can with just a few lines of code in your <strong>wp-config<\/strong> file. I&#8217;ve combined two codes to get the desired effect, one from\u00a0<a href=\"http:\/\/www.hongkiat.com\/blog\/renaming-wordpress-wp-content-folder\/\" target=\"_blank\">Hongkiat<\/a> and one from <a href=\"http:\/\/www.mcloud.rs\/blog\/wordpress-male-tajne-wp-config-php\/\" target=\"_blank\">MCloud<\/a>. The one from Hongkiat didn&#8217;t work on localhost.<\/p>\n<pre class=\"lang:php decode:true\">define('WP_HOME', 'http:\/\/board.resabi.com\/');\ndefine('WP_SITEURL', 'http:\/\/board.resabi.com\/');\n\ndefine ('WP_CONTENT_FOLDERNAME', 'base');\ndefine ('WP_CONTENT_DIR', ABSPATH . WP_CONTENT_FOLDERNAME) ;\ndefine('WP_CONTENT_URL', WP_SITEURL . WP_CONTENT_FOLDERNAME);<\/pre>\n<p class=\"lang:php decode:true\">Don&#8217;t forget to replace <strong>http:\/\/board.resabi.com\/<\/strong> with your site&#8217;s url and <strong>base<\/strong> with the name\/path you want for your folder.<\/p>\n<h2 class=\"lang:php decode:true\">Share buttons without a plugin<\/h2>\n<p>These are the essential share buttons (Facebook, Twitter and Google+), but you can easily add others following the instructions on their websites. Using these can help lower the time needed for the page to load.<\/p>\n<pre class=\"lang:php decode:true\">&lt;style type=\"text\/css\"&gt;\n.share {padding:20px; display:block; font-size:18px; text-align:center}\n.share a {padding:6px 12px; margin:0 3px; color:#fff; border-radius:3px}\n&lt;\/style&gt;\n&lt;div class=\"share\"&gt;\n\t\t\t\t&lt;a style=\"background:#5977B2\" href=\"http:\/\/www.facebook.com\/share.php?u=&lt;?php echo esc_url( get_permalink() ); ?&gt;\" onclick=\"Popup=window.open( this.href, 'Popup', 'toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=580,height=350, top=200, left=50');return false;\"&gt;Share on Facebook&lt;\/a&gt;\n\n\t\t\t\t&lt;a style=\"background:#00ACEE\" href=\"https:\/\/twitter.com\/share?url=&lt;?php echo esc_url( get_permalink() ); ?&gt;&amp;text=&lt;?php echo get_the_title(); ?&gt;\" onclick=\"Popup=window.open( this.href, 'Popup', 'toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=580,height=350, top=200, left=50');return false;\"&gt;Tweet&lt;\/a&gt;\n\n\t\t\t\t&lt;a style=\"background:#D14128\" href=\"https:\/\/plus.google.com\/share?url=&lt;?php echo esc_url( get_permalink() ); ?&gt;\"  onclick=\"Popup=window.open( this.href, 'Popup', 'toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=580,height=350, top=200, left=50');return false;\"&gt;Google +1&lt;\/a&gt;\n\t\t\t&lt;\/div&gt;<\/pre>\n<p>Got any cool snippet you would like to share?<\/p>\n<p>Please add it via comments.<\/p>\n<p>Photo Credit: <a href=\"https:\/\/www.flickr.com\/photos\/33859208@N00\/5036291025\/\">bobbigmac<\/a> via <a href=\"http:\/\/compfight.com\">Compfight<\/a> <a href=\"https:\/\/creativecommons.org\/licenses\/by\/2.0\/\">cc<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>What you can do with WordPress is limited only by your imagination and your ability to write or find the right piece of code. Today I&#8217;d like to share with you a couple of very useful snippets and tricks that can help you create custom functionality for your WordPress based website. Insert ads between posts [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":417,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"pagelayer_contact_templates":[],"_pagelayer_content":"","footnotes":""},"categories":[12],"tags":[28,42],"class_list":["post-301","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-wordpress","tag-snippets","tag-wp-tricks"],"_links":{"self":[{"href":"https:\/\/resabi.com\/board\/wp-json\/wp\/v2\/posts\/301","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/resabi.com\/board\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/resabi.com\/board\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/resabi.com\/board\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/resabi.com\/board\/wp-json\/wp\/v2\/comments?post=301"}],"version-history":[{"count":0,"href":"https:\/\/resabi.com\/board\/wp-json\/wp\/v2\/posts\/301\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/resabi.com\/board\/wp-json\/wp\/v2\/media\/417"}],"wp:attachment":[{"href":"https:\/\/resabi.com\/board\/wp-json\/wp\/v2\/media?parent=301"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/resabi.com\/board\/wp-json\/wp\/v2\/categories?post=301"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/resabi.com\/board\/wp-json\/wp\/v2\/tags?post=301"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}