Popular Posts Widget

/* ~~~~~~~~~~~ Popular Posts Widget ~~~~~~~~~~~ */

function register_ter_popular_posts(){ register_widget('TerPopularPosts'); }
add_action('widgets_init', 'register_ter_popular_posts');

function ter_set_post_views($id){
$count_key = 'ter_post_view_count';
$count = get_post_meta($id,$count_key,true);
if($count == ''){
$count = 0;
delete_post_meta($id,$count_key);
add_post_meta($id,$count_key,'0');
}
else{
$count++;
update_post_meta($id,$count_key,$count);
}
}

function ter_get_post_views($id){
$count_key = 'ter_post_view_count';
$count = get_post_meta($id,$count_key,true);
if($count == ''){
delete_post_meta($id,$count_key);
add_post_meta($id,$count_key,'0');
return '0 Views';
}
return $count . ' Views';
}

class TerPopularPosts extends WP_Widget{
function __construct() {
parent::__construct(
'ter_popular_posts',
__('Villages Popular Posts','terra'),
array('description' => __('The Popular Posts Widget','terra'),)
);
}

public function widget($args,$instance){
$title = apply_filters('widget_title',$instance['title']);
echo $args['before_widget'];
if(!empty($title)) echo $args['before_title'] . $title . $args['after_title'];

echo '<ul class="popular-posts">';
query_posts('meta_key=ter_post_view_count&orderby=meta_value_num&order=DESC&showposts=5');
if(have_posts()) : while (have_posts()) : the_post(); ?>
<li><a href="<?php the_permalink() ?>"><?php the_title() ?></a></li>
<?php
endwhile; endif;
wp_reset_query();
echo '</ul>';

echo $args['after_widget'];
}

public function form($instance){
if(isset($instance['title'])) $title = $instance['title'];
else $title = __('Popular Posts','terra');
?>
<p>
<label for="<?php echo $this->get_field_id('title') ?>"><?php _e( 'Title:' ) ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('title') ?>" name="<?php echo $this->get_field_name('title') ?>" type="text" value="<?php echo esc_attr($title); ?>">
</p>
<?php
}

public function update($new_instance,$old_instance){
$instance = array();
$instance['title'] = (! empty($new_instance['title'])) ? strip_tags($new_instance['title']) : '';
return $instance;
}

}//END TerPopularPosts

Limit WordPress Image Upload Size

function ter_limit_image_uploads($file){
$error_message = 'KB is to large. Please reduce the file size of your image to ' . TER_MAX_IMAGE_SIZE_KB . 'KB or less. Hosting space is limited and uploading large images will slow down your page loads. Use a tool such as http://toki-woki.net/p/Shrink-O-Matic/ to help you to resize your images. Target size: Width 1024px, file size 100KB-200KB';
$size_in_kb = $file['size'] / 1024;
if(preg_match('/image/',$file['type'])) if($size_in_kb > TER_MAX_IMAGE_SIZE_KB) $file['error'] = round($size_in_kb,2) . $error_message;
return $file;
}
add_filter('wp_handle_upload_prefilter','ter_limit_image_uploads'); //Limit uploaded image size

Shortcode AutoP Savior

function ter_shortcode_content_filter($content){
$block = join('|',array('one-third','grid'));
$rep = preg_replace("/(<p>)?[($block)(s[^]]+)?](</p>|<br />)?/","[$2$3]",$content);
$rep = preg_replace("/(<p>)?[/($block)](</p>|<br />)?/","[/$2]",$rep);
return $rep;
}
add_filter('the_content','ter_shortcode_content_filter');

Floating Back to Top

//Call like this:

<?php ter_back_to_top(900,600) ?>

//The Function

