PHP: Category Archive (Page 3)

Posts about developing with the PHP programming languages, including other projects which use it

Monday, August 6, 2007
  Incorporating an Akismet Counter into a WordPress Theme

Akismet is, by far, the most popular anti-spam plug-in for WordPress. (It comes bundled with the download, so that gets it market share. But, it's also very, very good.) It comes with a counter that can be put into a WordPress theme. It's attractive, but its light blue color may not integrate well into a given theme.

I went digging around in the source code, and found the line that actually pulls the count from the database. Using this parameter, I was able to integrate a spam count into the sidebar that has a look consistent with the rest of the site.

Here's the code that's in use on the theme on this site.

<li id="spamstats">
  <h2><?php _e('Akismet-Eaten Spam:'); ?></h2>
  <ul>
    <li><a href="//akismet.com"><?php
      echo(number_format(get_option("akismet_spam_count"))); ?>
      and counting...</a></li>
  </ul>
</li>

Of course, line 5 is the important one - that's how to get the number, formatted for whatever locale the server is set up for. (On my personal blog, the number is up over 1,400!)

Categorized under
Tagged , , , ,

Wednesday, June 13, 2007
  Posting Source Code in WordPress, Take 2

In my searching, I have found another WordPress source code plugin, called wp-syntax. This one uses GeSHi, the Generic Syntax Highlighter. It features many languages, and is extensible to even more. (If I ever post a COBOL snippet, I'll probably add COBOL support to it, and contribute it to the codebase.)

To use it, you simply put a pre tag, followed by lang=[language]. It will also do line numbering. Of course, with a single “language” parameter, the embedded language will not be highlighted as well. In this case, the previous plug-in works better; although the syntax highlighting has to be done manually, it can handle multiple languages.

(NOTE: The samples have been removed, as this blog is not running under WordPress. However, since the source code from another post was moved here, it is presented below using myWebLog's code highlighting.)

<?php
/**
 * This creates a list of category links that can be used with a category dropdown
 */
$aCategories = get_all_category_ids();
$iMaxCat = 0;
foreach($aCategories as $iThisCat) {
    if ($iMaxCat < $iThisCat) {
        $iMaxCat = $iThisCat;
    }
}
$iMaxCat++;
?>
<div style="text-align:center;">
<form name="categoryform" action="" style="text-align:center;">
    <script type="text/javascript">
        var aLink = new Array(<?php echo($iMaxCat); ?>);
<?php
foreach($aCategories as $iThisCat) {
    echo("aLink[$iThisCat] = \"" . get_category_link($iThisCat) . "\";\n");
} ?>
        function goCat() {
            window.location =
                aLink[document.getElementById('cat')[document.getElementById('cat').selectedIndex].text;
        }
    </script>
    <?php wp_dropdown_categories('class=sidebardropdown&orderby=name&show_count=1&hierarchical=1'); ?>
    <br /><br />
    <button class="sidebarbutton" type="button" style="margin-top:5px;" onclick="goCat();">View Category</button>
    </form>
</div>

This is another option, and is probably what I'll use for single-language posts, or posts where the embedded language may not be crucial.

Categorized under
Tagged , , , ,

Wednesday, May 30, 2007
  Posting Source Code in WordPress

Traditionally, posting source code in a WordPress blog, especially if it were HTML or PHP, was problematic. There are a few reasons for this…

  • WordPress is very good about ensuring that it outputs valid XHTML, so it strips mis-matched and invalid tags. This is usually desirable, but it wreaks havoc with HTML and XML code posts.
  • Since WordPress is coded in PHP, blocks of PHP will attempt to execute.
  • White space is collapsed, which can kill any readability that the user has set out; the “visual editor” (TinyMCE) does its best to create efficient HTML. This can actually break spacing-oriented languages such as Python.

So how do you do it? It's actually pretty easy, using the Code Markup WordPress plugin. You can download the plugin from that link, and upload it to ./wp-content/plugins. The instructions on the website are critical - the “visual” editor will not allow the code to come through unscathed. However, the visual editor only corrupts the code if you actually save it; what I have done is write the post using the visual editor, then disable it and put the code in. It's the best of both worlds! (Make sure that, once you have the code in, you don't edit the post using the visual editor - unless you have a good backup…)

The post below this one (about category lists in WordPress) was done using this plugin. (This isn't running in WordPress any more.) In addition, you can use the span tag to do the color-coding. I created a few CSS classes (“key” for keywords, “func” for functions, “attr” for attributes, “embed” for embedded language), and used them to accomplish the color coding. It seems that it would be pretty easy to write another plugin that used a list of keywords to do this syntax highlighting; maybe that's a challenge for another day.

A big “atta boy” to Bennet McElwee for a fantastic plugin!

Categorized under
Tagged , , ,

Thursday, May 24, 2007
  Category Drop-Down in WordPress

WordPress provides a template tag, wp_dropdown_categories, that inserts a drop-down list (the HTML select element) of categories, where the value of each item is the ID from the database. This works fine if you are not using rewrite rules (AKA “pretty links”) - you can construct a URL using the value (?cat=[number]). However, if you use any sort of rewrite rules, this does not work. I recently converted my personal site, which uses the Pool theme, to utilize a JavaScript array to assist with displaying category pages.

Here's the code…

Edit: Code has been moved to this post.

This works for both “pretty” and standard links, as it uses the template tag get_category_link to specify the link.

Categorized under ,
Tagged , ,

Sunday, August 1, 2004
  Wow

Today I installed Fedora Core 2. This thing is slick! WBEL looked a lot like RH8, which I had seen before my renewed Linux learning began. FC2 has a graphical loader that hides a lot of the background stuff (unless an error occurs) - that's cool. During the install, I skipped OpenOffice.org and MySQL, although I installed PHP with MySQL support. The reason for that is that I wanted to get the latest and greatest versions of those two products. We'll see if this proves to be a good decision or not.

The wireless network card still wasn't recognized (phooey). I did some more searching, armed with the knowledge that I have an adm8211 chipset. One of the first hits under Google's Linux search for “adm8211” pointed me to a project called NDISwrapper. This is a “wrapper” that uses the vendor's Windows DLL file, and converts the hooks from Windows to Linux. Doing this, this driver can (in theory) support most any network card, especially those that aren't in the Linux Hardware Compatibility List (HCL). I downloaded it, compiled it, and followed the directions to install my driver under it. I still wasn't able to create a connection, but on a hunch, I restarted the computer. NDISwrapper is also a kernel module, and I know that often those are only read at startup. Once the computer was restarted, I was able to create a connection, and now my network card works! YEA!!! (And it was only one night's worth of work - much better.)

Now that I have networking working under both operating systems, I plan to try to get four things working the same, whether I'm booted to WXP or Linux - E-mail (using Mozilla Thunderbird), PHP (using Apache on Linux, IIS on WXP), MySQL (using the exact same version on both), and a web server that uses the same html root directory (again, Apache on Linux, IIS on WXP). If I didn't already have IIS up, running, and configured under WXP, I'd probably just do Apache on both, but this will be interesting - it should work, as I don't have many creative permission rules.

Categorized under , , , ,

Sunday, June 27, 2004
  Programming Like a Banshee

Despite being ill early in the day (which caused me to miss Sunday School and church), the day on the computer went pretty well. I made a lot more progress on TMTS, although I ran into a problem - it appears that the PHP that came with WBEL may not have MySQL support enabled. Rather than get sidelined with this right now, I'm going to continue converting pages on the application, and work this issue as part of unit testing.

I also managed to catch up on comp.lang.cobol and comp.sys.unisys, two newsgroups in which I participate. I was able to play a file off a DVD (although I still can't play the disc itself).

Categorized under ,