Showing posts with label Tutorials. Show all posts
Showing posts with label Tutorials. Show all posts

Thursday 27 February 2014

Adding Floating Contact Form To Blogger



Just a quick news before I start the stuff. Google Code has suspended my code account & all repositories I hosted there, so all my code that I use in this website & in tutorials in no longer controlled by me. That's it!

If you're not satisfied with adding Blogger's contact form widget to a specific page of your blog, you can also use it as a floating widget.

BloggerItems has created a cool floating contact form widget for Blogger. First, add Blogger's official contact form widget to your blog.

  • Go To Blogger > Template > Edit HTML
  • Search and paste this code just about ]]></b:skin>:
.floating-ct-body {
 height: 285px;
 background: white;
 border: 1px solid #09f;
 padding: 10px;
 display: none;
}
.floating-ct-head {
 text-align: right;
}
.floating-ct-body .ContactForm {
 margin: 0;
 display: block!important;
}
Then paste the below code before </body> tag in your template:

<script type='text/javascript'>
//<![CDATA[
/*Floating Contact Form by BloggerItems.com*/
$('body').append('<div class="floating-ct"><div class="floating-ct-head"><a href="#no-move">Contact</a></div><div class="floating-ct-body"></div></div>');
$('.ContactForm').appendTo('.floating-ct-body');
var slide_up_ct = false;
$('.floating-ct-head a').click(function(){
 if (!slide_up_ct) {
 slide_up_ct = true;
 $('.floating-ct-body').slideDown();
 } else {
 slide_up_ct = false;
 $('.floating-ct-body').slideUp();
 }
});
//]]>
</script>

That's IT!

Tuesday 31 December 2013

Amazing Post Navigation For Blogger


It's already 2014 here in India, so I'd like to wish you all a very happy New Year. Stay safe and don't drink to much ;)

It's still New Year's Eve in most of part of the globe, so let me end the year with a final post on this blog. And this tutorial comes from the Blogger King MBT.

It's a new stylish jQuery post navigation for your blog. It'll appear on in your Blogger posts and it'll definitely catch the attention of your readers. The pagination buttons make no sense when placed at the bottom of comments container.

Add This To Blogger:

