Archive

Posts Tagged ‘Development’

Dropbox CDN Updates to come

December 2nd, 2010 5 comments

Some of you may have read this post over at Lifehacker about the bandwidth limitations that Dropbox puts on the public folder a user shares. Well, since my Dropbox CDN plugin uses the public folder of your Dropbox account, I’m going to be including a feature that will seamlessly check to make sure that you haven’t hit your Dropbox limit before using the plugin settings.

I hope to have this feature released sometime this weekend. I’ll keep you posted.

WordPress Dev 101: Replacing post content in-line

November 18th, 2010 No comments

Something I did recently when I updated the Better AdSense Targeting plugin, was create the ability to allow users to specify certain sections of each post that Google should ignore when determining what ads to display. I did this by using the str_replace() function in PHP and a little thing called ‘add_filter’ from the WordPress hooks. It’s rather simple and looks a little like the following.

First let’s think of something we might want to replace. Let’s say we want to make every instance of the string ‘[my other site]‘ turn into a link to your another website you run. Typically you’d need to code that out or use all the tools in the editing screen to do this, but instead, we cause use the power of str_replace() to do that for us.

We’ll need to include this fix in a plugin or the functions.php of your theme. First let’s make sure we’re adding the ‘filter’ on the content that we are taking from the post loop.

add_filter( 'the_content', 'url_replace' );

This line simply runs the function url_replace() whenever the_content() is run.

Next we’ll need to create the actual function that does the replacing:

function url_replace($content='') {
return str_replace( '[my other site]', '<a href="http://myothersite.com">My Other Site</a>', $content);
}

A little explanation here, we are running the function and are looking for instances of ‘[my other site]‘ and replacing it with the full HTML anchor tag and text in the string $content.

If you are wanting to replace multiple words or phrases you’ll need to do something like the following:

function multi_replace($content='') {
$content = str_replace( '[my other site]', '<a href="http://myothersite.com">My Other Site</a>', $content);
$content = str_replace( '[WordPress]', '<a href="http://www.wordpress.org/">WordPress.org</a>', $content);

return $content;
}

I don’t think I have to explain much here now, but we’re simply replacing any instance of ‘[my other site]‘ with the full HTML link and we are also looking for ‘[WordPress]‘ and making it link to WordPress.org. Easy enough, it creates links for you quickly, and allows you to focus more on writing. I find it common practice to put things like [WordPress] in and then later look for these strings in my drafts and replace them with links. Anytime I can avoid breaking my writing flow, I do, and shorthand notes are the way to go.

There you have it, you’ve successfully replaced content. Where this is useful is if you wish to include specific code wherever need be. You don’t have to replace this text with visible text. For instance, my AdSense Plugin replaces the string ‘‘ and ‘‘ with the proper HTML that I need to use to have Google AdSense ignore the content between these tags when selecting ads.

In this practice I used the str_replace() function, which is great for unique strings like [my other site] and [WordPress] however it can cause some issues when simply replacing normal text by including parts of other words as long as they meet the requirements of the replacement. For example, the word ‘and’ would be replaced in the words ‘brand‘, ‘sand‘, or ‘stand‘. If you are looking for a more indepth and intensive replace you might want to try using preg_replace() which uses Regular Expressions for it’s replacement rules

For more information on WordPress development check out the other posts in my ‘WordPress Dev 101′ series:

Happy Coding!

Categories: WordPress Tags: , , ,

WordPress Dev 101: Setting Activation/Deactivation options [updated]

October 3rd, 2010 2 comments


When you are writing a WordPress plugin, you are more than likely going to have some sort of settings involved. These options are stored in the wp_options (or wp_blog id_options for multi site users) table of your database. Most of today’s WordPress users aren’t privy to the fact that every plugin they install, probably has something within their database taking up space. Some of the sites I’ve seen have over 50 plugins installed that, at one point or another, were activated. That’s 50+ possible rows in the database of options that may not be needed anymore if the plugin developer didn’t remove the options from the database upon deactivation.

The goal of every plugin should be optimal user experience without affecting the overall performance of the users site. In order to provide this experience you should have a set of ‘default’ settings for your plugin. These option settings should be set upon activation. This provides users with immediate gratification, and who doesn’t like instant gratification? Likewise, if someone chooses to deactivate your plugin and delete it, you should no longer be leaving an imprint on their configuration.
Continue reading “WordPress Dev 101: Setting Activation/Deactivation options [updated]” »

Categories: WordPress Tags: , , ,

Using x.co with Twitter Tools in WordPress [updated: official plugin]

September 8th, 2010 No comments

I have made this an official plugin and you can visit the page for this plugin by visiting Twitter Tools – X.co URLs

So the other day GoDaddy.com released it’s new URL shortener x.co. They’ve also built in an API, so from that, we can now use the Twitter Tools WordPress plugin with the x.co URL Shortener. I’ve hacked together a file based off the Twitter Tools bit.ly plugin that works in conjunction with the main Twitter Tools plugin without any issues that I’ve noticed so far. For now you need to do the following:

  1. An account with x.co (which is the same as a GoDaddy.com account or using the steps listed below in the update)
  2. Install Twitter Tools for WordPress
  3. Download this .zip file and extract it into wp-content/plugins/
  4. Activate the Twitter Tools – x.co URLs plugin from your plugins list
  5. Login to your x.co account and click on the ‘Settings’ tab, copy down your API Key.
  6. From the ‘Settings’ menu select ‘Twitter Tools’ and you will need to enter your X.co API Key.
  7. That’s it, you are ready to tweet your posts with x.co shortened URLs!

If you have any feedback let me know and I’d be glad to see what I can do to help you out. I will try and get this up on WordPress.org within the next day or two for installation directly in your WordPress admin area.


Update:
You do not need a GoDaddy Account to use x.co. You can use the following steps:

To sign up for X.CO
  1. Go to X.CO.
  2. If you have a Go Daddy account, log in with your user name and password.
  3. If you don’t have a Go Daddy account, on the bottom-left of the screen, click Sign Up for a Free Account.
  4. In the To Create a New Account form, enter your information and create a password.
  5. When finished, click Create a New Account.

WordPress Dev 101: Hooks

August 30th, 2010 No comments


When I first got into WordPress development, I had no idea how the Core of WordPress was designed. I knew that I wanted my snippet of code to fire at a specific time, but short of hacking it into the theme I was using at the time, I had no clue how to achieve this. It was then that I discovered the Hooks of WordPress. Think of WordPress’ Hooks as the OnRamp to the rendering of your WordPress based site. Your code will sit dormant until the moment the Hook you have ‘Hooked’ fires, then your code will jump in and be executed. Continue reading “WordPress Dev 101: Hooks” »