Home  |  2011  |  June

Fixing Added Slashes in WordPress

I was developing some theme options and in fields where I was going to allow the user to enter in some HTML I was noticing that whenever a link was entered in (e.g. <a href=”http://www.google.com”>Google</a>) the actual outputted markup was causing the URL to be broken due to added slashes. The output would look like the following

<a href=\"www.google.com\">Google</a>

After searching around for a solution to this I eventually found that this issue is largely related to the configuration on the server and PHP’s deprecated magic quotes. Fortunately there is a way around it. I added the following code at the top of my functions.php file and that seemed to fix the problem.

$_POST      = array_map( 'stripslashes_deep', $_POST );
$_GET       = array_map( 'stripslashes_deep', $_GET );
$_COOKIE    = array_map( 'stripslashes_deep', $_COOKIE );
$_REQUEST   = array_map( 'stripslashes_deep', $_REQUEST );

Show Popular Posts in WordPress

Thanks to this blog post here on Bin Blog, below is a snippet of code that will allow you to show the most popular (i.e. most commented on WordPress post in your blog. Since most listings in various WordPress themes are returned as unordered list items, that’s how this code returns things as well. This could easily be modified, just remove the <li> and the </li> tags.

<?php
$popular_posts = $wpdb->get_results("SELECT id,post_title FROM {$wpdb->prefix}posts ORDER BY comment_count DESC LIMIT 0,10");
foreach($popular_posts as $post) {
    echo "<li><a href='". get_permalink($post->id) ."'>".$post->post_title."</a></li>\n";
    }
    ?>