/* Back to Top */
function ter_back_to_top($offset = 220, $duration = 500){?>
<a href="#" class="back-to-top rounded-5"><img src="<?php echo TER_CHILD_GRAPHICS ?>top.png" alt="back to top"></a>
<script type="text/javascript">
jQuery(document).ready(function(){
var offset = <?php echo $offset ?>;
var duration = <?php echo $duration ?>;
jQuery(window).scroll(function(){
if(jQuery(this).scrollTop() > offset) jQuery('.back-to-top').fadeIn(duration);
else jQuery('.back-to-top').fadeOut(duration);
});
jQuery('.back-to-top').click(function(event){
event.preventDefault();
jQuery('html,body').animate({scrollTop: 0},duration);
return false;
})
});
</script>
<?php
}

Rotator, quote or whatever

<div id="hypfb-quote-item-1" class="hypfb-quote-item none">Quote Here</div>
<div id="hypfb-quote-item-2" class="hypfb-quote-item none">Quote 2 Here</div>
<img src="" onmouseover="hypfb_rotate_quote(1);" alt="">
<img src="" onmouseover="hypfb_rotate_quote(2);" alt="">

<script type="text/javascript">
function hypfb_rotate_quote(id){
if(currentQuote == '#hypfb-quote-item-' + id) return;
jQuery(currentQuote).stop(true).fadeOut(function(){
currentQuote = '#hypfb-quote-item-' + id;
jQuery(currentQuote).fadeIn();
});
}
</script>

Get post id and enqueue scripts

//Enqueue
function hypxml_enqueue_files(){
global $post;
$page_array = array(195);
if(in_array($post->ID,$page_array)){
add_action('wp_print_scripts', 'hypxml_enqueue_js');
add_action('wp_print_styles','hypxml_add_stylesheet');
}
}
add_action('wp','hypxml_enqueue_files',5,0);

function hypxml_enqueue_js(){
wp_enqueue_script('hypxml_js', HYPXML_PLUGIN . 'scripts.js',array('jquery'));
wp_enqueue_script('hypxml_sort_js', HYPXML_PLUGIN . 'jquery.tablesorter.min.js',array('jquery'));
}
function hypxml_add_stylesheet(){ wp_enqueue_style('hypxml_styles', HYPXML_PLUGIN . 'style.css'); }

Redirect site to subfolder

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteCond %{REQUEST_URI} !^/subfolder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /subfolder/$1
RewriteCond %{HTTP_HOST} ^(www.)?hyptx.com$
RewriteRule ^(/)?$ subfolder/Home.html [L]

Terms of Service custom field for Gravity Forms

