Friday, March 29, 2024

A Few Essential WordPress Skills to Better Manage your Website

A Few Essential WordPress Skills to Better Manage your Website

Since its initial launch in 2003, WordPress has grown into one of the most popular open source content publishing systems around. Maybe that’s because, even with hardly any knowledge of CSS, PHP, HTML and JavaScript, WordPress can help you create an impressive site. Having said that, to achieve a more interactive and dynamic website, you will need to hone a few useful proficiencies. Having worked with WordPress for a number of years now, I have a few specific WordPress skills to suggest. In today’s article, I’ll present some of my top picks.

PHP Coding

It isn’t immediately obvious from the management pages, but all of WordPress is written in PHP. Some of these files are part of the core that should never be tampered with, while others are there to be modified to better customize your site. WordPress contains two built-in editors that allow you to edit Theme files directly from within WordPress admin. They are the Theme Editor and Plugin Editor. The Theme Editor is accessible from the Administration > Appearance > Editor menu, while the Plugin Editor is located at Administration > Plugins > Editor.

In my experience, most edits take place in the functions.php file, located in the Themes folder. It’s a special WordPress Plugin that adds features and functionality to a WordPress site. You can use it to call both PHP and built-in WordPress functions, as well as define your own functions.

functions_php_file.jpg

Here is some example code that redirects to another page if the current user may perform a particular action. Note that the current_user_can(), wp_redirect(), and mm_corepage_link() functions are WordPress functions, while stripos() and array() are native to PHP:

if ( !current_user_can('manage_options') ) {
  function profile_redirect() {
      $result = stripos($_SERVER['REQUEST_URI'], 'profile.php');
      if ($result !== false) {
           wp_redirect( mm_corepage_link( array("type"=>"myaccount") ) );
      }
      else if ( stripos($_SERVER['REQUEST_URI'], 'index.php') !== false) {
           wp_redirect( mm_corepage_link( array("type"=>"homepage") ) );
      }
  }
}

Familiarize Yourself with WordPress Action and Filter Hooks

While learning PHP is certainly a great start, you won’t get very far in WordPress until you can link your functions to WordPress. This is accomplished using hooks. In WordPress, a Hook is a function where you can add your own code or change what WordPress is doing or outputting by default. The “doing” and “outputting” map to two types of hooks called actions and filters respectively. An Action in WordPress is a hook that is triggered at specific time when WordPress is running and lets you take an action. This can include things like adding a menu item when WordPress is initializing or preforming some sort of validation when someone publishes a post. A Filter in WordPress allows you get and modify WordPress data before it is sent to the database or the browser. Some examples of filters would include customizing how excerpts are displayed or adding some custom code to the beginning or end of a blog post.

Adding an Action Hook

To “hook” into a WordPress action, you call the add_action() function, passing in the hook name and function name. The add_action() function also accepts a couple of optional arguments. The priority is an integer value based on a scale of 1 to 999 that determines the priority of order for functions tied to that specific hook. A higher priority means it will run later, while a lower priority means it will run earlier. The $accepted_args parameter is used to pass multiple arguments to your function.

add_action( $hook, $function_to_add, $priority, $accepted_args );

Here is the call to add_action() that invokes our profile_redirect() function when WordPress performs the ‘admin_menu’ action. This action is used to add extra submenus and menu options to the admin panel’s menu structure and runs after the basic admin panel menu structure is in place.

add_action('admin_menu', 'profile_redirect');

Adding a Filter Hook

The add_filter() function accepts the same arguments as add_action(), but filters WordPress output instead. As such, your function both receives a value and has to return it at the end of the function. Actions, on the other hand, simply run the code they need to and don’t return a value.

add_filter( $tag, $function_to_add, $priority, $accepted_args );

Here is some code that adds a “Duplicate” link to items on the list table using the “page_row_actions” filter. It passes an array of row action links to our “duplicate_post_link” function, along with the Post object. Default row action links include ‘Edit’, ‘Quick Edit’, ‘Restore, ‘Trash’, ‘Delete Permanently’, ‘Preview’, and ‘View’.

function duplicate_post_link( $actions, $post ) {
        if ($post->post_type == 'menu') {
                $actions['duplicate'] = '<a href="admin.php?action=rd_duplicate_post_as_draft&post='
                          . $post->ID
                          . '" title="Duplicate" rel="permalink">Duplicate</a>';
        }
        return $actions;
}
add_filter( 'page_row_actions', 'duplicate_post_link', 10, 2 );

Here is the result in the list table:

duplicate_link.jpg

Conclusion

These two essential skills are only the start. I’ll be describing more skills in the coming months. In the meantime, you can learn more about WordPress Action Hooks here and Filters here.

Rob Gravelle
Rob Gravelle
Rob Gravelle resides in Ottawa, Canada, and has been an IT guru for over 20 years. In that time, Rob has built systems for intelligence-related organizations such as Canada Border Services and various commercial businesses. In his spare time, Rob has become an accomplished music artist with several CDs and digital releases to his credit.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Popular Articles

Featured