Half-Elf on Tech

Thoughts From a Professional Lesbian

Tag: function

  • Capital H Dangit

    Capital H Dangit

    My new gig at DreamHost comes with a minor ‘d’oh!’ and that is it’s a capital H. Which I seem to be incapable of remembering. So I wrote a function. I stole capital_P_dangit() and swapped the WordPress for DreamHost. To save myself from embarrassment, here it is:

    // Capital H in DreamHost
    add_option( 'helf_capital_H_dangit', 'yes', '', 'yes');
    
    if ( get_option( 'helf_capital_H_dangit' ) == 'yes' ) {
        foreach ( array( 'the_content', 'the_title', 'comment_text' ) as $filter )
            add_filter( $filter, 'capital_H_dangit', 11 );
    }
    
    function capital_H_dangit( $text ) {
        // Simple replacement for titles
        if ( 'the_title' === current_filter() )
            return str_replace( 'Dreamhost', 'DreamHost', $text );
        // Still here? Use the more judicious replacement
        static $dblq = false;
          if ( false === $dblq )
            $dblq = _x('“', 'opening curly quote');
          return str_replace(
            array( ' Dreamhost', '‘Dreamhost', $dblq . 'Dreamhost', '>Dreamhost', '(Dreamhost' ),
            array( ' DreamHost', '‘DreamHost', $dblq . 'DreamHost', '>DreamHost', '(DreamHost' ),
          $text );
    }
    

    Now. Here’s where it gets fun. No ‘hacks’ posts will be affected by this code! Otherwise how would I show it to you here? Normally this is where you would just run remove_filter( 'the_content', 'capital_H_dangit', 11 ); in the functions file for your theme. Due to the way I’ve wrapped my various functions into mu-plugins, the down and dirty way was to wrap the above block of code with a check for if ( $blog_id != 2 ) { { ... }.

    Most of the time you won’t care about things like this. I just needed it so I could demonstrate code. I’ve done the normal filter remove here so I can also say ‘Wordpress’ in my code related posts. For proof this works, I assure you, 100%, that I typed in ‘Dreamhost’ over in my post about quitting my job and going to work for them.

    Sorry about that, Simon!

  • WordPress Site Description

    WordPress Site Description

    Someone asked me how I got the asterisks in my site description to be a link. It was actually really frustrating for about an hour. And then I remembered my filters.

    This site is using Hybrid Core, so there are some extra hooks:

    
    add_filter('option_blogdescription', 'halfelf_site_description');
    
    function halfelf_site_description($desc) {
            $desc .= '<a href="https://halfelf.org/#bitch" title="Brave, Intelligent, Tenacious, Creative and Honest">*</a>';
            return $desc;
    }
    

    If you’re doing it on a non-Hybrid theme, you have to filter bloginfo

    add_filter( 'bloginfo', 'halfelf_bloginfo', 10, 2 );
    function halfelf_bloginfo( $text, $show ) {
        if( 'description' == $show ) {
            $text .= '<a href="https://halfelf.org/#bitch" title="Brave, Intelligent, Tenacious, Creative and Honest">*</a>';
        }
        return $text;
    }
    

    Pretty simple.

  • bbPress 1.x Functions

    bbPress 1.x Functions

    Since I don’t actively develop for bbPress 1.x at this time, you’re on your own with these ones.

    bbPress is a forum platform by the same people who do WordPress. It’s very much a barebones bulletin board system (which makes layers on layers of bb jokes). bbPress is not for the non-techy at this point. It’s still in alpha mode, with a lot of rough edges, and is comparable to WordPress 1. Maybe. Most of the features are done as plugins, rather than defaults, so you don’t get things like PMing and Quick Tags and inline images. You have to add in plugins for that. While I was using it, I wrote a few of my own plugins and hacks to make things work like I wanted.

    All of these hacks can be directly applied to the functions.php file of your theme.

    Change ‘Sticky’ to an image

    If you make a topic sticky it gets ‘pinned’ to the top of the forum. This great for things like announcements etc. It’s really annoying that it’s hard to change. I wanted a post-it image to show that it was pinned, so I made a function to delete the default function and replace it with my own.

    This goes in your functions.php for your theme, just like it would for WordPress

    <?php
    // This sets sticky label as an image
    remove_filter('bb_topic_labels', 'bb_sticky_label', 20);
    function my_sticky_label( $label ) {
            global $topic;
            if (is_front()) {
                    if ( '2' === $topic->topic_sticky ) {
                            return sprintf(__('<img src="/images/sticky.png" alt="&#91;sticky&#93;" /> %s'), $label);
                    }
            } else {
                    if ( '1' === $topic->topic_sticky || '2' === $topic->topic_sticky ) {
                            return sprintf(__('<img src="/images/sticky.png" alt="&#91;sticky&#93;" /> %s'), $label);
                    }
            }
            return $label;
    }
    add_filter('bb_topic_labels', 'my_sticky_label', 20);
    ?>
    

    Change the text of a ‘Closed’ topic

    By default when you close a topic it renames the topic to Closed: Topic Name. I wanted to change that to Read Only: Topic Name, so what I did was delete the old closed filter and replaced it with my own.

    <?php
    // This sets closed lable to read as 'read only'
    remove_filter('bb_topic_labels', 'bb_closed_label');
    function my_closed_label( $label ) {
            global $topic;
            if ( '0' === $topic->topic_open )
                    return sprintf(__('[Read Only] %s'), $label);
            return $label;
    }
    add_filter('bb_topic_labels', 'my_closed_label');
    ?>
    

    Woopra

    If you use Woopra to monitor traffic and Gravatar for icons, you may notice that just slapping Woopra’s tracker in the bottom of your site doesn’t show the Gravars. Since bbPress themeing is what it is, I put this in my theme’s footer.php file to make gravatars show up in my Woopra console. The trick was getting it to extract the logged in user’s email. So here it is. Put this in your footer file and Woopra will show gravatars.

    <!-- Woopra Code Start -->
    <?php
            global $user_id, $user_email, $user_identity;
            $woopra_name = bb_get_current_user_info( 'login' );
            $woopra_email = bb_get_current_user_info( 'email' );
            if ( $woopra_name == '') {
                    echo "<script type=\"text/javascript\">";
                    echo "\n";
            }
            else {
                    echo '<script type="text/javascript">';
                    echo "\nvar woopra_visitor = new Array();";
                    echo "\nwoopra_visitor['name'] = '$woopra_name';";
                    echo "\nwoopra_visitor['email'] =  '$woopra_email';";
                    echo "\nwoopra_visitor['avatar'] = 'http://www.gravatar.com/avatar/" . md5(strtolower($woopra_email)) . 
                    "&amp;size=60&amp;default=http%3A%2F%2Fstatic.woopra.com%2Fimages%2Favatar.png';";
                    echo "\n";
            }
    ?>
    var _wh = ((document.location.protocol=='https:') ? "https://sec1.woopra.com" : "http://static.woopra.com");
    document.write(unescape("%3Cscript src='" + _wh + "/js/woopra.js' type='text/javascript'%3E%3C/script%3E"));
    </script>
    <!-- Woopra Code End -->
    
  • Ban Whack-a-Mole

    Ban Whack-a-Mole

    If Ban Hammer is too much for you, I understand. But you still want to smack down users from a specific domain? Otto came up with this and it works nicely.

    Copy the code into your theme’s functions.php file (just make one if you don’t have one) and change the call to .ru to whatever it is you need. I’m not 100% sure what would happen if you added in multiple calls, but this is perfect for banning just ONE annoying person.

    function no_russians($login, $email, $errors) {
            if (strpos($email, '.ru') !== FALSE) {
                    $errors->add('email_banned', __('ERROR: This email address is not allowed, please choose another one.'));
            }
    }
    add_action('register_post', 'no_russians', 10, 3);