Exclude SlideDeck posts in WordPress

Tags: , , , ,

On a WordPress site I’ve been working on I’m using SlideDeck as a feature article slider on the homepage.  The only problem was I have additional articles populating underneath it and the articles from the SlideDeck were showing up down there as well.

We didn’t want that behavior so I researched how to filter out SlideDeck posts from the post loop.  At this time, it’s apparently easy to filter for content that has a custom meta-tag, but you can’t easily filter for content that doesn’t have a custom meta-tag applied to it.  It’s because when a post doesn’t have a custom met-tag attached to it that custom meta-tag doesn’t come up as “null” or “0”  it simply doesn’t come up at all.  So there’s no way to use query_posts to filter out SlideDeck posts, you have to make custom database query and use $wpdb->get_results.

This post from SlideDeck’s customer support website did most of the heavy lifting for me in putting together the exact query I needed to filter out only SlideDeck posts.  The only problem was that this gave me no insight as to how I could paginate the posts.  Thankfully google came to rescue once again and I found this post and this post which helped me piece together the script.  I posted my findings on SlideDeck’s customer service website, but I’m posted it here as well so I can easily find it again if I need it.

So for the code, inside loop.php before have_posts() I put…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
if(is_home()) {
    //query_posts(array( 'meta_key' => 'slidedeck_post_featured') );
    global $wpdb;
    $ppp = intval(get_query_var('posts_per_page'));
 
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $offset = ($paged-1) * $ppp;
    $sql = "SELECT p.*
        FROM {$wpdb->posts} AS p
        LEFT JOIN {$wpdb->postmeta} AS pm
        ON pm.post_id = p.ID AND pm.meta_key = %s
        WHERE p.post_status = %s AND p.post_type = %s AND pm.meta_value IS NULL
	ORDER BY p.post_date desc
        LIMIT $offset, $ppp";
    $posts = $wpdb->get_results($wpdb->prepare( $sql, '_slidedeck_post_featured', 'publish', 'post' ));
}

if(is_home())  will make it so it will only filter out SlideDeck posts on the homepage, and apparently the have_posts() works exactly as it normally does even when I change the loop using $wpdb->get_results, which means I don’t have to do much to the rest of the page in order for this to work.  WordPress, you rock <3

This will paginate the posts according to the number of items I have set in the WordPress settings, but for some reason when I did this on the last page it printed out empty posts to fill out that page.

For instance, say I only have 7 posts left on the last page, but I have it set to show a maximum of 10 posts per page – it would print out 3 empty posts on that page.

To combat this I put the following code inside have_posts() so it wouldn’t print anything if the post ID was null.

1
2
3
if(get_the_ID() > 0) {
    //html for posts
}

Pretty straight forward and everything has been working great so far :)

 

References

http://support.slidedeck.com/slidedeck/topics/wp_query_a_list_of_posts_not_featured_in_the_slide_deck?from_gsfn=true

http://wordpress.org/support/topic/custom-loop-pagination

http://www.scriptygoddess.com/archives/2011/02/23/wordpress-pagination-woes-solved-i-hope/

Permalink » No comments

PHP database backup

Tags: , , ,

I recently added in a way for admin users to download a backup of the database to a CMS I work on.  I came up with something between this solution and this one.  Add this little snippet after a database connection and change “table_1″ and “table_2″ to whichever tables you’d like to output, and add as many tables as you need.

What I like about this script is that it doesn’t save a copy to the server so folks can’t nab it if they know the entire URL, but it can be easily integrated to a web based interface.  It also uses “create table if not exists” which was missing from the other two scripts.  I think a backup should create tables in case you loose the table structure, but it shouldn’t delete the existing data.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?php
function datadump ($table) {
    $result .= "# Dump of $table \n";
    $result .= "# Dump DATE : " . date("d-M-Y") ."\n\n";
    
    $row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
    $result .= "\n\n".str_replace('CREATE TABLE','CREATE TABLE IF NOT EXISTS',$row2[1]).";\n\n";
    
    $query = mysql_query("select * from $table");
    $num_fields = @mysql_num_fields($query);
    $numrow = mysql_num_rows($query);
    
    for ($i =0; $i<$numrow; $i++) {
        while($row = mysql_fetch_row($query)) {
            $result .= "INSERT INTO ".$table." VALUES(";
            for($j=0; $j<$num_fields; $j++) {
                $row[$j] = addslashes($row[$j]);
                $row[$j] = ereg_replace("\n","\\n",$row[$j]);
                
                if (isset($row[$j])) $result .= "\"$row[$j]\"" ; else $result .= "\"\"";
                if ($j<($num_fields-1)) $result .= ",";
            }
            $result .= ");\n";
        }
    }
    return $result . "\n\n\n";
}
 
