Is it just み?

Tag: Wordpress

WordPress: Highlight PHP Code

by on Nov.22, 2009, under PHP, Programmierung, Technik

Ich werde mal versuchen einige meiner Einträge auf Englisch zu verfassen – um in der Übung zu bleiben.

I like to write about programming, and since I am using WordPress and writing plugins for it, PHP is the language of choice when it comes to blog-related development.

I wanted to put my PHP-Code into my entries and I wanted it to look nice, which means mono-spaced font and syntax highlighting. The first one was fairly easy to archieve by just changing the CSS. Whenever I put code into my blog, I write it between <pre> and </pre>, so I just had to change the CSS for the pre-element:

 pre {
    height: auto; 
    overflow: auto;
    color: #EEE;
    padding: 5px;
    margin: 10px;
    scroll: auto;
    font-family: Courier New, monospace;
    font-weight: bold;
    white-space: pre;
    font-size: 9pt;
    line-height: 10pt;
}

The highlighting however must be done on the server side by PHP, so I had to write another (in fact two) filter-function:


<?php


function highlightCode($content) {
    if (
false === strpos($content'<pre>')) {
        return 
$content;
    }

    
$content preg_replace_callback(
        
'|\<pre\>(.*?)\</pre\>|is',
        
'highlightCodeCallback',
        
$content
    
);

    return 
$content;
}

function 
highlightCodeCallback($matches) {
    if (
false === strpos($matches[0], '<?php')) {
        return 
$matches[0];
    }
    
    
$code $matches[0];
    
$code preg_replace(
        
'|\<code\>\s*\</code\>|is'
        
'/*§§BR§§*/'$code
    
);
    
$code strip_tags($code);
    
// Wordpress adds the space when saving
    
$code str_replace('<?php''<?php'$code); 
    
$code highlight_string($codetrue);
    
$code str_replace('/*§§BR§§*/'"\n\n"$code);
    
$code htmlspecialchars_decode($code);
    
$code str_replace('<?php''<?php'$code);

    return 
'<pre>'$code .'</pre>';
}
Comments Off on WordPress: Highlight PHP Code :, , more...

WordPress: Filter certain categories on main page

by on Nov.20, 2009, under PHP, Programmierung

Ich werde mal versuchen einige meiner Einträge auf Englisch zu verfassen – um in der Übung zu bleiben.

I am using the Twitter Tools plugin to automatically create Tweets for my blog entries and vice versa to have some kind of tweet archive on my blog. That works flawlessly.

The only problem is: I tweet too much. I only want to show five entries on my front page, which basically means, that my last real blog entry is gone after one or two days, because of the twitter entries. The solution: Entries from the Tweets category should not be visible on the front page.

I had to extend my my-blog-exclusively-plugin with a function that filters the posts that appear on the front page, for this I added a filter to the hook “the_posts” which is called quite early in the loop before anything has been done to the posts. The posts that would be shown on the front page are given to the callback-function as first parameter in the form of an array. Here is the code:


<?php


/**/
// Filter post from the category Tweets on front page
add_filter(
    
'the_posts',
    
'filterTweets'
); /**/

function filterTweets($posts) {
    
// Filter only on the front page!
    
if (!is_front_page()) {
        return 
$posts;
    }

    
$tweetCategoryName 'Tweets';
    
$numVisiblePosts 5;

    
$postsToShow = array();
    
$filteredPosts = array();

    
$num 0;
    foreach (
$posts as $post) {
        
$cats in_category($tweetCategoryName, (int) $post->ID);
        if (!
$cats) {
            
$postsToShow[] = $post;
            
$num++;
        } else {
            
$filteredPosts[] = $post;
        }

        if (
$num >= $numVisiblePosts) {
            break;
        }
    }

    if (
$num $numVisiblePosts) {
        
// Too many twitter posts... what the hell... show some of them
        
$numTwitterPosts $numVisiblePosts $num;
        
$postsToShow array_merge(
            
$postsToShow
            
array_slice($filteredPosts0$numTwitterPosts)
        );
    }

    return 
$postsToShow;
}

Another thing I had to do was increase the number of posts shown on the front page, since my plugin now handles restricting the number of posts and I have to ensure that my function gets at least some posts that are not tweets. (But even if there are not enough, it fills up the front page with twitter posts.)

Comments Off on WordPress: Filter certain categories on main page :, , , , more...