Enter your keyword

Blogs

How to Marge two arrays in one array

$array1 = array("red", "shape", "color");
$array2 = array("a", "b",  "green", "trapezoid");
$result = array_merge($array1, $array2);
print_r($result); 

How to Search in Array

$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
$key = array_search('green', $array); 

How to Add Odd and Even CSS Classes to WordPress Posts

function oddeven_post_class($classes ) { 
global $current_class;
$classes[] = $current_class;
$current_class = ($current_class == 'odd') ? 'even' : 'odd'; 
return $classes;
}
add_filter( 'post_class' , 'oddeven_post_class' );

global $current_class;
$current_class = 'odd';

How to Disable Search Feature in WordPress

function fb_filter_query( $query, $error = true ) {
if (is_search()){
$query->is_search = false; 
$query->query_vars[s] = false; 
$query->query[s] = false;    // to error 
if ( $error == true )  $query->is_404 = true;  
}
}
add_action( 'parse_query', 'fb_filter_query'); 
add_filter( 'get_search_form', create_function( '$a', "return null;" ) );

Dynamic Copyright Date in WordPress Footer

function wp_copyright() { 
global $wpdb;
$copyright_dates = $wpdb->get_results("SELECT YEAR(min(post_date_gmt)) AS firstdate, YEAR(max(post_date_gmt)) AS lastdate  FROM $wpdb->posts WHERE post_status = 'publish'  "); 
$output = ''; 
if($copyright_dates){ 
$copyright = "© " .$copyright_dates[0]->firstdate; 
if($copyright_dates[0]->firstdate != $copyright_dates[0]->lastdate) { 
$copyright .= '-' . $copyright_dates[0]->lastdate;
}
$output = $copyright;
}
return $output;
}
echo wp_copyright();

New htaccess Code for WordPress Permalinks

For Root Directory

# BEGIN WordPress  
<IfModule mod_rewrite.c>  
RewriteEngine On
RewriteBase /  
RewriteCond %{REQUEST_FILENAME} !-f  
RewriteCond %{REQUEST_FILENAME} !-d  
RewriteRule . /index.php [L]  
</IfModule>  
# END WordPress

For Subdirectory

# BEGIN WordPress
<IfModule mod_rewrite.c> 
RewriteEngine On 
RewriteBase /subdirectory/ 
RewriteRule ^index\.php$ - [L] 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule . /subdirectory/index.php [L] 
</IfModule> 
# END WordPress

How to Create and using custom hooks in WordPress

Step 1: Define custom hook

function dswebsolutions_hook() {
do_action('dswebsolutions_hook');
}

Step 2: Tag your custom hook

Place the function call in the desired location within your theme template:

dswebsolutions_hook();

Step 3: Add your function

function hello_wordpress() {  	
echo '<h1>Hello WordPress!</h1>';  
}
add_action('dswebsolutions_hook', 'hello_wordpress', 7);

How to create Google Maps Shortcode

//Google Maps Shortcode
function fn_googleMaps($atts, $content = null) {
extract(shortcode_atts(array(
"width" => '640',
"height" => '480',
"src" => ''
), $atts));
return '<iframe width="'.$width.'" height="'.$height.'" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="'.$src.'&amp;output=embed"></iframe>';
}
add_shortcode("googlemap", "fn_googleMaps");
[googlemap width="200" height="200" src="[url]"]

How to insert custom content after each post

// add custom post content
function add_post_content($content){
if(!is_feed() && !is_home()) {
$content .= '<p>This article is copyright &copy; '.date('Y').'&nbsp;'.bloginfo('name').'</p>';
}
return $content;
}
add_filter('the_content', 'add_post_content');

How to Register a Custom Menu in Wordpress Admin

function register_my_custom_menu_page(){
add_menu_page('custom menu title', 'custom menu', 'manage_options', 'myplugin/myplugin-admin.php', '', 'dashicons-admin-site', 6 );
}
add_action( 'admin_menu', 'register_my_custom_menu_page' );

How to add default avatar in Wordpress

function wp_addgravatar($avatar_defaults){
$myavatar = get_template_directory_uri() . '/images/avatar.png';
$avatar_defaults[$myavatar] = 'avatar';
return $avatar_defaults;
}
add_filter( 'avatar_defaults', 'wp_addgravatar' );

Automatically Link Featured Images to Posts

function wpb_autolink_featured_images( $html, $post_id, $post_image_id ) {
  if(! is_singular()) {
  $html = '<a href="' . get_permalink( $post_id ) . '" title="' . esc_attr( get_the_title( $post_id ) ) . '">' . $html . '</a>';
  return $html;
  } else {
  return $html;
  }
  }
  add_filter( 'post_thumbnail_html', 'wpb_autolink_featured_images', 10, 3 );

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