Skip to content

Instantly share code, notes, and snippets.

View itzmekhokan's full-sized avatar
👨‍💻
Eat Sleep <Code> Repeat

Khokan Sardar itzmekhokan

👨‍💻
Eat Sleep <Code> Repeat
View GitHub Profile
@itzmekhokan
itzmekhokan / functions.php
Last active March 26, 2019 15:57
Change the default login logo in wordpress
<?php
function custom_loginlogo() {
echo '<style type="text/css">
h1 a {background-image: url('.get_bloginfo('template_directory').'/images/login_logo.png) !important; }
</style>';
}
add_action('login_head', 'custom_loginlogo');
@itzmekhokan
itzmekhokan / functions.php
Last active March 26, 2019 15:56
Popularity counts in WordPress
<?php
function getPostViews($postID){
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count==''){
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
return "0 View";
}
return $count.' Views';
@itzmekhokan
itzmekhokan / functions.php
Last active March 26, 2019 15:56
WordPress Custom search Include All Custom Post Types
<?php
function my_custom_search($query) {
// Check to verify it's search page
if( is_search( ) {
$post_types = get_post_types(array('public' => true, 'exclude_from_search' => false), 'objects');
$searchable_types = array();
// Add available post types
if( $post_types ) {
foreach( $post_types as $type) {
$searchable_types[] = $type->name;
@itzmekhokan
itzmekhokan / functions.php
Last active March 26, 2019 15:55
Automatically Add Dynamic Title to Pages in WordPress
<?php
/* Dynamic Titles **/
// This sets your <title> depending on what page you're on, for better formatting and for SEO
// You need to set the variable $longd to some custom text at the beginning of the function
function dynamictitles() {
$longd = __('Enter your longdescription here.', 'texdomainstring');
if ( is_single() ) {
wp_title('');
echo ' | '.get_bloginfo('name');
} else if ( is_page() || is_paged() ) {
@itzmekhokan
itzmekhokan / functions.php
Last active March 26, 2019 15:55
Display Twitter Followers Counts and More in WordPress
<?php
function get_twitter_user_data( $username, $field, $display = false ) {
    $interval = 3600;
    $cache = get_option('get_twitter_user_data');
    $url = 'http://api.twitter.com/1/users/show.json?screen_name='.urlencode($username);
    if ( false == $cache )
    $cache = array();
    
    // if first time request add placeholder and force update
    if ( !isset( $cache[$username][$field] ) ) {
@itzmekhokan
itzmekhokan / display.php
Last active March 26, 2019 15:54
Display Twitter Followers Counts and more in WordPress do follows
<?php
echo get_twitter_user_data('Khokan', 'name') . ' has ' .
get_twitter_user_data('Khokan', 'followers_count') . ' followers after ' .
get_twitter_user_data('Khokan', 'statuses_count') . ' updates.';
@itzmekhokan
itzmekhokan / functions.php
Last active May 13, 2019 08:42
Create order dynamically in woocommerce
<?php
/*
* Create order dynamically
*/
function create_order_dynamically() {
$customer_email = 'peter@parker.com';
$customer_address = array(
'first_name' => 'Peter',
'last_name' => 'Parker',
'company' => '',
@itzmekhokan
itzmekhokan / .htaccess
Created March 26, 2019 15:48
Redirect mobile users to mobile site
RewriteEngine On
# Check for mime types commonly accepted by mobile devices
RewriteCond %{HTTP_ACCEPT} "text\/vnd\.wap\.wml|application\/vnd\.wap\.xhtml\+xml" [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^ http://m.domain.com%{REQUEST_URI} [R,L]
@itzmekhokan
itzmekhokan / add-mime-types.php
Created March 28, 2019 17:29
Add additional file types support in wordpress mainly adobe illustrator files
<?php
function add_custom_upload_mimes( $mimes_types ) {
$mimes_types['eps'] = 'application/postscript'; // Adobe Illustrator files
$mimes_types['ai'] = 'application/postscript'; // Adobe Illustrator files
return $mimes_types;
}
add_filter( 'upload_mimes', 'add_custom_upload_mimes' );
@itzmekhokan
itzmekhokan / mime-extension-exception.php
Created March 28, 2019 17:47
to handle file types not permitted for security reasons
<?php
function add_allow_upload_extension_exception( $types, $file, $filename, $mimes ) {
// Do basic extension validation and MIME mapping
$wp_filetype = wp_check_filetype( $filename, $mimes );
$ext = $wp_filetype['ext'];
$type = $wp_filetype['type'];
if( in_array( $ext, array( 'ai', 'eps' ) ) ) { // if follows illustrator files have
$types['ext'] = $ext;
$types['type'] = $type;
}