Custom WordPress Strings with WP-Strings

Custom WordPress Strings with WP-Strings

By Jonathan on July 16, 2012

I’ve been meaning to develop a custom string plugin for a long time now, I first thought of it when going through a clients website changing contact numbers and other pieces of information. WP-Strings plugin allows anybody to enter custom text into fields in the admin area of WordPress. By using this method to input text into your WordPress website, when you come to change key information, like a telephone number, you only have to change it in one location. Making updating the site much easier to maintain in the long run. By default there are 10 strings that can be used, however with the use of some nifty Ajax you can add and delete new custom string names on the fly. Furthermore there is no limit on the amount of strings you can have, or the custom text length.

As you can see below, this is where you would add your custom string names, remember though these are only the names that you’ll reference them by. You can enter the text that you want to display into the text fields on the main form. You will also notice that you can add up to 100 new strings at once, so this should also save you a chunk of time!

You might now be thinking ‘Thats all good, but how can I display this on my website?’. Well, there are two ways you can do this which will allow the custom strings to be placed anywhere on your website. Firstly lets take a look at using shortcodes, you can use these shortcodes from within the backend of wordpress, these will mainly be used for posts or pages that don’t require additional WordPress templates. If you’ve use WordPress shortcodes before, then its pretty simple, if not here hows you use them.

Where ever you want your custom string to appear, simply get the ‘NAME’ of the string ie, Telephone. Secondly add the following code: [string s=""], but in between the quotations place your custom string name. You should have something that looks like this: [string s="telephone"]. This should then display whatever text you have input for the custom string telephone. A few things to note are, a) its not case-sensative (Yay!), b) you’ll need to include spaces & other types of formatting, c) you obviously can’t call more than one string at a time (Der!).

If you’re using templates and want to hardcode some text into the template files, BUT want to some text to be dynamic you can use the string_option() function. This function is basically the same as the shortcode functionality, however with this you can choose to ‘echo’ the string or to simply ‘return’ it so that it can be placing into a variable of your liking.

Again, you’ll need to get the ‘NAME’ of the string that you want to use. Within you PHP file you’ll need to place string_option(); into it where you either want the custom string to be displayed, or where you want to return the string too. This function will take two arguments, the first of the two is mandatory. This is the name of the string you want to get, this needs to be within quotes. The second argument is a true/false integer, which is optional, that decides if the custom string is to be ‘echoed’ or ‘returned’ via the function. 1 is echo, which is default and 0 is return.

If you wish to echo the telephone number you’ll use this:



string_option('telephone', 1);

if you want to return the string into a variable you’ll use this:

$telephone = string_option('telephone', 0);

You can download the plugin via WordPress plugins by searching ‘wp-strings’, or you can download the plugin from WordPress here. If you use the plugin and enjoy it, please either send feedback or donate, to keep the plugin up-to-date and new functionality coming!

Falkon blog post featured image for post - WordPress Templates

WordPress Templates

Ever wanted to customise a single page in WordPress with the template system but never knew how? Its simple with...