Enter your keyword

Blogs

Registering the Widget Area in Genesis

The first step is to register the before header widget area through your functions file. Use the code below to do accomplish that:

<?php
//* Register before header widget area
genesis_register_sidebar( array(
'id' => 'before-header',
'name' => __( 'Before Header', 'theme-name' ),
'description' => __( 'This is the before header widget area.', 'theme-name' ),
) ); 
?>

Hooking the Widget Area

Now that you have registered the before header widget area, you need to place that onto a hook that resides before the site header markup.

The code below will do that for you:

<?php
//* Hook before header widget area before site header
add_action( 'genesis_before_header', 'bg_before_header_widget_area' );
function bg_before_header_widget_area() {
genesis_widget_area( 'before-header', array(
'before' => '<div class="before-header widget-area"><div class="wrap">',
'after' => '</div></div>',
) );
} ?>

Custom Shortcode For Call WordPress Menu

function menu($atts, $content = null)
    {
        extract(shortcode_atts(array(
                'theme_location' => '',
                'menu' => '',
                'container' => 'div',
                'container_class' => '',
                'container_id' => '',
                'menu_class' => 'menu',
                'menu_id' => '',
                'echo' => true,
                'fallback_cb' => 'wp_page_menu',
                'before' => '',
                'after' => '',
                'link_before' => '',
                'link_after' => '',
                'depth' => 0,
                'walker' => ''
        ) , $atts));
        return wp_nav_menu(array(
                'theme_location' => $theme_location,
                'menu' => $menu,
                'container' => $container,
                'container_class' => $container_class,
                'container_id' => $container_id,
                'menu_class' => $menu_class,
                'menu_id' => $menu_id,
                'echo' => false,
                'fallback_cb' => $fallback_cb,
                'before' => $before,
                'after' => $after,
                'link_before' => $link_before,
                'link_after' => $link_after,
                'depth' => $depth,
                'walker' => $walker
        ));
    }
// Create the shortcode 
add_shortcode("menu", "menu");
[menu menu=Top Navigation menu_class=main_menu]  // Here "Top Navigation" is Menu Name

Remove Particular Body Class in WordPress

add_filter('body_class', 'remove_body_class', 20, 2);
function remove_body_class($wp_classes)
    {
        foreach($wp_classes as $key =--> $value)
            {
                if ($value == 'right-sidebar') unset($wp_classes[$key]);
            }
        return $wp_classes;
    }

Custom WordPress Dashboard Widget

function register_my_dashboard_widget() {
global $wp_meta_boxes;
wp_add_dashboard_widget(
'my_dashboard_widget',
'Publication Schedule',
'my_dashboard_widget_display'
);
$dashboard = $wp_meta_boxes['dashboard']['normal']['core'];
$my_widget = array( 'my_dashboard_widget' => $dashboard['my_dashboard_widget'] );
unset( $dashboard['my_dashboard_widget'] );
$sorted_dashboard = array_merge( $my_widget, $dashboard );
$wp_meta_boxes['dashboard']['normal']['core'] = $sorted_dashboard;
}
add_action( 'wp_dashboard_setup', 'register_my_dashboard_widget' );
function my_dashboard_widget_display() {
?>
<p>
Don't forget that there are three slots for publication each day. <strong>8am, 1pm and 6pm</strong> EDT. Posts must be finalized 12 hours before their publication time. This includes final checks and proofreading.
</p> 
<p>
For particularly time-sensitive posts please contact the editor in chief to make sure the required time does not conflict with any ongoing projects.
</p> 
<?php
}

Custom WordPress Dashboard Notification

function dsweb_admin_notice() {
        echo '<div class="updated"><p>';
        echo 'This is an admin notice just for you, some username.';
        echo '</p></div>';

}
add_action( 'admin_notices', 'dsweb_admin_notice' );

Custom Login Form in WordPress Frontend

