[WordPress] Detect a user’s admin color scheme

So when making a plugin, I needed to use jQuery UI and make it match the admin color scheme. I didn’t think it would be too easy.

I was wrong.

I just had to use the get_userdata function. I don’t know why I thought it would be more difficult than that. So in order to keep others from falling into my pit of confusion, I’m posting this.

1
2
3
4
5
//get the data of the user with an id equal to the $id variable
$user=get_userdata($id);

//print the admin color scheme being used
echo $user->admin_color;

See how easy that was? I almost didn’t write this post because it was so easy, but if I got confused, then others may too.

Go ahead. You can laugh at me now. I can take it.

Posted in Code | 1 Comment

[WordPress]Using the media browser in your plugin

I try to make plugins that tie in as much as possible with default WordPress functionality. I like the idea that my plugin can seem to get better if WordPress makes an improvement. Maybe that’s only in my mind, but it’s there none-the-less.

I have a plugin that I’m working on for church that involves the need to attach an image to a database record. In order to do this, I thought that using the media browser would be the best option. The problem was I wasn’t sure how to do it since there isn’t a straight up function for that sort of thing.

What helped save me from banging my head against a wall trying to figure it out were two posts that laid it out, plain and simple.

The first post I found was from Matt over at Webmaster-Source. It was very clear in how to implement the media browser in my plugin, and it works well for using with one form with hardcoded ids and names. This, however, was very close, but still not what I needed.

Enter Rich from The English Guy Web Design. He adapted the same code for use with multiple entries on a single page. That was exactly what I needed. I plugged it into my plugin and tested it, and I got that smug satisfaction that can only come from seeing something work the way it’s supposed to.

Now with that out of the way, I can move on to implement Google Maps into the same plugin. Let’s hope that works out the way it’s supposed to.

Posted in Code, PHP, Plugins, Wordpress | Leave a comment

[WordPress] TypePad AntiSpam

I had asked on Twitter awhile back what a good spam fighting plugin would be. The only catch was that it couldn’t be Akismet. I have nothing against Akismet, but I didn’t have money to pay for a plugin.

Someone then recommended I check out TypePad AntiSpam. I was skeptical about the name at first, but once I installed, I realize there was nothing else I needed to do. The plugin uses it central database to find potential spam and automatically marks it as such.

After using it for a few weeks, I can confidently say that this plugin has cut my spam management time down to nearly nothing. I have gotten a handful of spam comments instead of the 10 or more a day I was used to.

The plugin is available in the repository and at their website. You will need an API key, which is free, but once that’s done, all you will have to do is check it every now and again. I highly recommend this plugin.

Posted in Plugins, Wordpress | Leave a comment

[WordPress] How to make a shortcode plugin

If you (like me) are one of those impatient types that wants to see the full code right now, scroll to the bottom of the post.

There are so many plugins available for WordPress! And many of those use shortcodes to do their work. So many, in fact, that you can make WordPress do just about anything you’d want a CMS to do. What happens, though, when you can’t find one that you need? Why make your own of course!

Using the API, you can use your PHP skills to implement all sorts of things. First, you have to make sure you have correctly formatted plugin information as outlined here. The rest of the time I’m going to assume you have done this.

Shortcodes can be very versatile and they allow you to pass variables to them via the $atts variable. This variable must be run through the shortcode_atts function if you want to use them, though. Once run through the function, it will return an associative array with your variables as the keywords. Word of warning here: you MUST use lowercase variables here. Uppercase will not work.

In addition to the $atts variable, you have the $content variable which, if you are using a plugin that uses beginning and ending tags, returns the content from between your tags. This is useful if you want to allow someone to format the content being altered or just to pass a large string.

The last variable is the $code variable. This is rarely used and is only to pass the shortcode name being used. I have never used it, myself, but it’s there if you need it.

To start let’s create a function

1
2
3
4
function my_function($atts,$content){
    $html='Hello World';
    return $html;
}