$content = datadump('table_1').datadump('table_2');
 
$file_name = "database_backup.sql";
Header("Content-type: application/octet-stream");
Header("Content-Disposition: attachment; filename=$file_name");
echo $content;
?>

Permalink » 3 Comments

Twitter Critter

Tags: , , , ,

I recently just finished a project called “Twitter Critter” It’s a simple program that works with Twitter to generate an avatar.  All you have to do is enter in your own username, or anyone’s username, and it will create an avatar based on your Tweets.

It searches through your latest 200 tweets and uses those words to determine what color, critter, and what accessories your Twitter Critter will have.  It also generates a cute bio for that Critter based on those choices.  There are thousands of possible combinations, and if you come back after doing some Tweeting your result will change!  You can also Tweet out a permalink to the specific Twitter Critter your generated, and with one click you can set it as you avatar on Twitter.

Click here to get started!  http://twitter-critter.contropa.com/

Related Blogs:

http://rakugakiman.blog6.fc2.com/blog-entry-2827.html

Permalink » No comments

Spamlists and WordPress

Tags: , , , ,

I’ve been getting dozens of e-mails with spam posts to this blog and it’s become far to annoying to ignore any further!  I’ve been using WordPress’s integrated blacklist tool, but it’s time consuming to maintain and only protects against past threats.  I took a look at the database and I’ve managed to collect over 3,000 spam comments that take up more than 20 mbs of database space…which is quite a lot IMO.

In the past the best prevention I’ve found against spammers is to use spam lists.  One I like in particular is FSpamlist.com.  You have to sign-up to get an API key, but after that using it is very straight forward.   Stop Forum Spam is a big one too, you don’t need an API key to check for spammers so it was even easier to add.  I put a simple WordPress plugin together to integrate the lists with my blog.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?php
/*
Plugin Name: Spamlist
Plugin URI: http://wannabe.contropa.com/fspamlist-and-wordpress/351/
Description: Uses spamlists to stop spammers
Version: 1.0
Author: Foxumon
Author URI: http://www.contropa.com
License: GPL2
*/
 
add_action('wp_blacklist_check', 'spamlist', 10 , 6);
 
function spamlist($author, $email, $url, $comment, $user_ip, $user_agent) {
    $username = $author;
    $email = $email;
    //$ip = $user_ip;
    
    $fspamcheck = file_get_contents('http://www.fspamlist.com/xml.php?key=######&spammer='.$email.','.$username.','.$ip);
    if (strpos($fspamcheck, '<isspammer>true</isspammer>') !== FALSE) {
        wp_die('Your e-mail address has been marked as spam.  You may request that it be removed here: http://www.temerc.com/forums/viewforum.php?f=72','spam');
    }
    
    $fspamcheck = file_get_contents('http://www.stopforumspam.com/api?email='.$email.'&username='.$username);
    if (strpos($fspamcheck, '<appears>yes</appears>') !== FALSE) {
        wp_die('Your e-mail address has been marked as spam.  You may request that it be removed here: http://stopforumspam.com/removal','spam');
    }
}
?>

I’m currently not planning on submitting it to the WordPress plugins directory or anything like that, but you want to try it out too you just save the code as “spamlist.php” add it to a “spamlist” folder in your plug-ins directory and then it ought to show up in your admin where you can active it.

It’s currently not set to report the spam that it blocks in any way…I’m not sure if I want it to or not.  I’m currently looking at over 3k of spam comments that are just sitting around taking up room in my database, but at the same time I don’t want to get any legitimate posters blocked without anyway of knowing.  Anyways, I’m going to test it out for now and see how it goes :)

Permalink » No comments

Facebook Connect for phpBB2

Tags: , , , ,

Another mod for phpBB2 that I have just completed is the well sought after “Facebook Connect” mod.  It’s a very straightforward version and should be very simply to install, though I give no guarantee that it will work for you.

I used to JavaScript Api to get the job done.  In a nutshell what it does is it first adds in a column to your User’s table to store someone’s Facebook ID.  Then the first time a user clicks on the “facebook connect” button it will ask the user if they want to link to an existing account or create a new one.  It will then add in the facebook ID to th row for the selected account.  Then the next time a user clicks on the “Facebook Connect” button it will check the User’s table for the user’s facebook ID and automatically log them in.

So, like I said, pretty straight forward.  The forum account will still work independently from Facebook, but will give the user instant activation and a faster way to login.

Features:

  • Instant account activation.
  • Single click log in when user is already logged into Facebook.
  • Allows for linking/unlinking for logged in users.
  • Will add the user’s Facebook avatar if they don’t already have one.

Click here to download the mod

8/02/10 Minor edit needed: It reads “Please submit both an email and password” which should be changed to “Please submit both a username and password”

Permalink » 13 Comments