Skip to content

Instantly share code, notes, and snippets.

View ravinderk's full-sized avatar

Ravinder Kumar ravinderk

View GitHub Profile
// only author can see their own post
function ravs_parse_query_useronly( $wp_query ) {
if ( strpos( $_SERVER[ 'REQUEST_URI' ], '/wp-admin/edit.php' ) !== false ) {
if ( !current_user_can( 'level_10' ) ) {
global $current_user;
$wp_query->set( 'author', $current_user->id );
}
}
}
//Display variation dropdown as table
function woocommerce_variable_add_to_cart() {
global $product, $post;
$variations = $product->get_available_variations();
foreach ($variations as $key => $value) {
?>
<form action="<?php echo esc_url( $product->add_to_cart_url() ); ?>"method="post" enctype='multipart/form-data'>
<input type="hidden" name="variation_id" value="<?php echo $value['variation_id']?>" />
<input type="hidden" name="product_id" value="<?php echo esc_attr( $post->ID ); ?>" />
<?php
@ravinderk
ravinderk / gist:6543905
Last active December 22, 2015 22:59
Implement Radius Search
Important Note:
-----------------------------
a). Latitude and Logitude stored as post meta.
b). skeleton for $seached_post and $all_post is same:
//paste this code in wordpress loop to collect required data of searched posts.
$searched_post[] = array(
'id' => get_the_ID(), // post id
'lat'=> $lat, // latitude value
@ravinderk
ravinderk / gist:6553212
Last active December 23, 2015 00:19
Overwrite activation page template in gravity form's user registration addon
Paste this code in functios.php
( copy /wp-content/plugins/gravityformsuserregistration/includes/activate.php file in /wp-content/themes/<your_theme>/ and edit activate.php as your html layout )
/* overwrite user activation template */
function ravs_user_activation_template(){
$template_path = TEMPLATEPATH.'/activate.php';
if( file_exists($template_path) && rgget('page') == 'gf_activation')
@ravinderk
ravinderk / gist:6554207
Created September 13, 2013 18:20
Edit WordPress Menu and Admin Bar Menu
function change_post_menu_label() {
global $menu;
$menu[2][0] = 'Edit My Ads';
echo '';
}
function change_admin_bar_menu_label() {
global $wp_admin_bar;
$wp_admin_bar->remove_node( 'dashboard' );
//Add a link called 'Edit My Ads'...
$wp_admin_bar->add_menu( array(
function GetMultiCellHeight($w, $h, $txt, $border=null, $align='J') {
// Calculate MultiCell with automatic or explicit line breaks height
// $border is un-used, but I kept it in the parameters to keep the call
// to this function consistent with MultiCell()
$cw = &$this->CurrentFont['cw'];
if($w==0)
$w = $this->w-$this->rMargin-$this->x;
$wmax = ($w-2*$this->cMargin)*1000/$this->FontSize;
$s = str_replace("\r",'',$txt);
$nb = strlen($s);
@ravinderk
ravinderk / gist:6722720
Created September 27, 2013 00:47
List All Hooked Functions
function list_hooked_functions($tag=false){
global $wp_filter;
if ($tag) {
$hook[$tag]=$wp_filter[$tag];
if (!is_array($hook[$tag])) {
trigger_error("Nothing found for '$tag' hook", E_USER_WARNING);
return;
}
}
else {
@ravinderk
ravinderk / gist:6722746
Created September 27, 2013 00:50
Storing Your Whole Page In A Variable
add_action('wp_head', 'smashing_buffer_start');
add_action('wp_footer', 'smashing_buffer_end');
function smashing_buffer_start() {
ob_start( 'smashing_callback' );
}
function buffer_end() {
ob_end_flush();
}
@ravinderk
ravinderk / gist:6764079
Last active December 24, 2015 07:29
plugin error check
/**
* Debugging plugin error
*
*/
function save_error() {
update_option( 'plugin_error', ob_get_contents() );
}
add_action( 'activated_plugin', 'save_error' );
@ravinderk
ravinderk / gist:6788384
Created October 2, 2013 02:41
PHP Relative Time Function
function relative_time($date, $postfix = ' ago', $fallback = 'F Y')
{
$diff = time() - strtotime($date);
if($diff < 60)
return $diff . ' second'. ($diff != 1 ? 's' : '') . $postfix;
$diff = round($diff/60);
if($diff < 60)
return $diff . ' minute'. ($diff != 1 ? 's' : '') . $postfix;
$diff = round($diff/60);
if($diff < 24)