• 7 WordPress Hacks You Might Find Useful For Your Blog

    Advertisement

    WordPress comes with many useful features but depending on your needs you can make your wordpress blog more functional and dynamic.This can be done by adding piece of codes in your theme files.
    Wordpress snippets allow you to change or add many functions to your blog.Thankfully,there are many people out there releasing new wordpress hacks.In today’s post,we are featuring wordpress code hacks which you might find useful for your blog.But before customizing your theme files,do not forget to backup your files,especially functions.php file.

    Keep WordPress from Compressing JPGs

    Add the following code into your theme’s function.php file

    add_filter('jpeg_quality', function($arg){return 100;});

    Source

     

    Embed a Page inside a Page

    Paste the code below within the loop. Make sure to update page ID on line 1

    <?php $recent = new WP_Query("page_id=**ID**"); while($recent->have_posts()) : $recent->the_post();?>
           <h3><?php the_title(); ?></h3>
           <?php the_content(); ?>
    <?php endwhile; ?>

    Source

     

    Increase the Excerpt Field Height

    Simply paste the following code into your functions.php file. Height can be adjusted on line 5.

    add_action('admin_head', 'excerpt_textarea_height');
    function excerpt_textarea_height() {
        echo'
        <style type="text/css">
            #excerpt{ height:500px; }
        </style>
        ';
    }

    Source

     

    Add search form to specific wp_nav_menu

    Add this snippet into your theme’s functions.php file to add a search form to your wordpress wp_nav_menu.

    add_filter('wp_nav_menu_items', 'add_search_form', 10, 2);
    function add_search_form($items, $args) {
    if( $args->theme_location == 'MENU-NAME' )
            $items .= '<li class="search"><form role="search" method="get" id="searchform" action="'.home_url( '/' ).'"><input type="text" value="search" name="s" id="s" /><input type="submit" id="searchsubmit" value="'. esc_attr__('Search') .'" /></form></li>';
            return $items;
    }

    Source

     

    Add custom post types to archives page

    Add this snippet into functions.php file.It will let you add custom post types to archives page

    function add_custom_types_archive( $query ) {
    if( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
    $query->set( 'post_type', array(
     'post', 'your-custom-post-type-here'
                ));
          return $query;
        }
    }
    add_filter( 'pre_get_posts', 'add_custom_types_archive' );

    Source

     

    Exclude category from homepage

    To exclude some categories from the the loop on the frontage, you can use this snippet in your function.php.3, 5, and 23 are the IDs of the categories and the – makes them go away

    <?php
    function excludeCat($query) {
      if ( $query->is_home ) {
        $query->set('cat', '-3,-5,-23');
      }
      return $query;
    }
    add_filter('pre_get_posts', 'excludeCat');
    ?>


    Source

     

    Automatically Set the Featured Image in WordPress

    Add the following code into your functions.php file.The function will set the first image as featured image.

    function autoset_featured() {
              global $post;
              $already_has_thumb = has_post_thumbnail($post->ID);
                  if (!$already_has_thumb)  {
                  $attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );
                              if ($attached_image) {
                                    foreach ($attached_image as $attachment_id => $attachment) {
                                    set_post_thumbnail($post->ID, $attachment_id);
                                    }
                               }
                            }
          }  //end function
    add_action('the_post', 'autoset_featured');
    add_action('save_post', 'autoset_featured');
    add_action('draft_to_publish', 'autoset_featured');
    add_action('new_to_publish', 'autoset_featured');
    add_action('pending_to_publish', 'autoset_featured');
    add_action('future_to_publish', 'autoset_featured');


    Source

    2 Responses to “7 WordPress Hacks You Might Find Useful For Your Blog”
    1. Good WordPress Tips :)
      Most likely I’ll need these someday.
      Thanks.

    2. Kai

      Nice tips, especially the Jpeg no compression code is new for me and really useful. Thanks.