Enter your keyword

Blogs

Add Additional File Types to be Uploaded in WordPress

function dsweb_myme_types($mime_types){ 
$mime_types['svg'] = 'image/svg+xml'; //Adding svg extension
$mime_types['psd'] = 'image/vnd.adobe.photoshop'; //Adding photoshop files
return $mime_types;
}
add_filter('upload_mimes', 'dsweb_myme_types', 1, 1);

Add an Admin User in WordPress

function wpb_admin_account(){ 
$user = 'Username'; 
$pass = 'Password'; 
$email = 'email@domain.com'; 
if ( !username_exists( $user )  && !email_exists( $email ) ) {  
$user_id = wp_create_user( $user, $pass, $email );
$user = new WP_User( $user_id );
$user->set_role( 'administrator' );
}
}
add_action('init','wpb_admin_account');

How to Add Additional Image Sizes in WordPress

WordPress automatically creates several image sizes when you upload an image. You can also create additional image sizes to use in your theme. Add this code your theme's functions file.

add_image_size( 'sidebar-thumb', 120, 120, true ); // Hard Crop Mode  
  add_image_size( 'homepage-thumb', 220, 180 ); // Soft Crop Mode  
  add_image_size( 'singlepost-thumb', 590, 9999 ); // Unlimited Height Mode

You can display an image size in anywhere in your theme using this code

the_post_thumbnail( 'homepage-thumb' );

How to Change WP Admin Footer Left Text

 function change_footer_admin () {
 echo 'Develop By <a href="http://www.dswebsolutions.in" target="_blank">DS Web Solutions</a></p>';
 }
 add_filter('admin_footer_text', 'change_footer_admin');
  

How to change WP Admin Logo

function custom_logo_wp_admin() { 
?>
<style type="text/css">
body.login div#login h1 a {
background-image: url(<?php echo get_bloginfo('stylesheet_directory') ?>/images/logo.png);  /* Logo Path*/
background-size: 220px;  /* Logo Background size */
height: 180px; /* Logo Height */
width: 180px; /*  Logo Width  */
}
</style>
<?php 
} 
add_action( 'login_enqueue_scripts', 'custom_logo_wp_admin' );

How to get CSV File Data in Php

$file_name = 'samplefile.csv';
 $row = 0;
if (($handle = fopen($file_name, "r")) !== FALSE) {
	 echo '<table>';
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        if($row == 0){ 
            $row++; 
            } else {
        echo '<tr>';
          echo '<td>'.$data[0].'</td>';
        echo '<td>'.$data[1].'</td>';
        echo '<td>'.$data[2].'</td>';
        echo '<td>'.$data[3].'</td>';
        echo '</tr>';
        }
}
    echo '</table>';
    } else {
    echo 'File could not be opened.';
    }	
    fclose($handle);

Instagram API how to get user ID?

function getInstaID($username)
{
    $username = strtolower($username); // sanitization
    $token = "InsertThatHere";
    $url = "https://api.instagram.com/v1/users/search?q=".$username."&access_token=".$token;
    $get = file_get_contents($url);
    $json = json_decode($get);
    $output = '';
    foreach($json->data as $user)
    {
    if($user->username == $username)
     {
     // $output .= $user->id 
       $output .= '<div class="result"> ';
       $output .= '<div class="id"><h4>User ID: '.$user->id.'<br></h4></div> ';
       $output .= '<div class="restults">';
       $output .= '<div class="avatar"> <img alt="" src="'.$user->profile_picture.'"></a></div>';
       $output .=	'<div class="text">Username: '.$user->username.'<br>Name: '.$user->full_name.'<br></div>';
       $output .=    '</div>';
       $output .= '</div>';
       //return $user->id;
        }
    }
    return $output; // return this if nothing is found
}

echo getInstaID('aliciakeys'); // this should print 20979117

 

Replace the WooCommerce default page Navigation

If you need to then manually replace the WooCommerce default page navigation then add this code to your current themes functions.php file:

/**
* Replace WooCommerce Default Pagination with WP-PageNavi Pagination
*
* @author WPSnacks.com
* @link http://www.wpsnacks.com
*/
remove_action('woocommerce_pagination', 'woocommerce_pagination', 10);
function woocommerce_pagination() {
wp_pagenavi();
}
add_action( 'woocommerce_pagination', 'woocommerce_pagination', 10);

WP-PageNavi

Show cart contents/total in WooCommerce

To display the cart contents and total in your template use something like:

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

To ajaxify your cart viewer so it updates when an item is added (via ajax) use:

// Ensure cart contents update when products are added to the cart via AJAX (place the following in functions.php)
add_filter( 'woocommerce_add_to_cart_fragments', 'woocommerce_header_add_to_cart_fragment' );
function woocommerce_header_add_to_cart_fragment( $fragments ) {
  ob_start();
  ?>
  <a class="cart-contents" href="<?php echo WC()->cart->get_cart_url(); ?>" title="<?php _e( 'View your shopping cart' ); ?>"><?php echo sprintf (_n( '%d item', '%d items', WC()->cart->cart_contents_count ), WC()->cart->cart_contents_count ); ?> - <?php echo WC()->cart->get_cart_total(); ?></a> 
  <?php  
  $fragments['a.cart-contents'] = ob_get_clean();
  return $fragments;
}

Change the `add to cart` text on product archives in WooCommerce

add_filter( 'add_to_cart_text', 'woo_custom_cart_button_text' );                        // < 2.1
add_filter( 'woocommerce_product_add_to_cart_text', 'woo_custom_cart_button_text' );    // 2.1 +
function woo_custom_cart_button_text() {
        return __( 'My Button Text', 'woocommerce' );
}

Customise `add to cart` text on single product pages in WooCommerce

Add the following to your functions.php file replacing 'my button text' with whatever you want it to say:

add_filter( 'add_to_cart_text', 'woo_custom_cart_button_text' );                                // < 2.1
add_filter( 'woocommerce_product_single_add_to_cart_text', 'woo_custom_cart_button_text' );    // 2.1 +
function woo_custom_cart_button_text() {
        return __( 'Buy Now', 'woocommerce' );
}

How to automatically complete orders in WooCommerce

Automatically mark orders for virtual products as Completed after a successful payment by adding this snippet into your functions.php:

/**
* Auto Complete all WooCommerce orders.
* Add to theme functions.php file
*/

add_action( 'woocommerce_thankyou', 'custom_woocommerce_auto_complete_order' );
function custom_woocommerce_auto_complete_order( $order_id ) {
    global $woocommerce;
    if ( !$order_id )
        return;
    $order = new WC_Order( $order_id );
    $order->update_status( 'completed' );
}

How to change Registration page in wordpress

function ds_change_register_link($link, $action=null, $query=null)
{
    $parsed_link = parse_url($link);
    if ($parsed_link['query'] == 'action=register') {
        $link = site_url().'/registration/';
    }
    return $link;
}
add_filter('tml_page_link', 'ds_change_register_link');