Pages

Monday, April 28, 2014

Wordpress pass variable when calling template - [resolved]

If you want to pass any variable when include template than please follow below method:

1. I want to send page ID to sidebar than I have create one array for this and pass it using below method:

<?php
$params = array(
'ID' => $parentId
);
getTemplatePart('sidebar', 'subpages', $params);
?>

2. If you want to get this array than please see at below code:

<?php
$parentId = $ID;

// here, you can set your login
?>

3. Please write below function in your functions.php for calling the template:

<?php
function getTemplatePart($slug = null, $name = null, array $params = array()) {
    global $posts, $post, $wp_did_header, $wp_query, $wp_rewrite, $wpdb, $wp_version, $wp, $id, $comment, $user_ID;

    do_action("get_template_part_{$slug}", $slug, $name);
    $templates = array();
    if (isset($name))
        $templates[] = "{$slug}-{$name}.php";

    $templates[] = "{$slug}.php";

    $_template_file = locate_template($templates, false, false);

    if (is_array($wp_query->query_vars)) {
        extract($wp_query->query_vars, EXTR_SKIP);
    }
    extract($params, EXTR_SKIP);

    require($_template_file);
}
?>

Remove Extra "P" tags from content area in wordpress- [resolved]

If you want to remove extra "<p>" tags from content than please write below code in your functions.php

Method 1:

function wp_remove_extra_p_tags($content) {
    $array = array (
        '<p>[' => '[',
        ']</p>' => ']',
    );
    $content = strtr($content, $array);
return $content;
}
add_filter('the_content', 'wp_remove_extra_p_tags', 10);


Method 2:

function wp_remove_extra_p_tags($content) {
    $content = str_replace(array("<p></p>","<p> </p>"),"",$content);
    return $content;
}
add_filter('the_content', 'wp_remove_extra_p_tags');

Monday, April 14, 2014

Woocommerce change error message OR repalce default message - [resolved]

If you want to change default messages in woocommerce than you have to write below code in functions.php

function sagepay_woocommerce_add_error( $error ) {
    if( 'SagePay Direct - Card number required.' == $error ) {
        $error = 'Card number required.';
    } else if( 'SagePay Direct - Fullname required.' == $error ) {
        $error = 'Fullname required.';
    } else if( 'SagePay Direct - CV2 required.' == $error ) {
        $error = 'CV2 required.';
    }
    return $error;
}
add_filter( 'woocommerce_add_error', 'sagepay_woocommerce_add_error' );

Woocommerce Change/Remove Payment Gateway Icon-[resolved]

If anyone wants to change payment gateway details than he can do this from below way.
Please write "woocommerce_available_payment_gateways" hook in functions.php

function remove_sagepage_payment_gateway_icon( $gateways ) {
if(isset($gateways['sagepaydirect'])){
$gateways['sagepaydirect']->icon = '';
}
return $gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'remove_sagepage_payment_gateway_icon' );

Thursday, April 3, 2014

Woocommerce get top level category - [resolved]

function get_top_level_product_cat(){
    global $post;
    $prod_terms = get_the_terms( $post->ID, 'product_cat' );
    foreach ($prod_terms as $prod_term) {

        // gets product cat id
        $product_cat_id = $prod_term->term_id;

        // gets an array of all parent category levels
        $product_parent_categories_all_hierachy = get_ancestors( $product_cat_id, 'product_cat' ); 
       
        // This cuts the array and extracts the last set in the array
        $last_parent_cat = array_slice($product_parent_categories_all_hierachy, -1, 1, true);
        foreach($last_parent_cat as $last_parent_cat_value){
            return $product_category = get_product_category_by_id( $last_parent_cat_value );           
        }
    }
}

function get_product_category_by_id( $category_id ) {
    $term = get_term_by( 'id', $category_id, 'product_cat', 'ARRAY_A' );
    return $term['name'];
}

Split to max characters without breaking word - PHP [resolved]

<?php

function split_str($str, $maxlen) {
    if (strlen($str) <= $maxlen) {
       return $str;
    } 
    $newstr = substr($str, 0, $maxlen);
    if ( substr($newstr,-1,1) != ' ' )
        $newstr = substr($newstr, 0, strrpos($newstr, " "));
    return $newstr;
}

echo split_str("Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.", 22);

?>