Note that we return the string instead of simply echoing. This allows WordPress to place the shortcode where you want it and not at the top of your content.

Next we should let WordPress know that this is a shortcode function. We just add this bit of code after the function

1
add_shortcode("my_shortcode", "my_function");

Now let’s mess with our attributes. Let’s say we may want to switch out ‘World’. We could do a str_replace, or we could use the $attr variable with a default settings array.

1
2
3
4
5
6
7
function my_function($atts,$content){
    $opts=shortcode_atts( array(
        'who' => 'World'
        ), $atts );
    $html='Hello '.$opts['who'];
    return $html;
}

With the shortcode_atts function, the first associative array you pass is your default settings. These will be overridden, however, by the $atts you pass to the function. That means that you could put something like

[my_shortcode who=”Everyone”]

and the output would be “Hello Everyone”.

Now what if you have some content between those tags that you want to edit? That sounds like a job for the $content function!

1
2
3
4
5
6
7
function my_function($atts,$content){
    $opts=shortcode_atts( array(
        'who' => 'World'
        ), $atts );
    $html=str_replace('{greeting}','Hello '.$opts['who'],$content);
    return $html;
}

Now the function does a string replace looking for the {greeting} string and replacing it with our Hello World string. So if you used the shortcode

[my_shortcode who=”Everyone”]. How are you?[/my_shortcode]

you would get the result “Hello Everyone. How are you?”.

That’s all there is to the shortcodes! Obviously you would probably want to do something a smidge more complex, but this give you the basic idea.

Full code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function my_function($atts,$content){
    //get any attributes that may have been passed; override defaults
    $opts=shortcode_atts( array(
        'who' => 'World'
        ), $atts );

    //adjust the $content
    $html=str_replace('{greeting}','Hello '.$opts['who'],$content);

    //return the html, don't echo
    return $html;
}

//let WordPress know the function is a shortcode
add_shortcode("my_shortcode", "my_function");
Posted in Code, How To, Informative, PHP, Plugins, Wordpress | Leave a comment

[PHP] Convert HEX to RGB and back again

I have done work with colors in PHP numerous times. Typically for the purpose of making various plugins work with each other and with user input.

Along the way I found a set of plugins that would do the converting for me in a simple and straight forward way. The backbone of these functions are the hexdec and dechex functions. Simple, but powerful for what we need.

So without any more explanation, here’s how to convert the HEX to RGB:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
function html2rgb($color){
   
    //remove the hash
    if ($color[0] == '#')
        $color = substr($color, 1);

    //find the length of the string and split it up by 1 or 2 digits
    if (strlen($color) == 6)
        list($r, $g, $b) = array($color[0].$color[1],
                                 $color[2].$color[3],
                                 $color[4].$color[5]);
    elseif (strlen($color) == 3)
        list($r, $g, $b) = array($color[0].$color[0], $color[1].$color[1], $color[2].$color[2]);
    else
        return false;

    //assign the correct RGB values
    $r = hexdec($r); $g = hexdec($g); $b = hexdec($b);

    return array($r, $g, $b);
}

And now we go back again!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
function rgb2html($r, $g=-1, $b=-1)
{
    //if the first value is an array, assign accordingly
    if (is_array($r) && sizeof($r) == 3)
        list($r, $g, $b) = $r;

    //turn the values into numbers because of the list function
    $r = intval($r); $g = intval($g);
    $b = intval($b);

    //convert the RGB values into HEX
    $r = dechex($r<0?0:($r>255?255:$r));
    $g = dechex($g<0?0:($g>255?255:$g));
    $b = dechex($b<0?0:($b>255?255:$b));

    //combine the values into a single HEX value
    $color = (strlen($r) < 2?'0':'').$r;
    $color .= (strlen($g) < 2?'0':'').$g;
    $color .= (strlen($b) < 2?'0':'').$b;
    return '#'.$color;
}

Simple and straight forward. They handle the job well, and can help you with most color conversions.

Posted in Code, PHP | Leave a comment