// login form fields
function dsweb_login_form_fields() {
  ob_start(); 
    // show any error messages after form submission
     ?>
    <form id="loginform"  class="dsweb_form" action="" method="post">
      <fieldset>
        <p class="login-username">
          <label for="dsweb_user_Login">Username or Email</label>
          <input name="dsweb_user_login" id="dsweb_user_login" class="required" type="text"/>
        </p>
        <p class="login-password">
          <label for="dsweb_user_pass">Password</label>
          <input name="dsweb_user_pass" id="dsweb_user_pass" class="required" type="password"/>
        </p>
      <p class="login-remember"><label><input type="checkbox" value="forever" id="rememberme" name="rememberme"> Remember Me</label></p>
        <p class="login-submit">
          <input type="hidden" name="dsweb_login_nonce" value="<?php echo wp_create_nonce('dsweb-login-nonce'); ?>"/>
          <input class="button-primary" id="dsweb_login_submit" type="submit" value="Login"/>
        </p>
       <p class="lost-password-link">
        <a href="<?php echo esc_url( get_permalink(get_page_by_path('lost-password') ) ); ?>"><?php _e( 'Lost your password?', 'ib-educator' ); ?></a>
        </p>
      </fieldset>
    </form>
  <?php
  return ob_get_clean();
}
// logs a member in after submitting a form
function dsweb_login_member() {
  if(isset($_POST['dsweb_user_login']) && wp_verify_nonce($_POST['dsweb_login_nonce'], 'dsweb-login-nonce')) {
    // this returns the user ID and other info from the user name
  if ( is_email( $_POST['dsweb_user_login'] ) ) {
        $user = get_user_by_email( $_POST['dsweb_user_login'] );
        $username = $user->user_login;
    } else {
    $user = get_userdatabylogin($_POST['dsweb_user_login']);
    $username = $user->user_login;
  }

    if(!$user) {
      // if the user name doesn't exist
      dsweb_errors()->add('empty_username', __('Invalid username'));
    }
 
    if(!isset($_POST['dsweb_user_pass']) || $_POST['dsweb_user_pass'] == '') {
      // if no password was entered
      dsweb_errors()->add('empty_password', __('Please enter a password'));
    }
 
    // check the user's login with their password
    if(!wp_check_password($_POST['dsweb_user_pass'], $user->user_pass, $user->ID)) {
      // if the password is incorrect for the specified user
      dsweb_errors()->add('empty_password', __('Incorrect password'));
    }
 
    // retrieve all error messages
    $errors = dsweb_errors()->get_error_messages();
 
    // only log the user in if there are no errors
    if(empty($errors)) {	
      wp_setcookie($username, $_POST['dsweb_user_pass'], true);
      wp_set_current_user($user->ID, $username );
      do_action('wp_login', $username);
      wp_redirect(home_url().'/account/my-modules/'); exit;
    }
  }}
add_action('init', 'dsweb_login_member');
// used for tracking error messages
function dsweb_errors(){
    static $wp_error; // Will hold global variable safely
    return isset($wp_error) ? $wp_error : ($wp_error = new WP_Error(null, null, null));
}

// displays error messages from form submissions
function dsweb_show_error_messages() {
  if($codes = dsweb_errors()->get_error_codes()) {
    echo '<div class="dsweb_errors">';
        // Loop error codes and display errors
       foreach($codes as $code){
            $message = dsweb_errors()->get_error_message($code);
            echo '<span class="error"><strong>' . __('Error') . '</strong>: ' . $message . '</span><br/>';
        }
    echo ' </div>';
  }	
}

How to add dynamic Author Emails in Contact Form 7

function author_email($tag)  {
$curauth = ( get_query_var( 'author_name' ) ) ? get_user_by( 'slug', get_query_var( 'author_name' ) ) : get_userdata( get_query_var( 'author' ) );
  $view = get_query_var( 'view' );
  $role = '';
  if ( in_array( 'author_role', $curauth->roles ) ) {
    $role = 'author_role';
  }
  $html = $curauth->user_email;
  return $html;
}
add_shortcode('authormail', 'author_email');

Use this Short Code in Contact form 7 => [dynamichidden email-author "authormail"] and use the contact Email shortcode  [email-author]

How to Add Author Links in WordPress Admin

function add_to_author_profile( $contactmethods ) {
  $contactmethods['facebook_profile'] = 'Facebook Profile URL';
  $contactmethods['twitter_profile'] = 'Twitter Profile URL';
  $contactmethods['linkedin_profile'] = 'Linkedin Profile URL';
  
  return $contactmethods;
}
add_filter( 'user_contactmethods', 'add_to_author_profile', 2, 1);

Remove Admin Help Tab in WordPress

add_action('admin_head', 'dsweb_remove_help_tabs');
function dsweb_remove_help_tabs() {
    $screen = get_current_screen();
    $screen->remove_help_tabs();
}

WordPress wp_query With Custom Pagiantion

<?php
//The Query
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
        'post_type' => 'post',
        'paged' => $paged,
        'order' => 'DESC',
        'posts_per_page' => '5'
);
$query = new WP_Query($args);
 
if ($query->have_posts())
while ($query->have_posts()) : $query->the_post();
        echo '<h1>' . get_the_title() . '</h1>';
        the_excerpt();
endwhile; // end of the loop.
 
/* Pagination  Code Start  */
global $wp_query;
$big = 999999999; // need an unlikely integer
$totalpages = $query->max_num_pages;
$current = max(1, get_query_var('paged'));
$paginate_args = array(
        'base' => str_replace($big, '%#%', esc_url(get_pagenum_link($big))) ,
        'format' => '?paged=%#%',
        'current' => $current,
        'total' => $totalpages,
        'show_all' => False,
        'end_size' => 1,
        'mid_size' => 3,
        'prev_next' => True,
        'prev_text' => __('« Previous') ,
        'next_text' => __('Next »') ,
        'type' => 'plain',
        'add_args' => False,
        'add_fragment' => '',
        'before_page_number' => '',
        'after_page_number' => ''
);
$pagination= paginate_links($paginate_args);
echo "<nav class='pagination'>";
echo "<span class='page-numbers page-no'>Page " . $paged . " of " . $totalpages . "</span> ";
echo $pagination;
echo "</nav>";
 