First. we will add CSS and some necessary script to our Blogger template.

  • Go To Blogger > Template
  • Click "Edit HTML"
  • Search and add following CSS just above ]]></b:skin>

    /*################MBT Pager ##########################*/

    .mbt-pager { border-top: 2px dashed #ddd; border-bottom: 2px dashed #ddd;  margin-bottom: 10px;   overflow:hidden; padding:0px;}
    .mbt-pager li.next { float: right; padding:0px; background:none; margin:0px;}
    .mbt-pager li.next a { padding-left: 24px; }

    .mbt-pager li.previous { margin:0px -2px 0px 0px; float: left;  border-right:1px solid #ddd; padding:0px; background:none;
    }
    .mbt-pager li.previous a { padding-right: 24px;  }

    .mbt-pager li.next:hover, .mbt-pager li.previous:hover  {background:#333333; }

    .mbt-pager li { width: 50%; display: inline; float: left; text-align: center; }
    .mbt-pager li a { position: relative; min-height: 77px; display: block; padding: 15px 46px 15px; outline:none; text-decoration:none;}

    .mbt-pager li i { color: #ccc; font-size: 18px; }

    .mbt-pager li a strong { display: block; font-size: 20px; color: #ccc; letter-spacing: 0.5px; font-weight: bold; text-transform: uppercase; font-family:oswald, sans-serif, arial; margin-bottom:10px;}

    .mbt-pager li a span { font-size: 15px; color: #666;  font-family:oswald,Helvetica, arial; margin:0px;}
    .mbt-pager li a:hover span,
    .mbt-pager li a:hover i { color: #ffffff; }
    .mbt-pager li.previous i { float:left; margin-top:15%; margin-left:5%; }
    .mbt-pager li.next i { float: right;
    margin-top: 15%;
    margin-right: 5%; }

    .mbt-pager li.next i, .mbt-pager li.previous i ,
    .mbt-pager li.next,  .mbt-pager li.previous{
    -webkit-transition-property: background color; -webkit-transition-duration: 0.4s; -webkit-transition-timing-function: ease-out;
    -moz-transition-property: background color; -moz-transition-duration: 0.4s; -moz-transition-timing-function: ease-out;
    -o-transition-property: background color; -o-transition-duration: 0.4s; -o-transition-timing-function: ease-out;
    transition-property: background color; transition-duration: 0.4s; transition-timing-function: ease-out; }

    .fa-chevron-right {padding-right:0px;}

  • Now, we will add fonts from Google Fonts & jQuery support.  Search for <head> and just below it paste the following code:

<script src="http://widcraft.googlecode.com/svn/jquery.js" type="text/javascript"></script>
<link href='http://fonts.googleapis.com/css?family=Oswald' rel='stylesheet' type='text/css'/>

  • Now, it's for the main event. We will add our navigator to the bottom of your post. Search for data:post.body and paste following below it:

    <b:if cond='data:blog.pageType == &quot;item&quot;'>

    <b:if cond='data:blog.pageType != &quot;static_page&quot;'>

    <ul class='mbt-pager'>

            <li class='next'>
    <b:if cond='data:newerPageUrl'>
    <i class='fa fa-chevron-right'/><a class='newer-link' expr:href='data:newerPageUrl' expr:id='data:widget.instanceId + &quot;_blog-pager-newer-link&quot;' rel='next'/>
    <b:else/>
    <i class='fa fa-chevron-right'/><a rel='next'><strong>Next</strong> <span>You are viewing Most Recent Post</span></a>
    </b:if>
    </li>

        <li class='previous'>
    <b:if cond='data:olderPageUrl'>
    <i class='fa fa-chevron-left'/><a class='older-link' expr:href='data:olderPageUrl' expr:id='data:widget.instanceId + &quot;_blog-pager-older-link&quot;' rel='previous'/>
    <b:else/>
    <i class='fa fa-chevron-left'/><a rel='previous'><strong>Previous</strong> <span>You are viewing Last Post</span></a>
    </b:if>
    </li>

        </ul>


    <script type='text/javascript'>
    //<![CDATA[
    (function($){  
        var newerLink = $('a.newer-link');
        var olderLink = $('a.older-link');
        $.get(newerLink.attr('href'), function (data) {
         newerLink.html('<strong>Next</strong> <span>'+$(data).find('.post h3.post-title').text()+'</span>');  
        },"html");
        $.get(olderLink.attr('href'), function (data2) {
         olderLink.html('<strong>Previous</strong> <span>'+$(data2).find('.post h3.post-title').text()+'</span>');  
        },"html");
    })(jQuery);
    //]]>
    </script>

          </b:if></b:if>

Save your template, and that's it for this year!

Huge Announcement:

In 2014, BWidgets.com will only cover stuff related to Blogger & Google. All WordPress and other stuff will go into your NEW WEBSITE (details coming soon). Our new site will also feature a new Google PageRank checker among other cool tools. So stay tuned!

Friday 13 December 2013

Add Falling Snow To Blogger

Single bells single bells single all the way! It's HOLIDAY SEASON, and we're back with another great script. It's a great script and will work everywhere, not just Blogger.

This great script comes from schillmania.com. It's just a single .js file, we don't need any jquery support for this script. Just add this script to your Blogger blog and we're done. For newbies, here's how to add:

  • Blogger > Template > Edit HTML> Add following script just above </head> tag:

<script src="https://widcraft.googlecode.com/svn/snowstorm.js"></script>

Want to customize snow fall effects? See Customizing Snowstorm for more.

Wednesday 11 December 2013

Take Screenshot In Windows Without Any Software

Today I'm sharing a very basic, but a very useful trick with you all. Most of us bloggers have a screenshot software on their devices, but they're just a waste of space as you can easily shot your screen with one click! Here's how to do it:


Press PrntScr key and Windows captures the entire screen and copies it to its clipboard. Now start “Paint” or any other software where you can paste an image. Hold CTRL and press V and Windows pastes the screenshot, which is in the clipboard, into your document. Now just save your image!

Tuesday 3 December 2013

How To Enable Jetpack Stats On MobilePress

Almost every WordPress admins uses WordPress' Jetpack plugin on their site to connect and use WordPress.com feature on their self-hosted WordPress. There are a lot of great modules in the Jetpack plugin, but Jetpack Stats module is my personal favorite.

MobilePress is also a well known plugin to create a mobile friendly version of your WordPress. It allows custom themes and a few other mobile friendly settings. But Jetpack's stats module only tracks the desktop version of your site, not the MobilePress version.

Don't worry about this, as I have found a way to enable Jetpack's stats module on our MobilePress. Here is how:

  • First of all, enable Jetpack stats module on your WordPress. Now visit your WordPress website as a guest (without signing in).
  • Now right click & click on View Page Source to view the source code of your current page, and search stats.wordpress.com in the source code page. You'll find a code similar to this:

<script src="http://stats.wordpress.com/e-201349.js" type="text/javascript"></script>
<script type="text/javascript">
st_go({v:'ext',j:'1:2.6',blog:'YOURUNIQUEBLOGID',post:'0',tz:'-5'});
var load_cmc = function(){linktracker_init(YOURUNIQUEBLOGID,0,2);};
if ( typeof addLoadEvent != 'undefined' ) addLoadEvent(load_cmc);
else load_cmc();
</script>

  • Copy your unique blog id from your unique Jetpack stats snippet and place it in the following code:

<script src="http://stats.wordpress.com/e-201341.js" type="text/javascript"></script><script type="text/javascript">
st_go({v:'ext',j:'1:2.5',blog:'YOURUNIQUEBLOGID',post:'<?php the_ID(); ?> ',tz:'-4'});
var load_cmc = function(){linktracker_init(YOURUNIQUEBLOGID,0,2);};
if ( typeof addLoadEvent != 'undefined' ) addLoadEvent(load_cmc);
else load_cmc();
</script>

  • Now paste the above code in the footer.php (webroot / wp-content / plugins / mobilepress / themes / default / footer.php) file of your current MobilePress theme. That's IT!!

As you can see, there's only one difference in the above two codes. In the second code, I have replaced the current post id with <?php the_ID(); ?>, otherwise your Jetpack wouldn't count views for specific page, and all your visits would be counted as Homepage views or of a specific post.

Saturday 30 November 2013

Set WordPress Permalinks Settings From functions.php

Permalinks are the permanent URLs to your individual WordPress or blog posts, as well as pages and other lists of blog postings. It's very important to have an easy and beautiful permalinks structure for your site, as it's a part of optimizing your site for SEO.

The default WordPress permalinks structure looks something like this:

http://www.whatever.com/?p=316

And I hate this as much as you all do. However, you can change the default structure of your blog permalinks in the settings of your WordPress blog. But we can also change the default permalinks structure from our functions.php file.

It's pretty easy, just add following snippet to your functions.php file:

<?php
// set permalink
function set_permalink(){
    global $wp_rewrite;
    $wp_rewrite->set_permalink_structure('/%year%/%monthnum%/%postname%/');
}
add_action('init', 'set_permalink');
?>

Use this snippet to change the permalink settings from your functions.php file instead of in the admin area. You can replace the red area in above code with following structure codes:

%year%
    The year of the post, four digits, for example 2004

 %monthnum%
    Month of the year, for example 05

 %day%
    Day of the month, for example 28

 %hour%
    Hour of the day, for example 15

 %minute%
    Minute of the hour, for example 43

 %second%
    Second of the minute, for example 33

 %post_id%
    The unique ID # of the post, for example 423

 %postname%
    A sanitized version of the title of the post (post slug field on Edit Post/Page panel). So “This Is A Great Post!” becomes this-is-a-great-post in the URI.

 %category%
    A sanitized version of the category name (category slug field on New/Edit Category panel). Nested sub-categories appear as nested directories in the URI.

 %author%
    A sanitized version of the author name.

Thursday 28 November 2013

Disable Auto Updates In WordPress

WordPress 3.7 came out with a new automatic background update feature in an effort to promote better security. By the default, only minor releases and translation file updates are enable. But we could manually disable or enable anything we want.

Just put following codes to your wp-config.php file to disable/enable the updates:

To specifically enable development (nightly) updates, use the following:

add_filter( 'allow_dev_auto_core_updates', '__return_true' );

To specifically disable minor updates, use the following:

add_filter( 'allow_minor_auto_core_updates', '__return_false' );

To specifically enable major updates, use the following:

add_filter( 'allow_major_auto_core_updates', '__return_true' );

Plugins, themes and translation file:

Automatic plugin and theme updates are disabled by default.

To enable automatic updates for plugins, use the following:

add_filter( 'auto_update_plugin', '__return_true' );

To enable automatic updates for themes, use the following:

add_filter( 'auto_update_theme', '__return_true' );

Automatic translation file updates are already enabled by default, the same as minor core updates.  To disable translation file updates, use the following:

add_filter( 'auto_update_translation', '__return_false' );

Core Updates:

To disable all core-type updates only, use the following:

add_filter( 'auto_update_core', '__return_false' );

All Updates

To completely disable all types of automatic updates, core or otherwise, add the following to your wp-config.php file:

define( 'AUTOMATIC_UPDATER_DISABLED', true );

You can also disable all automatic updates using the following filter:

add_filter( 'automatic_updater_disabled', '__return_true' );

Hope this would help you. Source: WordPress Codex

Saturday 23 November 2013

Add Extra CSS In WordPress Posts With Custom Fields

I posted last couple of WordPress articles on BWidgets about the amazing power of custom fields. This is article is also about another great use of custom fields.

Sometimes, a specific post needs to be more styles with some custom CSS codes. We all know that we can directly write our CSS code in post editor, but that's not very clean way to do this. So, we're going to do this with custom fields.

This is clean, easy, and a bit fun as well. Yea, these things makes me feel like James Bond 007. Here we go:

First, we need to add following code to your WordPress theme's header.php file:

<?php if (is_single()) {
    $css = get_post_meta($post->ID, 'css', true);
    if (!empty($css)) { ?>
        <style type="text/css">
        <?php echo $css; ?>  
        </style>
    <?php }
} ?>

Save the file. Once saved, when you're writing a post which requires some custom extra CSS styling, you'll just have to put your custom CSS styles in a custom field named css.

Friday 22 November 2013

How To Get Custom Fields Outside The Loop In WordPress

Custom fields are one of the most useful and powerful feature in WordPress and are used on many WordPress-powered blogs. In this post, I'm going to show you how to get custom fields values outside the loop.

Thanks to Paul Whitehead for this trick. I want to share link to his website, but the website is dead right now.

Simply post following snippet anywhere in your WordPress site. Don’t forget to replace customField on line 4 by the name of the custom field you want to display.

<?php
global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, 'customField', true);
?>

Wednesday 20 November 2013

How To Get Value Of A Specific Custom Field In WordPress

WordPress allows us to add custom fields to a our posts, which adds extra information, which is known as meta-data. But we can do a lot more there custom fields. For example, I embedded a video in this post with the use of custom fields.

More of newbies gets confused when it comes to use custom fields. But they're not that complicated, at least the basic use of these fields.

Here's how you can get value of a specific custom field in your WordPress posts:

<?php echo get_post_meta($post->ID, 'mood', true); ?>

"mood" would be ID value of custom field. That was simple.

And here's how to display custom field only if exists:

<?php
    $url = get_post_meta($post->ID, 'snippet-reference-URL', true);

    if ($url) {
        echo "<p><a href='$url'>Reference URL</a></p>";
    }
?>

Sunday 17 November 2013

Using A Different Post Template For Posts Within A Specific Category

On Thursday I posted an article about using different post template for posts formats, but now it's time for doing the same thing with categories, instead of formats. You can use this trick in your WordPress to create different type of posts, such as portfolio items.

We discussed everything previously, so let's get into this trick:

Using A Different Post Template:

In this tutorial, we'll create a different post template for the cat-1 category. We'll use multiple single.php files to make it more easy and customizable.

Create a single.php file for your category. We're going to name it single-cat-1.php. You can also copy your single.php file to this template and make some changes that you want in the video template.

Upload single-cat-1.php to the root of your current theme. Same place where single.php file is located.

Okay, so now we'll tell WordPress to use single-cat-1.php template for the posts within video category. Add following to your theme's functions.php file:

add_action('template_include', 'load_single_template');
  function load_single_template($template) {
    $new_template = '';

    // single post template
    if( is_single() ) {
      global $post;

      // 'cat-1' is category slugs
      if( has_term('cat-1', 'category', $post) ) {
        // use template file single-template-cat-1.php
        $new_template = locate_template(array('single-cat-1.php' ));
      }

    }
    return ('' != $new_template) ? $new_template : $template;
  }

In above code, replace cat-1 is the slug of your category, and single-cat-1.php is your custom post template file. Save the file. That's it.

How To Log Out From New Facebook Messenger Android App


Facebook has released the latest update of its Messenger app for Android. This release covers some major changes in the app interface and features. You can also text your contracts from this app, which makes it replacement of texting.

But Facebook forgot to add something in this new upload. There's no option to Log Out of your account in this new update. Don't worry there's a solution of every problem.

Most of us used to log out of our Facebook accounts from Account settings menu, which will not work in this case. Here's how to log out from this app:

In Gingerbread: Click on Settings > Applications > Manage applications > Find and click on "Messenger" > Click on Clear Data > That's IT!

In ICS & JellyBean: Click on Settings > Apps > Find and click on "Messenger" > Click on Clear Data > That's IT!

Thursday 14 November 2013

Using A Different Post Template For Posts Formats In WordPress

Post Formats is a theme feature which was introduced with the WordPress 3.1 release. It gave us a new feature to style posts based on its format. If you ever used WordPress' default themes, then you might know about their different post templates based on the post format. So it's time for you to style your posts based on its format. Here's a demo of this trick from my own WordPress site:

This is a simple post.

This is a video post with a different template.

Add Post Format Support To Your WordPress Theme:

Before starting this tutorial, it's important for you to add support for post formats in your current WordPress theme. If this section is available in your post editor then you can skip this step:


If not, then follow this tutorial.

Using A Different Post Template:

In this tutorial, we'll create a different post template for the Video format. Most of developers uses a single post template file for all different format. But we'll use multiple single.php files to make it more easy and customizable.

Create a single.php file for your post format (we're using Video). We're going to name it single-video.php. You can also copy your single.php file to this template and make some changes that you want in the video template.

Upload single-video.php to the root of your current theme. Same place where single.php file is located.

Okay, so now we'll tell WordPress to use single-video.php template for the posts with video format. Add following to your theme's functions.php file:

add_action('template_include', 'load_single_template');
  function load_single_template($template) {
    $new_template = '';

    // single post template
    if( is_single() ) {
      global $post;

      // template for post with video format
      if ( has_post_format( 'video' )) {
        // use template file single-video.php for video format
        $new_template = locate_template(array('single-video.php' ));
      }

    }
    return ('' != $new_template) ? $new_template : $template;
  }

Save the file. That's it.

Now just publish a post with video format & see the magic. You can do the same with every post format. On a related note, I'll also share the same trick with categories, instead of formats.

Add Post Formats To WordPress Theme

It's school holiday for five days in a row, so as a part of the second day of this holiday, I was doing some random stuff around the internet, such as listing to new songs, reading about upcoming Next-Gen gaming consoles, browsing troll memes, blaming Justin Bieber, and more.

So in this busy day I took some time to edit my WordPress website for new feature. I did some thing new (at least for me) with some post formats, and I'm going to share it here on BWidgets. So as a part of that tutorial, here's the basic thing to learn before I post that article, which is adding post format support to your WordPress theme.

Post Formats is a theme feature introduced with the WordPress 3.1 release. It's piece of meta info that can be used by a theme to customize its post structure. If you ever used Tumblr, then you'll love this feature.

As of now, WordPress supports following post formats:

  • aside: Typically styled without a title. Similar to a Facebook note update.
  • gallery: An image gallery. This type of post will likely contain an image gallery using shortcode and plugins.
  • link: A link to an external webpage.
  • image: A single image. Yeah, just a single image.
  • quote: A quatation. My favorite quote is: Your arms are just too short to box with the God. - CM Punk to Dwayne "The Rock" Johnson
  • status: A short status update, similar to a tweet.
  • video: A single video or several videos. You can upload or embed a video to the post.
  • audio: An audeo file, likely if you run a Podcast.
  • chat: A chat transcript.

Add following snippet to your theme's functions.php file to add post format support to your post:

add_theme_support( 'post-formats', array( 'aside', 'chat','gallery','image','link', 'quote', 'status', 'video', 'audio' ) );

You can also add post format support to Page and custom post types:

// add post-formats to post_type 'page'
add_post_type_support( 'page', 'post-formats' );

// add post-formats to post_type 'my_custom_post_type'
add_post_type_support( 'my_custom_post_type', 'post-formats' );

Wednesday 13 November 2013

Remove Default Widget Bundle CSS From Blogger

If you're a Blogger template designer, then this article is very very helpful for your future designs. If you're a designer, then you probably know about two default Blogger stylesheets- widget_css_bundle.css and widget_css_2_bundle.css. Becaue they make it more diffecult for you to design your template.

Blogger automatically adds the following code in your Blogger template:

<link type='text/css' rel='stylesheet' href='//www.blogger.com/static/v1/widgets/1937454905-widget_css_bundle.css' />

<link type='text/css' rel='stylesheet' href='//www.blogger.com/static/v1/widgets/4219271310-widget_css_2_bundle.css' />

So it's time for your to remove there stylesheets from your Blogger template. If you don't know above there stylesheets, then don't follow this tutorial. Removing this code maybe can make your template destroyed, if you use official widgets from Blogger.

We are not really removing this code from our template, but this trick will make that code ignored by your browser. This tutorial is just converting that code to HTML comments, thanks to Damzaky for this trick.

  • Open Blogger > Template > Edit HTML, proceed, then CTRL+F this code: <b:skin><![CDATA[
  • If you have found that code, in below that code you may found your CSS, so just copy all the CSS to a notepad or anywhere because we need this code later in this tutorial.
  • Then the remaining code is  <b:skin><![CDATA[    ]]></b:skin>
  • Replace above code with following:

&lt;style type=&quot;text/css&quot;&gt;

&lt;!-- /*<b:skin><![CDATA[*/]]></b:skin>

  • Then post following code just above the </head> of your template:

<style type='text/css'>

YOUR CSS FROM NOTEPAD

</style>

  • In above code, replace the YOUR CSS FROM NOTEPAD from all CSS from your notepad that we copied earlier.
  • Save your template.

That's it.

Monday 11 November 2013

Change WordPress Login Logo Without Plugins

Above is a screen shot of the login page of my WordPress website. As you can see in above image, I'm using my website's logo in the login page instead of WordPress logo. And it looks pretty good.

If you're running a website with public registration or a client website, then you should replace the default logo with your own. There are lots of plugins to change the custom logo in some easy steps. However, we're going to do this without any plugin.

Add this snippet to the functions.php file of your active theme:

<?php
function custom_login_logo() {
    echo '<style type="text/css">
        h1 a { background-image:url('CUSTOM-LOGO-URL.png') !important; }
    </style>';
}
add_action('login_head', 'custom_login_logo');
?>

Replace the CUSTOM-LOGO-URL.png in above code with url of your custom login logo. That's it.

Sunday 10 November 2013

How To Set Minimum Word Count For WordPress Posts

If you're running a WordPress site with some co-authors then it's really important for you to focus on your content's quality. Quality content is the ultimate key to success in blogging. Most of bloggers write like a monkey in the very beginning; I was also in that league when I started this blog.

Since recent Google updates you can no longer post an article with some couple of words and expect the post to rank highly for the keywords in the title. Now you need more content in the entire post with a lot of different keywords.

With WordPress you can create a function to make sure that you have a minimum number of words in your post.

By adding following code to your functions.php file, you can check if the post is under 500 words and if it is you can end the publishing process so the post will not be published:

function minimum_number_words($content)
{
    global $post;
    $content = $post->post_content;
    if (str_word_count($content) < 500 )
    wp_die( __('The current post is below the minimum number of words, it must be over 500 words.') );
}

Friday 8 November 2013

How To Set Minimum Comment Length In WordPress

Most of us turn on comments in our sites to start a discussion and reach out more people. But some people comment just to get a link back to their own site. Their comments will consist of things like "nice post", "thanks" and all stuff.

If you're running a big website, then moderating these crappy comments will waste a lot of time. So how about setting a minimum character count length on your comments.

Add following snippet to your functions.php file:

add_filter( 'preprocess_comment', 'minimal_comment_length' );

function minimal_comment_length( $commentdata ) {
    $minimalCommentLength = 20;

    if ( strlen( trim( $commentdata['comment_content'] ) ) < $minimalCommentLength )
        {
        wp_die( 'All comments must be at least ' . $minimalCommentLength . ' characters long.' );
        }
    return $commentdata;
}

Save your file. That's it. You can also replace 20 in the above code with your custom value.

Tuesday 5 November 2013

Bulk Remove Featured Images From WordPress Posts

By default, there's no such option in WordPress to remove featured image from all posts at once. You can only remove the featured image by editing each post and removing it manually. It can take a lot of time. For example, it's almost impossible to manually remove featured images from my WWEFansNation.com with more than 4,000 posts. But that's all right cuz we're here to solve this problem.

WPBeginner found a great way to remove (unset) featured images from all posts in bulk. It's worth nothing that this code will only remove the featured image from the posts, and the image will not be deleted from your site.

Copy & paste following code in your theme's functions.php file:

global $wpdb;

$attachments = $wpdb->get_results( "
     SELECT *
     FROM $wpdb->postmeta
     WHERE meta_key = '_thumbnail_id'
" );

foreach ( $attachments as $attachment ) {
    wp_delete_attachment( $attachment->meta_value, true );
}

$wpdb->query( "
    DELETE FROM $wpdb->postmeta
    WHERE meta_key = '_thumbnail_id'
" );

Important: Please remove this code immediately after saving your functions.php file. You only need to run this code once, there's no need to keep this code in your theme.

Monday 4 November 2013

Automatically Set The Featured Image In WordPress

Almost every WordPress theme developer is using WordPress' featured post image option in their latest themes, and it's a very great option. You can pick any image for your article before publishing

But ofter we forget to pick a featured image for out posts. Also there's no way to pick a featured image in WordPress' smartphone application. So today we're going to automatically set the featured image.

This snippet will automatically set the first image of your post as the featured image of the post. Note, you can always go and set another image as the featured image if you want to.

Add following snippet to your functions.php file:

function wpforce_featured() {
          global $post;
          $already_has_thumb = has_post_thumbnail($post->ID);
              if (!$already_has_thumb)  {
              $attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );
                          if ($attached_image) {
                                foreach ($attached_image as $attachment_id => $attachment) {
                                set_post_thumbnail($post->ID, $attachment_id);
                                }
                           } else {
                                set_post_thumbnail($post->ID, '414');
                           }
                        }
      }  //end function
add_action('the_post', 'wpforce_featured');
add_action('save_post', 'wpforce_featured');
add_action('draft_to_publish', 'wpforce_featured');
add_action('new_to_publish', 'wpforce_featured');
add_action('pending_to_publish', 'wpforce_featured');
add_action('future_to_publish', 'wpforce_featured');

There is one more cool option in this code. If you don't have an image in the post, the above code will let you set a default featured image as well. You must edit the attachment ID according to your WordPress site. The example above uses the attachment ID "414"

Credits: WPForce

Popular Posts

 
Powered by Blogger.