Pages

Tuesday, August 27, 2013

wordpress get attachment id by url


function wp_get_attachment_id_from_url( $attachment_url = '' ) {

global $wpdb;
$attachment_id = false;

// If there is no url, return.
if ( '' == $attachment_url )
return;

// Get the upload directory paths
$upload_dir_paths = wp_upload_dir();

// Make sure the upload path base directory exists in the attachment URL, to verify that we're working with a media library image
if ( false !== strpos( $attachment_url, $upload_dir_paths['baseurl'] ) ) {

// If this is the URL of an auto-generated thumbnail, get the URL of the original image
$attachment_url = preg_replace( '/-\d+x\d+(?=\.(jpg|jpeg|png|gif)$)/i', '', $attachment_url );

// Remove the upload path base directory from the attachment URL
$attachment_url = str_replace( $upload_dir_paths['baseurl'] . '/', '', $attachment_url );

// Finally, run a custom database query to get the attachment ID from the modified attachment URL
$attachment_id = $wpdb->get_var( $wpdb->prepare( "SELECT wposts.ID FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta WHERE wposts.ID = wpostmeta.post_id AND wpostmeta.meta_key = '_wp_attached_file' AND wpostmeta.meta_value = '%s' AND wposts.post_type = 'attachment'", $attachment_url ) );

}

return $attachment_id;
}

Wordpress insert new post status

Please write below code in your functions.php


function deactive_post_status_removed(){
register_post_status( 'removed', array(
'label'                     => _x( 'Removed', 'post' ),
'public'                    => false,
'exclude_from_search'       => true,
'show_in_admin_all_list'    => true,
'show_in_admin_status_list' => true,
'label_count'               => _n_noop( 'Removed <span class="count">(%s)</span>', 'Removed <span class="count">(%s)</span>' ),
) );
}
add_action( 'init', 'deactive_post_status_removed' );

I have use this code for deactivate post status

Jquery input label toggle effect


<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<script type="text/javascript" src="jquery-1.7.2.min.js"></script>
<script type="text/javascript">
(function($){$.fn.overlabel=function(options){var opts=$.extend({},$.fn.overlabel.defaults,options);var selection=this.filter('label[for]').map(function(){var label=$(this);var id=label.attr('for');var field=document.getElementById(id);if(!field)return;if(label.hasClass('cms-overlabel-active')==false){var o=$.meta?$.extend({},opts,label.data()):opts;label.addClass(o.label_class).addClass('cms-overlabel-active');var hide_label=function(){label.hide();label.data('shown',0);};var show_label=function(){if(!this.value&&label.data('shown')!=1){label.fadeIn('fast');label.data('shown',1);}else if(this.value&&label.data('shown')==1){label.hide();label.data('shown',0);}};var p=$(field).position();var h=parseInt($(field).height().toString().replace('px',''),10);var fsRaw=$(field).css('font-size');var fs=parseInt(fsRaw.replace('px',''),10);if(fs>h){fsRaw=$(field).parent().css('font-size');}
label.css({'top':p.top+'px','left':p.left+'px','padding-top':parseInt($(field).css("padding-top"))+'px','padding-left':(parseInt($(field).css("padding-left"))+2)+'px','font-family':$(field).css("font-family"),'font-size':fsRaw});$(field).focus(hide_label).blur(show_label).change(show_label).each(hide_label).each(show_label);}
return this;});return opts.filter?selection:selection.end();};$.fn.overlabel.defaults={label_class:'cms-label-dynamic',hide_css:{'text-indent':'-10000px'},show_css:{'text-indent':'0px','cursor':'text'},filter:false};})(jQuery);
</script>
</head>
<body>
<div class="cms-label-watermark-wrap">
<label for="first_name" class="cms-label-watermark">First Name</label>
<input class="" type="text" size="30" name="first_name" id="first_name" value="" />
</div>


<script type="text/javascript">
$(document).ready(function(){
$('label.cms-label-watermark').overlabel();
});
</script>

</body>
</html>

Thursday, August 22, 2013

Restrict admin panel from front user in wordpress


add_action( 'init', 'blockusers_init' );
function blockusers_init() {
    if ( is_admin() && ! current_user_can( 'administrator' ) && ! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
        wp_redirect( home_url() );
        exit;
    }
}

Sunday, August 18, 2013

WooCommerce - Show number of items in cart and total

<?php global $woocommerce; ?>

<a class="cart-contents" href="<?php echo $woocommerce->cart->get_cart_url(); ?>" title="<?php _e('View your shopping cart', 'woothemes'); ?>"><?php echo sprintf(_n('%d item', '%d items', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count);?> - <?php echo $woocommerce->cart->get_cart_total(); ?></a>

Woocommerce - disable/removing products descriptions tabs

Please write below code in functions.php

add_filter( 'woocommerce_product_tabs', 'woo_remove_product_tabs', 98 );
function woo_remove_product_tabs( $tabs ) {   
    unset( $tabs['description'] ); // Remove the description tab
    //unset( $tabs['reviews'] ); // Remove the reviews tab
    //unset( $tabs['additional_information'] ); // Remove the additional information tab
   
    return $tabs;
}

Tuesday, August 13, 2013

PHP get week number from date



function get_week_number_by_date($date) {

$today = $date;
$currentDay = date("w", strtotime($date));
$todate = date('Y-m-d', strtotime($date));
$myTime = $todate." 18:00:00";

$weekNumber = date("W", strtotime("0 day", strtotime($date)));

if($currentDay == 0){
if(strtotime($today) > strtotime($myTime)){
$weekNumber = date("W", strtotime("1 day", strtotime($date)));
}
}
return $weekNumber;
}

In this example I have setup "sunday 6:00 PM" as a week start day.