/* Pagination  Code End  */

[/spb_text_block] [spb_text_block pb_margin_bottom="no" pb_border_bottom="no" width="1/1" el_position="first last"]

/* ADD This CSS For Set Design Of Pagination */
.pagination span,
.pagination a {
  display: inline-block;
  padding: 2px 10px;
}
.pagination a {
    background-color: #000;
    color: #fff;
    text-decoration: none;
}
.pagination a:hover {
  background-color: #ff3c50;
  color: #fff;
}
.pagination span.page-no {
  margin-right: 10px;
  padding: 0;
}
.pagination span.dots {
  padding: 0;
  color: black;
}
.pagination span.current {
  background-color: #bb1e32;
  color: #fff;
}

Allow Email Login Username or Email Admin and Frontend

add_filter('authenticate', 'dsweb_allow_email_login', 20, 3);
function dsweb_allow_email_login( $user, $username, $password ) {
    if ( is_email( $username ) ) {
        $user = get_user_by_email( $username );
        if ( $user ) $username = $user->user_login;
    }
    return wp_authenticate_username_password( null, $username, $password );
}

How to Get User IP in WordPress

function get_the_user_ip() {
if ( ! empty( $_SERVER['HTTP_CLIENT_IP'] ) ) {
//check ip from share internet
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif ( ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
//to check ip is pass from proxy
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}
return apply_filters( 'wpb_get_ip', $ip );
}

How to Add CSS file into Admin area

Loads a CSS file into your admin area so you can override default styles. Just add wp-admin.css into your theme folder.

<?php 
// Custom CSS for the whole admin area
// Create wp-admin.css in your theme folder
function wpmy_adminCSS() {
  echo '<link rel="stylesheet" type="text/css" href="'.get_bloginfo('template_directory').'/wp-admin.css"/>';
}
add_action('admin_head', 'wpmy_adminCSS');
?>

Create custom widget area in wordpress

Custom widget area is enabled from function.php that is available in the themes folder.Add the following code in widgets function  to add new widget area in the wordpress theme:

register_sidebar( array(
'name'          => __( 'Custom Widgets ', 'twentythirteen' ),
'id'            => 'custom-Widget,
'description'   => __( 'Appears on posts and pages in the sidebar.', 'twentythirteen' ),
'before_widget' => '<aside id="%1$s">',
'after_widget'  => '</aside>',
'before_title'  => '<h3>',
'after_title'   => '</h3>',
) );

Or

You Will Be Download free Plugin Multiple Sidebar Generator from Our Site.

Use different single post page in WordPress

WordPress, by default  use one "single.php" for every post. if you want to display a different layout for specific categories  post ?

To Use different single post page we use simple php code in theme "single.php" file.and create category specific single.php files in theme folder.

In the below example lets assume the following:

  • Category 2 is Blog
  • Category 3 is Articles section
  • Category 4 is News section

Create category specific  files in them folder  like as:

single-blog.php  (This file use for Blog category posts )

single-articles.php (This file use for Articles category posts )

single-news.php (This file use for News category posts )

single-default.php (This file use for any post that is not in any of the specified categories (2,3,4) will just use a default template file – "single-default.php")

Put this code in "single.php" as follows:

<?php
$post = $wp_query->post;
if ( in_category('2') )
{
include(TEMPLATEPATH . '/single-blog.php');
}
elseif ( in_category('3') )
{
include(TEMPLATEPATH . '/single-articles.php');
}
elseif ( in_category('4') )
{
include(TEMPLATEPATH . '/single-news.php');
}
else
{
include(TEMPLATEPATH . '/single-default.php');
}
?>
//In this code 2 is Blog Category ID , 3 is articles Category ID and 4 is News Category ID.

How to Custom Filter Dropdown list in Wordpress admin

 function restrict_category() {
		global $typenow;
		$post_type = 'articles'; // change HERE
		$taxonomy = 'article_category'; // change HERE
		if ($typenow == $post_type) {
			$selected = isset($_GET[$taxonomy]) ? $_GET[$taxonomy] : '';
			$info_taxonomy = get_taxonomy($taxonomy);
			wp_dropdown_categories(array(
				'show_option_all' => __("Show All {$info_taxonomy->label}"),
				'taxonomy' => $taxonomy,
				'name' => $taxonomy,
				'orderby' => 'name',
				'selected' => $selected,
				'show_count' => true,
				'hide_empty' => true,
			));
		};
	}

	add_action('restrict_manage_posts', 'restrict_category');

How to User ID Encryption and Decryption

Use this Function in function.php

function user_encrypt($sData){
$idus=(double)$sData*425325.24;
return base64_encode($idus);
}

function user_decrypt($sData){
$url_id=base64_decode($sData);
$idus=(double)$url_id/425325.24;
return $idus;
}
echo user_encrypt($user_id);
echo user_decrypt($user_id);