remove links from images using functions.php

add_filter( ‘the_content’, ‘attachment_image_link_remove_filter’ ); function attachment_image_link_remove_filter( $content ) { $content = preg_replace( array(‘{<a(.*?)(wp-att|wp-content\/uploads)[^>]*><img}’, ‘{ wp-image-[0-9]*” /></a>}’), array(‘<img’,'” />’), $content ); return $content; } The regex could be simpler and unfortunately this also deprives you of the unique wp-image-xxx (where xxx is the attachment ID) class of the <img> tag, but it’s the safest one I … Read more

How to redirect to post if search results only returns one post

Add this snippet to your functions.php function redirect_the_single_post() { if (is_search() && is_main_query()) { global $wp_query; if ($wp_query->post_count == 1 && $wp_query->max_num_pages == 1) { wp_redirect( get_permalink( $wp_query->posts[‘0’]->ID ) ); exit; } } } add_action(‘template_redirect’, ‘redirect_the_single_post’ ); hope this will help you!!

Display random categories on the front page (Finding and Editing Theme Functions)

The real question here is: How do I find “TheirCode” which is responsible for this selection using tools such as firefox dev bar and the actual source? If you are referring to the HTML output/source, then for example on the official Storefront theme demo site, just right-click on the “Product Categories” heading or section and … Read more

Check if post is being published for the first time, or is an already published post being updated

Hook into edit_post to catch changes. And take a look at wp_transition_post_status() which is called on inserts and updates: function wp_transition_post_status($new_status, $old_status, $post) { do_action(‘transition_post_status’, $new_status, $old_status, $post); do_action(“{$old_status}_to_{$new_status}”, $post); do_action(“{$new_status}_{$post->post_type}”, $post->ID, $post); } On publish you hook into draft_to_publish, pending_to_publish and auto-draft_to_publish. For edits hook into publish_to_publish. Example A mini plugin that notifies all … Read more

Define page template in wp_insert_post

From the wp_insert_post() documentation, the page_template argument reads as follow: page_template: If post_type is ‘page’, will attempt to set the page template. On failure, the function will return either a WP_Error or 0, and stop before the final actions are called. If the post_type is not ‘page’, the parameter is ignored. You can set the … Read more

Custom page with variables in url. Nice url with add_rewrite_rule

I think the add_rewrite_tag() is not needed, and can be replaced with adding the variables to the public query vars directly: // Either directly (in your init hook): $wp->add_query_var( ‘var1’ ); $wp->add_query_var( ‘var2’ ); // Or via a filter: add_filter( ‘query_vars’, ‘wpse12965_query_vars’ ); function wpse12965_query_vars( $query_vars ) { $query_vars[] = ‘var1’; $query_vars[] = ‘var2’; return … Read more

Memorizing syntax

Not everyone will follow convention, so you can be assured if you are copy-pasting then you are getting a mix-and-match approach from people who do it “right” and do it “wrong” and sometimes the difference between right and wrong is a matter of opinion, lets not forget. Also this applies to NOT only Syntax Style … Read more

Include files in child theme functions file

Child themes reference parent themes by directory name, and in a normal install all your themes live in wp-content/themes/, so I’d say it’s fine to reference those themes by their relative path: include ‘../parent-theme/some-file.php’; If that makes you uncomfortable, I observe the following constants in WordPress 3.0.1 with a twentyten child theme called tt-child: TEMPLATEPATH … Read more