Open Source Activism






         How to use open source technology for activism

April 18, 2008

How to filter out keywords in blog titles from the main page?

Filed under: Wordpress — lennie @ 11:26 pm

This procedure is for the more experienced since you will have to directly edit PHP code to do this filtering. Here are the steps to filter offensive or spam keywords inside the wordpress loop:

  1. Login into your server either through your CPanel or SSH
  2. Traverse into your blog directory/wp-content/themes/yourtheme
  3. Select your index.php file or the file containing your feed list.
  4. Make a copy this file for precaution
  5. Edit the original file and replace your loop code with the following example code (I’ll explain the code further down):

    <?php $my_query = new WP_Query('category_name=Syndication&showposts=20');
    while ($my_query->have_posts()) : $my_query->the_post();
    $do_not_duplicate = $post->ID;?>
    <?php
    $curTitle = the_title('','',false);
    if ( !eregi('boob',$curTitle)
    && !eregi('Hello World',$curTitle)
    && !eregi('cunt',$curTitle)
    && !eregi('pussy',$curTitle)
    && !eregi('nude',$curTitle) ) {
    ?>
    <li><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a><br><?php the_author(); ?></li>
    <?php } ?>
    <?php endwhile; ?>

  6. Save the file
  7. Open another browser window and check your main page
  8. If your feed is now correct, your are done. If not continue
  9. In your file manager find you error_log file. The location is different on each system, but CPanel usually has an icon that says Error Log
  10. Review the last several lines of the error log. It will tell you what line the error is on. Most likely it will be a syntax error in one of the filter lines
  11. Fix the error in the file you are editing and repeat the test

The code replacement was just a small sample. To grab the entire loop code with all of my filters right click this code link and save as yourfilter.php. Then you can easily add the full fulter list into your code.

Now, let’s walk through the code so you can see how this works. This first section queries the database and pulls back the last 20 posts that have been syndicated. It also makes sure there are no duplicates. This is the beginning of the feed loop, i.e. the Live Feed section of Blogivists.com

<?php $my_query = new WP_Query('category_name=Syndication&showposts=20');
while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate = $post->ID;?>

Next I open a new php segment and the first thing I do is grab the post title. Setting the third parameter to false gives me the title and prevents it from being displayed. I set the title to a variable to make it easier to do the multiple filter checks.

<?php
$curTitle = the_title('','',false);

Now, the filtering. I am using a case insensitive search that tests the keyword against the post title. If the keyword is found anywhere in the title the if statement is broken and I move to the next keyword. If it is false, I continue. Be careful what words your filter on. This check will find partial words within words and you could end up wondering why posts are not displaying. For instance, filtering on sex would also catch Middlesex and exclude it.

As a quick note, you’ll see I filter out all of the “Hello World” posts with a filter.

Terminology:
    ! (exclamation) reverses a True/False value so a false gives me true to continue
    ;&& is the symbol for AND, i.e. all values in the AND clause must also be True to continue

if ( !eregi('boob',$curTitle)
&& !eregi('Hello World',$curTitle)
&& !eregi('cunt',$curTitle)
&& !eregi('pussy',$curTitle)
&& !eregi('nude',$curTitle) ) {

An easy mistake to make is forgetting to close the final keyword test with a final parenthesis.

&& !eregi('nude',$curTitle) ) {

Now end the PHP code segment to print the valid post titles.

?>

Allow the HTML code to display the post titles in a list.

<li><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a><br><?php the_author(); ?></li>

Finally, close the If part of the filter and end the loop.

<?php } ?>
<?php endwhile; ?>



1 Comment »

  1. [...] How to filter out keywords in blog titles from the main page? By lennie This procedure is for the more experienced since you will have to directly edit PHP code to do this filtering. Here are the steps to filter offensive or spam keywords inside the wordpress loop:. Login into your server either through … Open Source Activism - http://opensourceactivism.blogivists.com [...]

      PHP Coding School » Blog Archive » php code [2008-04-19 04:52:35] — April 19, 2008 @ 12:10 am

RSS feed for comments on this post. TrackBack URI

Leave a comment

Hosted by Sam Adams Alliance - Disclaimers

FireStats icon Powered by FireStats