you are here: codestackercodes [RSS]

disable post revisions

in your /wp-config.php

<?php

define('WP_POST_REVISIONS', false);

?>

delete post revisions

DELETE a,b,c  
FROM wp_posts a  
LEFT JOIN wp_term_relationships b ON (a.ID = b.object_id)  
LEFT JOIN wp_postmeta c ON (a.ID = c.post_id)  
WHERE a.post_type = 'revision'

page x of y in wordpress

based on http://design.sparklette.net/teaches/how-to-add-wordpress-pagination-without-a-plugin/

<?php

// paste in functions.php

function pagination($pages = '', $range = 4){
     $showitems = ($range * 2)+1;  

     global $paged;
     if(empty($paged)) $paged = 1;

     if($pages == '') {
         global $wp_query;
         $pages = $wp_query->max_num_pages;
         if(!$pages) {
             $pages = 1;
         }
     }   

     if(1 != $pages) {
         echo "Page ".$paged." of ".$pages;
     }
}

?>



<?php

// in your template 

if (function_exists("pagination")) { pagination($additional_loop->max_num_pages); } 

?>

embed youtube using html5

via http://apiblog.youtube.com/2010/07/new-way-to-embed-youtube-videos.html

<iframe class="youtube-player" type="text/html" width="640" height="385" src="http://www.youtube.com/embed/VIDEO_ID" frameborder="0">
</iframe>

jEUGDLFmpayttUirvGq

Got it! Thanks a lot again for hepilng me out!

Got it! Thanks a lot again for hepilng me out!

wordpress $post variables

<?php
$post–>ID # ID of the current post.
$post–>post_category # Retrieves the ID of the post category.
$post–>post_parent # ID of the page parent. Useful for creating custom navigational elements.
$post–>post_title # Post Title
$post–>post_excerpt # Post excerpt
$post–>post_content # Retrieves all of the post content, along with any markup.
$post–>post_name # Retrieving the slug of a post.
$post–>guid # Post Url
$post–>post_author # ID of post author post_parent
$post–>post_type # Returns the type, page or post.
$post–>menu_order # Returns the menu order as set in the post/page editing window. Often menu items are sorted via this value.
$post–>post_date # Retrieves the integer timestamp for when the post was published. The output can be customized. See the php.net date manual.
$post–>post_modified # Retrieves the integer timestamp for when the post was last modified.
$post–>post_status # Retrieves one of five possible posts statuses: publish, private, draft, pending, future.
$post–>comment_count # Returns the number of comments, pings, and trackbacks for a given post.
?>

remove wordpress 3.1 admin bar

paste in functions.php

wp_deregister_script('admin-bar');
wp_deregister_style('admin-bar');
remove_action('wp_footer','wp_admin_bar_render',1000);

grep an object's available methods

Object.methods.grep /inspect/
=> ["inspect", "pretty_inspect", "pretty_print_inspect"]

return the url base in ruby

>> url = "http://www.codestacker.com/tag/ruby"
>> url[/https?:\/\/.[^\/]+/]
=> "http://www.codestacker.com"

clean wordpress gallery css

paste in your functions.php and get these elements:

.gallery {}
.gallery-item {}
.gallery-icon {}
.gallery-caption {}

<?php

function remove_gallery_style() {
  return "<div class='gallery'>";
}
add_filter('gallery_style', 'remove_gallery_style');

?>

placeholder attribute for inputs in IE

require jquery and modernizer

if(!Modernizr.input.placeholder){
	$("input").each(function(){
		if($(this).val()=="" && $(this).attr("placeholder")!=""){
			$(this).val($(this).attr("placeholder"));
			$(this).focus(function(){
				if($(this).val()==$(this).attr("placeholder")) $(this).val("");
			});
			$(this).blur(function(){
				if($(this).val()=="") $(this).val($(this).attr("placeholder"));
			});
		}
	});
}

open all externals links in a new window

$('a[href*=http:]').each(function(){
        if (this.host != window.location.host) {
                $(this).attr('target', '_new');
        }
});

activerecord without rails

require 'active_record'

ActiveRecord::Base.establish_connection(:adapter => 'mysql2', :host => 'localhost', :user => 'root', :password => 'mypass', :database => 'mydatabase')

class User < ActiveRecord::Base ; end

puts User.count
#=> 9

writing a file

