The Ultimate Developers Guide To WordPress Snippets

As developers we always get into situations where we run around for solutions. Here we have put together the most common snippets that we use regularly. These simple WordPress snippets will pump up your productivity almost instantly.

Remove auto generated <p> tags

Add the below code to functions.php file of your theme.

remove_filter( 'the_content', 'wpautop' );
// OR
remove_filter( 'the_excerpt', 'wpautop' ); 

Custom excerpt length for posts

Add the below code to functions.php file of your theme.

function custom_excerpt_length($length) {
    global $post;
    if ($post->post_type == 'post')
    return 30;
    else
    return 50;
    }
    add_filter('excerpt_length', 'custom_excerpt_length');

Adding a Custom Post Type

Add the below code to functions.php file of your theme.


function my_custom_post_yourpostname() {
//labels array 
   $labels = array(
    'name'               => _x( 'YourPostName', 'post type general name' ),
    'singular_name'      => _x( 'YourPostName', 'post type singular name' ),
    'add_new'            => _x( 'Add New', 'YourPostName' ),
    'add_new_item'       => __( 'Add New YourPostName' ),
    'edit_item'          => __( 'Edit YourPostName' ),
    'new_item'           => __( 'New YourPostName' ),
    'all_items'          => __( 'All YourPostName' ),
    'view_item'          => __( 'View YourPostName' ),
    'search_items'       => __( 'Search YourPostNames' ),
    'not_found'          => __( 'No YourPostNames found' ),
    'not_found_in_trash' => __( 'No YourPostNames found in the Trash' ),
    'parent_item_colon'  => '',
    'menu_name'          => 'YourPostNames',
    'register_meta_box_cb' => 'wpt_add_yourpostname_metaboxes'
  );
// args array
   $args = array(
    'labels'        => $labels,
    'description'   => 'Displays YourPostName',
    'public'        => true,
    'menu_position' => 4,
    'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments','custom-fields' ),
    'has_archive'   => true,
    'capability_type'     => 'page',
  );
  register_post_type( 'yourpostname', $args );
}
add_action( 'init', 'my_custom_post_yourpostname' );

Generate theme path

file path to current Theme directory

get_stylesheet_directory()
url path to current Theme directory
get_stylesheet_directory_uri() 
file path to parent Theme directory
get_template_directory() 
url path to parent Theme directory
get_template_directory_uri() 

Add WOW effect (wow.js) to your theme

Add the below code to functions.php file of your theme.


add_action( 'wp_enqueue_scripts', 'sgems_enqueue_scripts' );
function sgems_enqueue_scripts() {
    wp_enqueue_style( 'animate', get_stylesheet_directory_uri() . '/css/animate.css' );
    wp_enqueue_script( 'wow', get_stylesheet_directory_uri() . '/js/wow.js', array(), '', true );
    wp_add_inline_script( 'wow', 'new WOW().init();' );
}

One thought on “The Ultimate Developers Guide To WordPress Snippets

Leave a Reply

Your email address will not be published. Required fields are marked *

Enquire Now
Share this
Send this to a friend