<?php
/**
* Plugin Name: Gravity Forms Terms of Service Field
* Terms of Service Gravity Forms Custom Form Field
* Plugin URI: http://wpsmith.net/2011/plugins/how-to-create-a-custom-form-field-in-gravity-forms-with-a-terms-of-service-form-field-example/
* Donate link: http://wpsmith.net/donation
* Description: The first generation of this plugin will set a default image for post thumbnails for the Genesis framework.
* Version: 0.2 beta
* Author: Travis Smith
* Author URI: http://www.wpsmith.net/
* License: GPLv2
*
* Copyright 2012 Travis Smith (email : http://www.wpsmith.net/contact)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2, as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
*
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/

// Add a custom field button to the advanced to the field editor
add_filter( 'gform_add_field_buttons', 'wps_add_tos_field' );
function wps_add_tos_field( $field_groups ) {
foreach( $field_groups as &$group ){
if( $group["name"] == "advanced_fields" ){ // to add to the Advanced Fields
//if( $group["name"] == "standard_fields" ){ // to add to the Standard Fields
//if( $group["name"] == "post_fields" ){ // to add to the Standard Fields
$group["fields"][] = array(
"class"=>"button",
"value" => __("Terms of Service", "gravityforms"),
"onclick" => "StartAddField('tos');"
);
break;
}
}
return $field_groups;
}

// Adds title to GF custom field
add_filter( 'gform_field_type_title' , 'wps_tos_title' );
function wps_tos_title( $type ) {
if ( $type == 'tos' )
return __( 'Terms of Service' , 'gravityforms' );
}

// Adds the input area to the external side
add_action( "gform_field_input" , "wps_tos_field_input", 10, 5 );
function wps_tos_field_input ( $input, $field, $value, $lead_id, $form_id ){

if ( $field["type"] == "tos" ) {
$max_chars = "";
if(!IS_ADMIN && !empty($field["maxLength"]) && is_numeric($field["maxLength"]))
$max_chars = self::get_counter_script($form_id, $field_id, $field["maxLength"]);

$input_name = $form_id .'_' . $field["id"];
$tabindex = GFCommon::get_tabindex();
$css = isset( $field['cssClass'] ) ? $field['cssClass'] : '';
return sprintf("<div class='ginput_container'><textarea readonly name='input_%s' id='%s' class='textarea gform_tos %s' $tabindex rows='10' cols='50'>%s</textarea></div>{$max_chars}", $field["id"], 'tos-'.$field['id'] , $field["type"] . ' ' . esc_attr($css) . ' ' . $field['size'] , esc_html($value));

}

return $input;
}

// Now we execute some javascript technicalitites for the field to load correctly
add_action( "gform_editor_js", "wps_gform_editor_js" );
function wps_gform_editor_js(){
?>

<script type='text/javascript'>

jQuery(document).ready(function($) {
//Add all textarea settings to the "TOS" field plus custom "tos_setting"
// fieldSettings["tos"] = fieldSettings["textarea"] + ", .tos_setting"; // this will show all fields that Paragraph Text field shows plus my custom setting

// from forms.js; can add custom "tos_setting" as well
fieldSettings["tos"] = ".label_setting, .description_setting, .admin_label_setting, .size_setting, .default_value_textarea_setting, .error_message_setting, .css_class_setting, .visibility_setting, .tos_setting"; //this will show all the fields of the Paragraph Text field minus a couple that I didn't want to appear.

//binding to the load field settings event to initialize the checkbox
jQuery(document).bind("gform_load_field_settings", function(event, field, form){
jQuery("#field_tos").attr("checked", field["field_tos"] == true);
jQuery("#field_tos_value").val(field["tos"]);
});
});

</script>
<?php
}

// Add a custom setting to the tos advanced field
add_action( "gform_field_advanced_settings" , "wps_tos_settings" , 10, 2 );
function wps_tos_settings( $position, $form_id ){

// Create settings on position 50 (right after Field Label)
if( $position == 50 ){
?>

<li class="tos_setting field_setting">

<input type="checkbox" id="field_tos" onClick="SetFieldProperty('field_tos', this.checked);" />
<label for="field_tos" class="inline">
<?php _e("Disable Submit Button", "gravityforms"); ?>
<?php gform_tooltip("form_field_tos"); ?>
</label>

</li>
<?php
}
}

//Filter to add a new tooltip
add_filter('gform_tooltips', 'wps_add_tos_tooltips');
function wps_add_tos_tooltips($tooltips){
$tooltips["form_field_tos"] = "<h6>Disable Submit Button</h6>Check the box if you would like to disable the submit button.";
$tooltips["form_field_default_value"] = "<h6>Default Value</h6>Enter the Terms of Service here.";
return $tooltips;
}

// Add a script to the display of the particular form only if tos field is being used
add_action( 'gform_enqueue_scripts' , 'wps_gform_enqueue_scripts' , 10 , 2 );
function wps_gform_enqueue_scripts( $form, $ajax ) {
// cycle through fields to see if tos is being used
foreach ( $form['fields'] as $field ) {
if ( ( $field['type'] == 'tos' ) && ( isset( $field['field_tos'] ) ) ) {
$url = plugins_url( 'gform_tos.js' , __FILE__ );
wp_enqueue_script( "gform_tos_script", $url , array("jquery"), '1.0' );
break;
}
}
}

// Add a custom class to the field li
add_action("gform_field_css_class", "custom_class", 10, 3);
function custom_class($classes, $field, $form){

if( $field["type"] == "tos" ){
$classes .= " gform_tos";
}

return $classes;
}
?>