WordPress Checklist – WP Functions

Key Points

Always ALWAYS ALWAYS leave comments in your code

Before writing a function you should have an understanding of what you want to return. Writing down comments in your code will help you plan out and create a successful function. This will help other developers understand your thinking behind the outcome if a change is required.

Your functions should tell a story:

  1. An Introduction – The purpose of the function
  2. The Plot – How to get the result
  3. A Twist- The fallback if your result returns empty
  4. Happy ever after – successfully returning a result.

Make code reusable

If you are a developer your time will be precious on a project, don’t waste it.

Its pointless writing tailored functions for every site, keep in mind you are most likely to come across the same use for it again.

Separate reusable code and tailored

A function file can get extremely long depending on the project, Its good practice to keep your reusable functions separate from tailored functions.

I can be used again - copy me
//////////
I wont

Remove functions you dont need

Bin them.

 

Useful custom functions and hooks

Below are some useful functions that I tend to use in my of my themes.  If you wish to use any, copy each snippet of code and paste it into your functions.php file. Note you dont have to use any of the below.

Base URL and Theme calls

You should NEVER  hardcode a URL for a href or internal permalink. You should use the get_bloginfo(); function or use the below code which i find quite cleaner and easy to remember. Simply copy the below and change the name to your preference.


// Cleaner theme calls...and easier to remember

// Your site URL. eg. http://www.johnburns87.com
define('BASE_URL',get_bloginfo('url'));

// Theme URL. eg. http://www.johnburns87.com/wp-content/themes/twentyten/
define('THEME_URL',get_bloginfo('template_url'));

// Get the URL of the current page
define('CURRENT_PATH','http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);

To use it in a theme template echo the value eg:

echo THEME_URL;

Remove html items from wp_head()

The below code stips out needless meta and html from your page if you use the wp_head() function in header.php

// Display the links to the extra feeds such as category feeds
remove_action('wp_head', 'feed_links_extra', 3); 

// Display the links to the general feeds: Post and Comment Feed
remove_action('wp_head', 'feed_links', 2);

// Display the link to the Really Simple Discovery service endpoint, EditURI link
remove_action('wp_head', 'rsd_link');

// Display the link to the Windows Live Writer manifest file.
remove_action('wp_head', 'wlwmanifest_link');

// index link
remove_action('wp_head', 'index_rel_link');

// prev link
remove_action('wp_head', 'parent_post_rel_link');

// Display the XHTML generator that is generated on the wp_head hook, WP version
remove_action('wp_head', 'wp_generator'); 

// Other crap!
remove_action('wp_head', 'rel_canonical');
remove_action('wp_head', 'previous_post_link');
remove_action('wp_head', 'next_post_link');
remove_action('wp_head', 'post_permalink');
remove_action('wp_head', 'the_permalink');
remove_action('wp_head', 'start_post_rel_link', 10, 0 );
remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);

Prevent the use of the file editor section within the CMS

define('DISALLOW_FILE_EDIT', true);

Disable WordPress Admin Bar for all users but admins

// Disable WordPress Admin Bar for all users but admins.
show_admin_bar(false);

Modify Editor to accept CSS classes

################################################
#
#  Add CSS classes to content in the Editor
#
################################################

add_filter('tiny_mce_before_init', 'add_custom_classes');

function add_custom_classes($initArray) {

 $initArray['theme_advanced_styles'] = "Banner Item=page-title banner__item,Section title=section-title,Bold=section-title__highlight";

 $initArray['theme_advanced_buttons2_add_before'] = 'styleselect';

 return $initArray;

}

Remove WP Upgrade message

Remove annoying yet risky WordPress update messages from the CMS

################################################
#
#  Remove WP Upgrade message
#
################################################

add_action('admin_menu','wphidenag');

function wphidenag() {

    remove_action( 'admin_notices', 'update_nag', 3 );

}

SVG Access

Allow the ability to upload SCG images into the CMS

################################################
#
#  Allow SVG uploads
#
################################################

function cc_mime_types( $mimes ){

    $mimes['svg'] = 'image/svg+xml';

    return $mimes;

}

add_filter( 'upload_mimes', 'cc_mime_types' );

Clean POST and GET values

################################################
#
#  Use $_CLEAN instead of $_POST and $_GET
#
################################################

function clean($elem) 

{ 

    if(!is_array($elem)) 

        $elem = htmlentities($elem,ENT_QUOTES,"UTF-8"); 

    else 

        foreach ($elem as $key => $value) 

            $elem[$key] = $this->clean($value); 

    return $elem; 

}

Disable user comments in CMS

A way to stop annoying spammers

################################################
#
#  Remove Comments section from CMS
#
################################################

// Removes from admin menu
add_action( 'admin_menu', 'my_remove_admin_menus' );

function my_remove_admin_menus() {

    remove_menu_page( 'edit-comments.php' );

}

// Removes from post and pages
add_action('init', 'remove_comment_support', 100);

function remove_comment_support() {

    remove_post_type_support( 'post', 'comments' );

    remove_post_type_support( 'page', 'comments' );

}

// Removes from admin bar
function mytheme_admin_bar_render() {

    global $wp_admin_bar;

    $wp_admin_bar->remove_menu('comments');

}

add_action( 'wp_before_admin_bar_render', 'mytheme_admin_bar_render' );

Create MYSQL table

Create MYSQL tables via your functions file :)

################################################
#
#  Create schema - MYSQL newsletter table
#
################################################

function create_newsletter_signup_table(){

    global $wpdb;

    $table_name = $wpdb->prefix . "newsletter_signups";

    $create_table_sql = "CREATE TABLE $table_name (

        id INT(11) NOT NULL AUTO_INCREMENT,

        user_email VARCHAR(255) NOT NULL,

        user_time INT(11) NOT NULL,

        user_ip VARCHAR(255) NOT NULL,

        UNIQUE KEY id (id)

    );";

    require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );

    dbDelta($create_table_sql);

}

create_newsletter_signup_table();

Get First sentence from string

Handy if you only want the first sentence from a post

################################################
#
#  Get First sentence from string
#
################################################

function first_sentence($content) {

    $content = html_entity_decode(strip_tags($content));

    $pos = strpos($content, '.');

    if($pos === false) {

        return $content;

    }

    else {

        return substr($content, 0, $pos+1);

    }

}

Example:

$content = get_the_content($post->ID);

echo first_sentence($content);