WordPress Development: Creating Top-Level Menu Items

Published on Mar 25, 2009   //  Development, WordPress

WordPress Development

Previously we talked about adding an options page, but today we’ll be talking about how to create top-level menu items.

At some point you may need to create a complex plugin with quite a few options. You may have too many options to fit on just one page under Settings. If you come across this situation, you should consider create a dedicated top-level menu item for your plugin. This way you can organize your many options in to separate pages all under one group. This will cause less confusion for the end-user.

Adding a top-level menu is fairly simple, and has some of the same principles of adding an options page. The function we use to add a top-level menu is add_menu_page(). The syntax is as follows:

[code='php']add_menu_page(page_title, menu_title, capability, file, [function], [icon_url]);[/code]

  • page_title – The title that will appear on the options page
  • menu_title – The title of the menu item
  • capability – The capability required to view this options page (usually manage_options is used)
  • file – The file the function can be found in. Use __FILE__ for the current file
  • [function] – Optional. The function to create the options page
  • [icon_url] – Optional. In WordPress 2.7 an icon is shown beside top-level menu items. You can define a custom one here (use the full URL to it, try to keep the icon in the plugin’s folder)

Remember that we still have to wrap this function inside of a function and hook into admin_menu. Let’s look at an example (using a fictional Polls plugin as :

[code='php']add_action('admin_menu', 'mgf_add_menu');
function mgf_add_menu() {
add_menu_page('Polls Options', 'Polls', 'manage_options', __FILE__, '', plugins_url('polls/menu-icon.png'));
}[/code]

Then there you have it, your own menu item. If you’re wondering, plugins_url() will give you the URL to the plugins directory appended by any path you give it.

Next week we’ll be going over how to add submenus to this custom top-level menu items, and also to the default top-level menu items (other than Settings).

WordPress Wednesday: Page Management

Published on Mar 25, 2009   //  WordPress

If you use WordPress as a blogging platform, you probably focus much more energy on the posts of your blog. This is your core content and this is why readers would want to come by and read what you have to say. The pages almost serve as a secondary role, because they’re there for background information. As part of this supporting role, they can aim to inform readers about who you are, how much you charge for advertising, what are some of the most popular posts, what is your contact information, and so forth. If you use WordPress as a CMS for a static site, the pages can take up on much more primary role.

In either situation, you should not forget about the importance of your pages. They shouldn’t be cast off and left to remain stagnant, because they still play a vital role in what your blog or website has to offer. By default, WordPress will list your pages in alphabetical order (based on the page slug) either across the top of the site or on your sidebar. This may not necessarily be the best order from a logical standpoint and you may not want to include every page in that listing. Your privacy policy, for instance, may be better suited for the footer. That’s the configuration I have on Beyond the Rhetoric, because it’s not really necessary to feature the privacy policy so prominently.

While you could write some custom code to handle this kind of customization, it’s probably a lot simpler and more user-friendly to use a plug-in. Speaking to this specific need is the PageMash Page Management plug-in from Joel Starnes. In a nutshell, it gives you a drag-and-drop GUI interface within the WordPress control panel to move the order of your pages around and block out the ones that you don’t want to be immediately visible. It’s simple and effective.

For more information, including a link to the installation file, check out the page on WordPress.org.

Community Poll

Published on Mar 25, 2009   //  Polls

Weekly Community Poll

Last week we asked how many times do you exercise per week and 39% said they don’t. This weeks question is…

 

{democracy:74}

Everything PHP: Constants and Globals

Published on Mar 24, 2009   //  Development
Off

Everything PHP

We have previously covered variables, which allow you to store and change data, and today we’re going to be talking about something related: global variables and constants.

Global Variables

When you create a variable, it is only available within the same scope. Which means that when you declare a variable in your script, that variable will only be available within the same scope. The scope is hard to example without examples, so let’s look at a few.

[code='php']$a = 26;
echo $a;[/code]

This works because $a is within the scope of echo.

[code='php']$a = 26;
function test_example() {
echo $a;
}
test_example();[/code]

This will not work because you’re trying to get $a from a different scope. Additionally, any variables declared within a function will not be available (or modify any) outside of that function’s scope. Basically outside of a function variables are in the global scope, and inside of a function, variables are in the local scope.

But, what if we want to use a variable from a different scope? Well, that’s where global variables come in to play. We can take a variable from a different scope and bring it into the current scope. This is done by declaring the variable(s) we want as a global. Let’s look at our last example again, but this time let’s make it work.

[code='php']$a = 26;
function test_example() {
global $a;
echo $a;
}
test_example();[/code]

Now that we have globaled the variable $a in the local scope, we now are free to use it and modify it (which will modify globally). We can global multiple variables in the same statement by separating them by commas. Alternatively to globalling a variable, we can use the form $GLOBALS['a'] in place of $a in our local scope.

Constants

A constant is similar to a variable, in that stores a value for retrieval later in your script, however there are a couple of major differences. First of all, once the initial value of a constant is set (when it’s defined), the value cannot be changed (hence the name constant). Also, constants are automatically available as a global. Constants are defined and referenced in a different way than variables. Let’s look at an example.

[code='php']define('THE_CONSTANT', 'bacon');
echo THE_CONSTANT;[/code]

In the define statement, the first parameter is the name of the constant (should always be uppercase, must start with a letter or underscore and may contain letters, numbers and underscores) and the second parameter is the value of the constant (can be a boolean, integer, float or string). That constant is now defined, available globally and it’s value cannot be changed. It is not possible to undefine a constant.

When referencing a constant in a statement, you can simply use the constant name as it was defined. If you do not know the name of the constant (such as if the name is stored in a variable), you can use the function constant() to reference it (ex: echo constant(‘THE_CONSTANT’);).

If you need to check if a constant has been defined, you can use the defined() function in an if statement (it will return true or false).

Conclusion

That finalizes our post on global variables and constants.

Business 101: Fashion and Function

Published on Mar 24, 2009   //  Business Topics

As much as we would like to believe that function trumps fashion, this isn’t always the case when it comes to business and selling your various products and services. People make their first impressions based on what they see and they can extrapolate all sorts of hidden meanings based on this initial impression.

Let’s say, for example, that you run a high-tech company of some kind. You invite a client over to your office for a brief meeting, escorting them over to your conference room. You’ve set up a PowerPoint presentation to run through some of your key points, but when the meeting begins, you discover that your ancient projector isn’t working properly. Clients expect a high-tech company to use state-of-the-art equipment and your 10+ year old projector projects the wrong kind of image.

Similarly, let’s say you are a freelance graphic designer and you decide to meet at your potential client’s office to show off some of the designs that you have created. By and large, graphic designers will make use of an Apple MacBook and most companies will expect as much. If you show up with an industrial-strength Panasonic ToughBook instead, you may be giving off the wrong kind of image. Does this mean that you will be ineffective as a designer? Of course not, but you could be swaying your client in a certain direction.

Appearances mean a lot. In order for your customers, business associates, and other acquaintances to truly appreciate the function side of your products, you may have to first acquaint them with the fashion side.

Surrey Meet & Tweet

Published on Mar 24, 2009   //  Events
Off

After our own failed attempt at a Fraser Valley Tweetup last month we have a new group of business people who are twittering that are trying to muster up another Tweetup in April. The speaker for this event is none other then Miss604 the local blogging celebrity. Rebecca will share her knowledge on using Twitter and how it can help you improve your business.

If you are not familiar with Twitter this event will be good for you too so you can see the power of social media.

When is the event happening?
Thursday, April 16, 2009 5:00 PM - 8:00 PM

Where is it happening?
Dublin Crossing - 18789 Fraser Highway - Surrey

Registration
To register for this event RSVP here.

Page 3 of 1212345...10...Last »