Month: January 2017

Load default jQuery core file from CDN to WordPress

To load default jquery core file from CDN to WordPress, you need to simply copy and paste the following code in your theme’s functions.php file.

function replace_jquery() {
	if (!is_admin()) {
		wp_deregister_script('jquery');
		wp_register_script('jquery', 'https://code.jquery.com/jquery-1.12.4.min.js', false, '1.12.4');
		wp_enqueue_script('jquery');
	}
}
add_action('init', 'replace_jquery');

Have any doubt, then comment here!

How to add new menu/tab to WordPress toolbar

To add a new menu/tab to the WordPress toolbar, you need to simply copy and paste the following code in your theme’s functions.php file.

add_action( 'admin_bar_menu', 'add_toolbar_link_top', 999 );
function add_toolbar_link_top( $wp_admin_bar ) 
{	
	$args = array(
		'id'    => 'your-id',
		'title' => 'Your Title',
		'href'  => site_url(),
		'meta'  => array( 'class' => 'your-class-name' )
	);
	$wp_admin_bar->add_node( $args );
}

This code will add new menu in WordPress toolbar.

Have any doubt, then comment here!

How to save and get user profile modified date in WordPress

WordPress doesn’t have an default option to save modified date. So we have save modified date when user updating their profile.
You can save the modified date by adding the following code in your theme’s functions.php file.

/*Save profile modified date*/
function update_profile_modified_date( $user_id ) {
  update_user_meta( $user_id, 'modified_date', date("Y-m-d") );
}
add_action( 'profile_update', 'update_profile_modified_date' );

You can get this date by using get_user_meta() function. for example see below code.

$user_id=25; // replace your user id here
$modified_date = get_user_meta( $user_id, 'modified_date', true ); 

Have any doubt, then comment here!

Redirect back to current page after logout in WordPress

We have a filter in WordPress for set url to redirect after logout. You can use this filter to redirect back to current page after logout. Add the following code in your theme’s functions.php file..

/* Logout and return to the page they were on*/
function admin_logout_redirect($logouturl, $redir)
    {
        return $logouturl . '&redirect_to=http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
    }
add_filter('logout_url', 'admin_logout_redirect', 10, 2);

Have any doubt, then comment here!

What are benefits of htaccess?

We can do following benefits stuff with htaccess.

  1. Routing the URL
  2. Mange Error Pages for Better SEO
  3. Redirection pages
  4. Detect OS (like Mobile/Laptop/Ios/Android etc)
  5. Set PHP Config variable
  6. Set Environment variable
  7. Allow/Deny visitors by IP Address
  8. Password protection for File/Directory
  9. Optimize Performance of website
  10. Improve Site Security

Disable comments section for a Single Page

You have to follow the below steps to disable comment section for a single page.

  1. Go to Dashboard -> Pages
  2. Select the particular page you want to disable comment.
  3. Click on Screen Options tab and check the box beside Discussion WordPress Screen Options
  4. Now scroll down to the bottom, you will find a discussion section. Uncheck Allow Comments & Allow trackbacks and pingbacks on this page.
  5. And Click Publish/Update

Have any doubt, then comment here!

Remove WordPress emoji code programmatically without plugin

WordPress added the following code in your header for support emoji.

window._wpemojiSettings = {"baseUrl":"http:\/\/s.w.org\/images\/core\/emoji\/72x72\/","ext":".png","source":{"concatemoji":"http:\/\/your-url\/wp-includes\/js\/wp-emoji-release.min.js?ver=4.2.1"}};
    !function(a,b,c){function d(a){var c=b.createElement("canvas"),d=c.getContext&&c.getContext("2d");return d&&d.fillText?(d.textBaseline="top",d.font="600 32px Arial","flag"===a?(d.fillText(String.fromCharCode(55356,56812,55356,56807),0,0),c.toDataURL().length>3e3):(d.fillText(String.fromCharCode(55357,56835),0,0),0!==d.getImageData(16,16,1,1).data[0])):!1}function e(a){var c=b.createElement("script");c.src=a,c.type="text/javascript",b.getElementsByTagName("head")[0].appendChild(c)}var f;c.supports={simple:d("simple"),flag:d("flag")},c.supports.simple&&c.supports.flag||(f=c.source||{},f.concatemoji?e(f.concatemoji):f.wpemoji&&f.twemoji&&(e(f.twemoji),e(f.wpemoji)))}(window,document,window._wpemojiSettings);
img.wp-smiley,
img.emoji {
    display: inline !important;
    border: none !important;
    box-shadow: none !important;
    height: 1em !important;
    width: 1em !important;
    margin: 0 .07em !important;
    vertical-align: -0.1em !important;
    background: none !important;
    padding: 0 !important;
}

you can remove this WordPress emoji code by WordPress actions. Add the following codes in your functions.php file located in your theme folder.

/*Remove WordPress emoji code programmatically without plugin*/
remove_action('wp_head', 'print_emoji_detection_script', 7);
remove_action('wp_print_styles', 'print_emoji_styles');

remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
remove_action( 'admin_print_styles', 'print_emoji_styles' );

Have any doubt, then comment here!