Enter your keyword

Blogs

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');

How to add Action Hook Field in WP User Frontend

/**
 * Add the input field to the form
 *
 * @param int $form_id
 * @param null|int $post_id
 * @param array $form_settings
 */
function price_function_name( $form_id, $post_id, $form_settings ) {
    $value = '';
    if ( $post_id ) {
        $value = get_post_meta( $post_id, '_price', true );
    }
    ?>
    <div class="wpuf-label">
<label>Price<span class="required">*</span></label>
    </div>
    <div class="wpuf-fields">
       <input type="number" size="40"  value="<?php echo esc_attr( $value ); ?>" placeholder="Price" name="_price" required="required" data-type="text" data-required="yes" id="_price" class="textfield" > 
    </div>
    <?php
}
add_action( 'price_new_hook', 'price_function_name', 10, 3 );
/**
 * Update the custom field when the form submits
 *
 * @param type $post_id
 */
function update_my_brand_new_hook( $post_id ) {
    if ( isset( $_POST['_price'] ) ) {
        update_post_meta( $post_id, '_price', $_POST['_price'] );
    update_post_meta( $post_id, '_regular_price', $_POST['_price'] );
    }
}
add_action( 'wpuf_add_post_after_insert', 'update_my_brand_new_hook' );
add_action( 'wpuf_edit_post_after_update', 'update_my_brand_new_hook' );

How to Modify Custom Taxonomy Columns

To modify our genre custom taxonomy columns, we must hook into the manage_edit-$taxonomy_columns filter.

Since our taxonomy name is genre, our filter hook becomes – manage_edit-genre_columns

// Add to admin_init function
add_filter("manage_edit-genre_columns", 'theme_columns');  
function theme_columns($theme_columns) {
    $new_columns = array(
        'cb' => '<input type="checkbox" />',
  'gener_images' => 'Image',
        'name' => __('Name'),
  'description' => __('Description'),
        'slug' => __('Slug'),
        'posts' => __('Posts')
        );
    return $new_columns;
}
// Add to admin_init function   
add_filter("manage_genre_custom_column", 'manage_theme_columns', 10, 3);
function manage_theme_columns($out, $column_name, $theme_id) {
    $theme = get_term($theme_id, 'genre');
    switch ($column_name) {
        case 'gener_images': 
            // get header image url
   $genre_image = get_field('genre_image', 'genre_'.$theme->term_id);
      $out .= '<img src="'.$genre_image.'" width="80" />'; 
            break;
 
        default:
            break;
    }
    return $out;    
}

How to add a new section to the customizer

/**
 * Adds the individual sections, settings, and controls to the theme customizer
 */
function example_customizer( $wp_customize ) {
    $wp_customize->add_section(
        'example_section_one',
        array(
            'title' => 'Example Settings',
            'description' => 'This is a settings section.',
            'priority' => 35,
        )
    );
}
add_action( 'customize_register', 'example_customizer' );
$wp_customize->add_setting(
    'copyright_textbox',
    array(
        'default' => 'Default copyright text',
    )
);
$wp_customize->add_control(
    'copyright_textbox',
    array(
        'label' => 'Copyright text',
        'section' => 'example_section_one',
        'type' => 'text',
    )
);
<?php echo get_theme_mod( 'copyright_textbox' ); ?>

Custom Links From WordPress Admin Toolbar

function create_dsweb_menu() {
  global $wp_admin_bar;
  $menu_id = 'wpbeginner';
  $wp_admin_bar->add_menu(array('id' => $menu_id, 'title' => '<img class="tool_logo" src="'.get_bloginfo('stylesheet_directory').'/images/logo-for-admin-toolbar.png"/>', 'href' => admin_url().'index.php'));
  $wp_admin_bar->add_menu(array('parent' => $menu_id, 'title' => __('Visit Site'), 'id' => 'dsweb-home', 'href' => site_url(), 'meta' => array('target' => '_blank'))); 
}
add_action('admin_bar_menu', 'create_dsweb_menu', 2000);

Search / Filter posts on Title/Content/Taxonomy and Tags

<?php 
$searchkeys = 'searchkeyword';
global $wpdb;
$querysearch=" SELECT * FROM $wpdb->posts, $wpdb->term_relationships, $wpdb->term_taxonomy, $wpdb->terms WHERE ($wpdb->terms.name = '$searchkeys' OR $wpdb->posts.post_content LIKE '%$searchkeys%' OR $wpdb->posts.post_title LIKE '%$searchkeys%') AND $wpdb->posts.post_type = 'post' AND $wpdb->posts.ID = $wpdb->term_relationships.object_id AND  $wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id AND $wpdb->term_taxonomy.term_id = $wpdb->terms.term_id ORDER BY $wpdb->posts.post_date DESC";

$get_results = $wpdb->get_results($querysearch, OBJECT_K);
?>