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();' );
}

1 Comment
April 10, 2018

Nice blog & thanks for sharing it.

Leave a Reply

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

See More Blogs

Contact us

Partner with Us for Comprehensive IT Solutions

We’re happy to answer any questions you may have and help you determine IT solutions to shape up your idea or streamline your processes.

Your benefits:
What happens next?
1

We Schedule a call at your convenience 

2

We do a discovery and consulting meeting 

3

We prepare a proposal 

Schedule a Free Consultation