This article lists 10 Useful WordPress Hacks & WordPress Functions that you will find helpful when developing your own wordpress themes. A few of my personal favorite WordPress Tips in this list are Caching custom WordPress queries and Deluxe Blog Tips Meta box script for easy custom metabox creation.
1.) How to Remove the Width & Height Attributes From WP Image Uploader
When creating a responsive WordPress theme you may need to remove the default image height and width attributes that are automatically added by the WordPress media uploader.
Open your WordPress themes functions.php and paste the code block below into it.
add_filter( 'post_thumbnail_html', 'remove_width_attribute', 10 ); add_filter( 'image_send_to_editor', 'remove_width_attribute', 10 ); function remove_width_attribute( $html ) { $html = preg_replace( '/(width|height)="d*"s/', "", $html ); return $html; }
2.) Get WordPress Thumbnail URL
Paste the code block below into your WordPress post loop.
<?php $image_id = get_post_thumbnail_id(); $image_url = wp_get_attachment_image_src($image_id); $image_url = $image_url[0]; ?>
3.) Add extra contact methods to user profiles
Paste the Code below into your WordPress themes functions.php file.
add_filter('user_contactmethods', 'my_user_contactmethods'); function my_user_contactmethods($user_contactmethods){ $user_contactmethods['twitter'] = 'Twitter Username'; $user_contactmethods['facebook'] = 'Facebook Username'; return $user_contactmethods; }
4.) Use WordPress shortcodes in theme files
Add the code below to your WordPress theme file where you wish the shortcode to display, replace the instance of my_shortcode with your own shortcode name.
<?php echo do_shortcode("[my_shortcode]"); ?>
5.) Speed up your blog by caching custom queries
Paste the code block below and replace your existing custom query, replace line 5 with your own query parameters.
<?php // Get any existing copy of our transient data if ( false === ( $special_query_results = get_transient( 'special_query_results' ) ) ) { // It wasn't there, so regenerate the data and save the transient $special_query_results = new WP_Query( 'cat=5&order=random&tag=tech&post_meta_key=thumbnail' ); set_transient( 'special_query_results', $special_query_results ); } // Use the data like you would have normally... ?>
6.) How to display your latest Google+ update on your WordPress blog
Copy and paste the code below into your WordPress theme where you would like your latest Google+ update to display, remeber to change the ID in line 3 to your own Google+ profile ID.
<?php include_once(ABSPATH.WPINC.'/rss.php'); $googleplus = fetch_feed("http://plusfeed.appspot.com/103329092193061943712"); // Replace 103329092193061943712 by your own ID echo '<a href="'; echo $googleplus->items[0]['link']; echo '">'; echo $googleplus->items[0]['summary']; echo ''; ?>
7.) Get WordPress category ID using category name
Paste the code block below into your WordPress themes functions.php file.
function get_category_id($cat_name){ $term = get_term_by('name', $cat_name, 'category'); return $term->term_id; }
Once you saved the file, just call the function with your category name as a parameter. Example:
$category_ID = get_category_id('WordPress Tutorials');
8.) Include All WordPress Post Types in Search
This snippet will automatically include all public post types, that have not been excluded from searches in your search results. Paste the code block into your WordPress themes functions.php file.
function pippin_include_post_types_in_search($query) { if(is_search() && is_main_query()) { $post_types = get_post_types(array('public' => true, 'exclude_from_search' => false), 'objects'); $searchable_types = array(); if($post_types) { foreach( $post_types as $type) { $searchable_types[] = $type->name; } } $query->set('post_type', $searchable_types); } return $query; } add_action('pre_get_posts', 'pippin_include_post_types_in_search');
9.) Improve the WordPress except
We really should make that excerpt a bit better, we already know how to change it’s length, but that ‘…’ could be improved to be more accessible and we could also make it link to the full article.
Add the following code block to your WordPress theme functions.php. Un-comment the latter return (and comment out the former) if you want to add the link!
add_filter('the_excerpt', 'new_excerpt_hellip'); function new_excerpt_hellip($text) { return str_replace('[...]', '…[+]', $text); //return str_replace('[...]', '…<a href="'. get_permalink($post->ID) . '">[+]</a>', $text); }
10.) Add custom meta boxes to WordPress post types
To add custom meta boxes for your custom fields is a great way to simplify the user interface in WordPress.
Adding your own hasn’t been very easy for someone without very much php experience, but using Deluxe Blog Tips Meta box script makes it a whole lot easier.
Download it, and include it using the instructions on their website.
<?php $meta_boxes[] = array( 'id' => 'personal', // meta box id, unique per meta box 'title' => 'Personal Information', // meta box title 'pages' => array('post', 'page', 'album'), // post types, accept custom post types as well, default is array('post'); optional 'context' => 'normal', // where the meta box appear: normal (default), advanced, side; optional 'priority' => 'high', // order of meta box: high (default), low; optional 'fields' => array( // list of meta fields array( 'name' => 'Full name', // field name 'desc' => 'Format: Firstname Lastname', // field description, optional 'id' => $prefix . 'fname', // field id, i.e. the meta key 'type' => 'text', // text box 'std' => 'Anh Tran', // default value, optional 'validate_func' => 'check_name' // validate function, created below, inside RW_Meta_Box_Validate class ) ) ); // Add as many as you want by copy the arrays and naming them $meta_boxes[1], $meta_boxes[2] etc. foreach ($meta_boxes as $meta_box) { new RW_Meta_Box($meta_box); } ?>
Leave a Reply
You must be logged in to post a comment.