Creating an Editorial Comment Shortcode

You can use the if_user_can() function to create a shortcode for notes that only editors and administrators can see, as shown in Listing 9-20.

Listing 9-20. An editorial comment shortcode

// Usage: [ed]this is a note only editors can read.[/ed]

function editorial_note($content = null) {

if (current_user_can('edit_pages') && is_single())

return '<span class="private">'.$content.'</span>'; else return '';

In this case, there are no attributes to worry about. The only argument for your shortcode function is the enclosed content, which you'll assume is empty by default. In addition to checking for the appropriate capability ('edit_pages', in this case), you've decided that these notes should appear only on single pages and post archives, so you've added a check for the is_singular() conditional. If the user has the right capability and you're on the right kind of page, you'll wrap the enclosed content in a span (so you can style it later, if you want to) and return it.

See the Dashboard Notepad widget in Chapter 8 for more examples with if_user_can().

0 0

Post a comment