You are currently browsing the archives for the wp-snippets category


paged query in wordpress

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
              query_posts('paged='.$paged);

contact form 7 controlling br tags

Have you encountered br tags messing your contact form 7 forms. you can remove it using below code in wp-config.php:

define( 'WPCF7_AUTOP', false );

more info at http://contactform7.com/controlling-behavior-by-setting-constants/

Posting to a wordpress page from a form

Ever tried to post a form to wordpress page? sometimes you might have encountered 404 error. this is due to reserved name for worpress like “name”. You should not have input with name “name” or else you will get 404 error after submission.
for more details go to : http://www.cabeeb.com/2009/09/posting-a-form-to-a-wordpress-page/

wp nav menu undocumented paramater

‘items_wrap’ => ‘<ul id="%1$s">%3$s</ul>

This is executed by: $nav_menu .= sprintf( $args->items_wrap, esc_attr( $wrap_id ), esc_attr( $wrap_class ), $items );

you can play around with the sprintf arguments

ex: ‘items_wrap’ => ‘%3$s’ would remove the wrapping <ul> tag

Get top parent page id in wordpress.

<?php
function getTopParentPageId($page_id){
 $mypage = get_page($page_id);
 if ($mypage->post_parent == 0){ 
   return $mypage->ID; 
 }
 else{ 
  return getTopParentPageId($mypage->post_parent); 
 }
}
?>

SEO friendly timthumb on wordpress

Many of you love using timthumb in your wordpress theme but don’t use it because it does not generates seo friendly urls for images. I was asked by a client to make timthumb urls seo friendly and here is how I did that.

First open your wordpress site .htaccess file. Generally it is located in root of your wordpress installation.

It should look like this:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Now create new folder in your root call it let’s say images and place cache folder and timthumb in there then edit your .htaccess file as below

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(images) [NC]
RewriteRule . /index.php [L]
RewriteRule ^images/(.*)x(.*)/r/(.*) images/timthumb.php?src=http://example.com/$3&amp;w=$1&amp;h=$2&amp;zc=1&amp;q=100&amp;a=tl
</IfModule>

# END WordPress

Replace example.com with you site name:
Now upload htaccess file and where you need to use timthumb do as below

<?php
if (has_post_thumbnail()):
  $thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id(), 'large');
  echo '&lt;a href="'.get_permalink().'"&gt;&lt;img src="'.get_bloginfo('url').'/images/715x440/r/'.str_replace(array('http://example.com/', 'http://www.example.com/'), array('',''), $thumbnail[0]).'" width="715" height="440" alt="'.get_the_title().'" /&gt;&lt;/a&gt;';
endif;
?>

That should do the trick for making timthumb url seo friendly.

Get menu name from menu location

<?php
function wpexpo_get_theme_menu_name( $theme_location ) {
	if( ! $theme_location ) return false;

	$theme_locations = get_nav_menu_locations();
	if( ! isset( $theme_locations[$theme_location] ) ) return false;

	$menu_obj = get_term( $theme_locations[$theme_location], 'nav_menu' );
	if( ! $menu_obj ) $menu_obj = false;
	if( ! isset( $menu_obj-&gt;name ) ) return false;

	return $menu_obj-&gt;name;
}
?>

Get category id in category.php

<?php
$category_id = get_cat_ID(single_cat_title("",false));
?>

Getting post thumbnail

<?php
if (has_post_thumbnail()):
 $thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id(), 'large');
 echo '<a href="'.get_permalink().'"><img src="'.get_bloginfo('template_url').'/timthumb.php? src='.$thumbnail[0].'&amp;amp;w=715&amp;amp;zc=1&amp;amp;q=95&amp;a=tl" width="715" alt="'.get_the_title().'" /></a>';
 endif;
?>
 <?php
                            if (has_post_thumbnail()):
                                $thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id(), 'large');
                            ?>
                            <a href="<?php echo $thumbnail[0];?>" class="modal"><img src="<?php bloginfo('template_url');?>/timthumb.php?src=<?php echo $thumbnail[0];?>&amp;w=218&amp;h=135&amp;zc=1&amp;q=95&amp;a=tl" class="frame_img" /></a>
                            <?php endif;?>

Tags