Enter your keyword

Blogs

How to add New Field in General Settings in WordPress

add_filter('admin_init', 'my_general_settings_on_dswebsolution');
function my_general_settings_on_dswebsolution()
{
    register_setting('general', 'general_field_dswebsolution', 'esc_attr');
    add_settings_field('general_field_dswebsolution', '<label for="general_field_dswebsolution">'.__('General Field Name' , 'general_field_dswebsolution' ).'</label>' , 'my_general_settings_dswebsolution_html', 'general');
}
function my_general_settings_dswebsolution_html()
{
    $value = get_option( 'general_field_dswebsolution', '' );
    echo '<input type="text" id="general_field_dswebsolution" name="general_field_dswebsolution" value="' . $value . '" />';
}

How to get User Role in WordPress

 function get_user_role() {
     global $current_user;
     $user_roles = $current_user->roles;
     $user_role = array_shift($user_roles);
     return $user_role; 
 }

How to Add a custom user role in WordPress

$result = add_role( 'designer', __('Designer' ),
	array(
    	    'read' => true, // true allows this capability
            'edit_posts' => true, // Allows user to edit their own posts
            'edit_pages' => true, // Allows user to edit pages
            'edit_others_posts' => true, // Allows user to edit others posts not just their own
            'create_posts' => true, // Allows user to create new posts
            'manage_categories' => true, // Allows user to manage post categories
            'publish_posts' => true, // Allows the user to publish, otherwise posts stays in draft mode
            'edit_themes' => false, // false denies this capability. User can't edit your theme
            'install_plugins' => false, // User cant add new plugins
            'update_plugin' => false, // User can't update any plugins
            'update_core' => false // user cant perform core updates
            )
       );

How to add Custom Status in Post/Custom Post Type in WordPress

function unapproved_post_status(){
    register_post_status( 'unapproved', array(
    'label'                     => _x( 'Unapproved', 'post' ),
    'public'                    => true,
    'exclude_from_search'       => false,
    'show_in_admin_all_list'    => true,
    'show_in_admin_status_list' => true,
    'label_count'               => _n_noop('Unapproved <span class="count">(%s)</span>', 'Unapproved <span class="count">(%s)</span>' ), ) );
}
add_action( 'init', 'unapproved_post_status' );

How to change Woocommerce Currency Symbol

add_filter( 'woocommerce_currencies', 'add_inr_currency');
add_filter( 'woocommerce_currency_symbol', 'add_inr_currency_symbol');
function add_inr_currency( $currencies ) {
$currencies['INR'] = 'INR';
return $currencies;
}
function add_inr_currency_symbol( $symbol ) {
$currency = get_option( 'woocommerce_currency' );
switch( $currency ) {
case 'INR': $symbol = 'Rs.'; break;
}
return $symbol;
}

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