code snippets

Code Snippets for Easy Site Customizations

Code snippets are small bits of PHP code that you can use to customize your WordPress website.  The Code Snippets plugin is a great convenience for adding snippets. The plugin is available for free from the WordPress plugin directory. It is regularly updated, is widely used, and has good reviews.  I’ve been using it for a few years. I’ve found that the developer treats the plugin as a labor of love and takes good care of it.

 

code snippets plugin

After you install and activate the plugin, there is a UI for adding code snippets. When you add the PHP code, you don’t add the <?php tags.  The input area is actually a code editor. After you add the title, snippet itself, and a description, you choose whether it is to be run on the backend admin area, the front of the site, both, or just once.

code snippets user interface

Some things to keep in mind when using snippets:

  • If you enter some bad PHP code you could end up breaking your site. There are some tips of how to recover in the support section for the plugin on WordPress.org.
  • The snippet interface is only available for users with the Admin role. If you have Admin users who you don’t want to have access to the snippets then you need to find a way to restrict the menu access.
  • Be sure to look over any code you add to your site to make sure it is doing what you think it is.

Here are a few snippets that I’ve used:

Limit Revisions:

This is something you might normally set in the wp-config.php file.

define( 'WP_POST_REVISIONS', 3 );

Add Plugins Link to Toolbar Menu:

You could use this same logic to add other menu items. I never understood why there was a link to ‘Themes’ but not ‘Plugins’.

add_action( 'admin_bar_menu', 'add_link_to_admin_bar',999 );

function add_link_to_admin_bar($wp_admin_bar) {
    $wp_admin_bar->add_node( array(
        'parent' => 'site-name',
        'id' => 'plugins',
        'title' => 'Plugins',
        'href' => esc_url( admin_url( 'plugins.php' ) ),
        'meta' => false
    ));
}

Customize Login Page:

I’m using a child theme, and put a logo in the ‘images’ folder.

function my_login_logo() { ?>
<style type="text/css">
    body.login div#login h1 a {
    background-image: url(<?php echo get_stylesheet_directory_uri(); ?>/images/knight-logo-trans.png);
    padding-bottom: 20px;
}
</style>
<?php }
add_action( 'login_enqueue_scripts', 'my_login_logo' );

function my_login_logo_url() {
    return home_url();
}
add_filter( 'login_headerurl', 'my_login_logo_url' );

function my_login_logo_url_title() {
    return 'Albert Einstein - knowledge is an activity';
}
add_filter( 'login_headertitle', 'my_login_logo_url_title' );

Add Login / Logout Menu Item:

To make this work, you need to look in the theme functions file and figure out what the theme named the menu area. This is the code for the header menu for the Astra theme.

add_filter( 'wp_nav_menu_items', 'add_loginout_link', 10, 2 );
function add_loginout_link( $items, $args ) {
   if (is_user_logged_in() && $args->theme_location == 'primary') {
      $items .= '<li class="menu-item"><a href="'. wp_logout_url() .'">Log Out</a></li>';
   }
   elseif (!is_user_logged_in() && $args->theme_location == 'primary') {
      $items .= '<li class="menu-item"><a href="'. site_url('wp-login.php') .'">Log In</a></li>';
  }
  return $items;
}

There are a number of sites around the net that have some useful code snippets. Here are a few links:

Using the Code Snippets plugin is a convenience.  Keep in mind that there are two other ways you can apply snippets.  You can add them to your child theme’s functions.php file or you can create a plugin.  Depending on your needs, one of those options might work better for you.

Hope you found the article useful.