r - Open a file for reading. The file must exist.
w - Create an empty file for writing. If a file with the same name already exists its content is erased and the file is treated as a new empty file.
a - Append to a file. Writing operations append data at the end of the file. The file is created if it does not exist.
r+ - Open a file for update both reading and writing. The file must exist.
w+ - Create an empty file for both reading and writing. If a file with the same name already exists its content is erased and the file is treated as a new empty file.
a+ - Open a file for reading and appending. All writing operations are performed at the end of the file, protecting the previous content to be overwritten. You can reposition (fseek, rewind) the internal pointer to anywhere in the file for reading, but writing operations will move it back to the end of file. The file is created if it does not exist.

doc = "hello world"

File.open(my_file, 'w') {|f| f.write(doc) }

hide address bar in safari mobile

this code lets you hide the address bar in safari mobile. your webapp will look like a native one.

<body onload="window.scrollTo(0, 1)">

[css3] link animation

a {     
	color: #fff;
	-webkit-transition: color .5s ease-in;  
	-moz-transition: color .5s ease-in;  
	-o-transition: color .5s ease-in;  
	transition: color .5s ease-in;  
}

a:hover {
	color: #ccc;     
}

[css3] fade in page

@-webkit-keyframes fade-in {
	0%   { opacity: 0; }
	50%   { opacity: 0; }
	100% { opacity: 1; }
}

body {
	-webkit-animation-name: fade-in;
	-webkit-animation-duration: 1.5s;
}

check avaliability

# controller

def check_availability
  render :update do |page|
    if User.find(:first, :conditions => { :login => params[:user][:login] })
      page[:login_instant_feedback].replace_html 'Not available'
    end
  end
end

# view

link_to_remote 'check availability', { :url => { :action => 'check_availability' }, :before => $('login_instant_feedback').update('checking...'); new Effect.Appear('login_instant_feedback');" }

my .irbrc file

gems = %w(rubygems irb/completion wirble hirb ap)

gems.each do |gem_name|
  begin
    require gem_name
  rescue LoadError => e
    puts e.message
  end
end

Wirble.init if Wirble # http://pablotron.org/software/wirble/

Wirble.colorize if Wirble
Hirb.enable if Hirb # display items in a table




# Based on http://stackoverflow.com/questions/123494/whats-your-favourite-irb-trick

# Display methods in columns

def colputs(array)
  def num_columns; 4; end

  def col_width; 25; end

  def force_length(x)
    x = x.to_s
    max_length = col_width+2
    if x.length > max_length
      x = x[0..max_length-4] + '...'
    end
    x += (' '*max_length)
    x[0..max_length-1]
  end

  def get_element(array, i) # displays in column order instead of row order

    num_rows = (array.length/num_columns)+1
    col = i % num_columns
    row = i / num_columns
    array[col*num_rows+row]
  end
  
  for i in (0..array.length)
    print force_length(get_element(array, i))
    print "  "
    puts if (i % num_columns) == (num_columns-1)
  end
  
  nil
end

class Object
  # Return only the methods not present on basic objects in columns

  def show_methods
    colputs (self.methods - Object.new.methods).sort
  end

  # Based on https://github.com/ryanb/dotfiles/blob/master/irbrc  

  # Display methods added by user

  def local_methods(obj = self)
    (obj.methods - obj.class.superclass.instance_methods).sort
  end
  
  # Print documentation

  #   ri 'Array#pop'

  #   Array.ri

  #   Array.ri :pop

  #   arr.ri :pop

  def ri(method = nil)
    unless method && method =~ /^[A-Z]/ # if class isn't specified

      klass = self.kind_of?(Class) ? name : self.class.name
      method = [klass, method].compact.join('#')
    end
    system 'ri', method.to_s
  end
  
  # Copy

  def copy(obj = self)
    system %{echo '#{obj.to_s.gsub "'", "\\'"}' | pbcopy}
  end
end

wordpress categories dropdown

<!-- old way -->
<?php wp_list_categories('orderby=name&exclude=1&title_li='); ?>

<!-- dropdown -->
<select onchange='document.location.href=this.options[this.selectedIndex].value;'>
	<option><?php echo attribute_escape(__('Select Month')); ?></option>
<?php wp_get_archives('type=monthly&format=option&show_post_count=1'); ?>
</select>
Displaying Code 61 - 80 of 301 in total