How to run the loop outside of WordPress

March 12, 2013 at 8:48 pm

Ever needed to be able to access your WordPress data and run a loop OUTSIDE of your WP install? Here’s a code snippet which allow you to run a WordPress loop on any PHP file, even outside of your WordPress install.

Paste the following code on any PHP file where you want to run your WordPress loop. Don’t forget to modify the following:
line 4: Please specify the path to your WordPress wp-blog-header.php file.
line 5: Query posts using the query_posts() function.

<?php
  // Include WordPress
  define('WP_USE_THEMES', false);
  require('/server/path/to/your/wordpress/site/htdocs/blog/wp-blog-header.php');
  query_posts('posts_per_page=1');
?>

<?php while (have_posts()): the_post(); ?>
   <h2><?php the_title(); ?></h2>
   <?php the_excerpt(); ?>
   <p><a href="<?php the_permalink(); ?>" class="red">Read more...</a></p>
<?php endwhile; ?>

Thanks to CSS Tricks for the great tip!

sql query to delete orphaned post meta in your WordPress database

March 6, 2013 at 6:02 pm

After an intensive use of your WordPress blog (creating posts, editing posts, and deleting posts) your database will contain thousands of rows of useless meta data. Here is a simple SQL query to delete orphaned post meta on your database.

Just run the following query on your WordPress database to delete orphaned post meta. Don’t forget to replace the table prefix wp_ if your database is using another prefix.
And of course, create a backup of your database before running the query!

DELETE pm
FROM wp_postmeta pm
LEFT JOIN wp_posts wp ON wp.ID = pm.post_id
WHERE wp.ID IS NULL

Thanks to Patrick Rauland for submitting this useful recipe!

How to allow more HTML tags in WordPress comments

March 5, 2013 at 4:40 pm

By default, WordPress accept a few HTML tags in comments, such as <a>, <strong>, etc… But what if you want to be able to use more HTML tags in WordPress comments? Just read on, I have the solution!

Simply paste the following code in your functions.php file. The list of tags to allow can be modified on line 4.

// Create function which allows more tags within comments
function allow_pres() {
  global $allowedtags;
  $allowedtags['pre'] = array('class'=>array());
}

// Add WordPress hook to use the function
add_action('comment_post', 'allow_pres');

Thanks to David Walsh for the handy tip!

Grid Layout Shock giveaway: 10 licenses to win!

February 28, 2013 at 4:47 pm

Today is the day for a new giveaway here at WPRecipes! This time, I will give away 10 licenses of Grid Layout Shock, a WordPress plugin which allow you to easily create Pinterest-like grids on your blog or website.

About Grid Layout Shock

Grid Layout Shock is a responsive WordPress plugin to create cool Pinterest-like grids using your posts, or external content (RSS). With several customization options to make every grid unique.

For more info about Grid Layout Shock, just have a look to the demo site: http://www.gridlayoutshock.com/

How to enter the giveaway?

It’s free, and it’s very simple: Just leave a comment on this post to join. In one week (Thursday March 7) 10 lucky winners will be randomly picked. They will receive their Grid Layout Shock license by email.

Now, good luck everyone!

WordPress function to show a total share counter (FB, Twitter, G+)

February 25, 2013 at 6:45 pm

Sharedcount.com is a useful website which allow you to get the total likes, shares, tweets, etc for a specific web page. Here’s a super handy function to display how many times a page has been liked/shared/tweeted on your blog.

Simply paste the following function where you want your counter to appear:

function social_shares() {
    $url = get_permalink( $post_id ); 
    $json = file_get_contents(&quot;http://api.sharedcount.com/?url=" .
rawurlencode($url));
    $counts = json_decode($json, true);
    $totalcounts= $counts[&quot;Twitter&quot;] + 
$counts[&quot;Facebook&quot;][&quot;total_count&quot;] +
$counts[&quot;GooglePlusOne&quot;];
    echo &quot;&lt;div&gt;$totalcounts Share&lt;/div&gt;&quot;;
}

Thanks a lot to Davide for submitting this tip!

How to easily make WordPress images responsive

February 19, 2013 at 5:12 pm

Responsive images can be big on wide screens and automatically adapt to smaller screens such as iPad. Making your images responsive is not difficult to do: Here is a simple recipe to achieve it on your blog.

The first thing to do is to create the shortcode. To do so, open your functions.php file and paste the following code in it:

function responsive_images($atts, $content = null) {
     return '<div class="image-resized">' . $content .'</div>';
}
 
add_shortcode('responsive', 'responsive_images');

Once done, open your style.css file and add those CSS rules:

@media only screen and (max-width:767px) {
    .image-resized img {
        width:100%;
        height:auto;
    }
}

You can now use the [responsive] shortcode to insert responsive images in your blog:

[responsive]<img src="image_url" alt="alt" title="title" />[/responsive]

Thanks to Rockable Themes for the tip!

How to disable automatic paragraphs in WordPress editor

February 14, 2013 at 6:19 pm

By default, WordPress automatically creates paragraphs on your post content. While this is generally useful, you might want to change this behavior to fit your specific needs. Today’s recipe explains how you can disable automatic paragraphs.

Simply paste the following line into your functions.php file:

remove_filter('the_content', 'wpautop');

That’s all. Once you saved the file, WordPress will no longer create automatic paragraphs on your content.

SQL query to turn categories into tags (and vice versa)

February 11, 2013 at 5:23 pm

Using the “Categories to Tags” tool, WordPress allows you to easily turn categories into tags. But if you have lots of categories to convert, this will take a while. Here is a simple SQL query that will turn all your categories into tags in a second.

Just run the following query on your WordPress database, and all categories will be turned into tags. Don’t forget to replace the table prefix wp_ if your database is using another prefix.
And of course, do not forget to make a backup of your database before running the query!

UPDATE wp_term_taxonomy SET taxonomy='post_tag', parent=0 WHERE taxonomy='category';

Thanks to Go WordPress for the tip!

How to exclude specific categories from your blog homepage

February 7, 2013 at 6:38 pm

When displaying your latest posts on the homepage, you might want to exclude some categories from displaying on this page. Here is a simple recipe to exclude specific categories from your blog homepage.

Copy the snippet below in your functions.php file. Replace the category IDs on line 3 with the ones you want to exclude. Then save the file and you’re done.

function exclude_category_home( $query ) {
    if ( $query->is_home ) {
        $query->set( 'cat', '-5, -34' );
    }
    return $query;
}
 
add_filter( 'pre_get_posts', 'exclude_category_home' );

Thanks to WP Mayor for the code snippet!

ThemeFuse giveaway! 3 premium themes to win

February 5, 2013 at 5:07 pm

Do you want to win a free premium theme from our friends at ThemeFuse? Well, today’s might be your lucky day as ThemeFuse will give away 3 premium themes to WPRecipes readers.

A word about ThemeFuse

ThemeFuse is one of the most popular actors in the premium WordPress themes market. They provide lots of themes with a great design and solid code.

They have lots of different designs for lots of different needs, so you should definitely have a look to their website!

How to enter the contest?

To enter, simply leave a comment on this article to let me know which theme from ThemeFuse WordPress themes you’d like to win. In one week (February 13, 2013), I’ll randomly pick up 3 winners. Each winner will receive the theme of their choice from ThemeFuse.

Good luck everybody!