How to log in a WordPress user programmatically
When you are building a WordPress based websites, it can be very useful to know how to programmatically log in a WordPress user. Here is a function that can do it easily.
Here is the function, drop it in your functions.php file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <?php function auto_login( $user ) { $username = $user; if ( !is_user_logged_in() ) { $user = get_userdatabylogin( $username ); $user_id = $user->ID; wp_set_current_user( $user_id, $user_login ); wp_set_auth_cookie( $user_id ); do_action( 'wp_login', $user_login ); } } ?> |
Then, to log in a user, do the following:
1 2 3 4 5 | <?php auto_login( 'admin' ) ?> |
How to Disable Google Fonts in WordPress
Google fonts are very more popluar, many developers employ it in WordPress theme,I have to say the WordPress dashboard also use google fonts.Now,there is a question that come in my head, how to disable google fonts in my WordPress?
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?php add_filter( 'gettext_with_context', 'wpjam_disable_google_fonts', 888, 4); function wpjam_disable_google_fonts($translations, $text, $context, $domain ) { $google_fonts_contexts = array('Open Sans font: on or off','Lato font: on or off','Source Sans Pro font: on or off','Bitter font: on or off'); if( $text == 'on' && in_array($context, $google_fonts_contexts ) ){ $translations = 'off'; } return $translations; } ?> |
Auto Login and Redirct to Custom page after WordPress Registration
If you want ro auto user login after registration and custom url for redirect after login, then use the below code snipats :
1 2 3 4 5 6 7 8 9 10 | // Auto login and redirect to a page function auto_login_new_user( $user_id ) { wp_set_current_user($user_id); wp_set_auth_cookie($user_id); // You can change home_url() to the specific URL,such as wp_redirect( 'https://www.freewebmentor.com' ); wp_redirect( home_url() ); exit; } add_action( 'user_register', 'auto_login_new_user' ); |
How to use PHP in WordPress widgets
Just add following snippet to your current theme’s functions.php file:
1 2 3 4 5 6 7 8 9 10 | add_filter('widget_text', 'php_text', 99); function php_text($text) { if (strpos($text, '<' . '?') !== false) { ob_start(); eval('?' . '>' . $text); $text = ob_get_contents(); ob_end_clean(); } return $text; } |
How to use shortcodes in WordPress sidebar widgets
I think you know it,shortcode is useful.But you may can’t use shortcode in sidebar widget.To allow shortcodes in sidebar widgets, simply edit the functions.php file from your them and add the following code:
1 2 | add_filter( 'widget_text', 'shortcode_unautop'); add_filter( 'widget_text', 'do_shortcode'); |
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.
Article Tags: Tip and tricks, wordpress