Created
December 1, 2019 07:58
-
-
Save machouinard/63202a763c80c865d4d94a72710781a4 to your computer and use it in GitHub Desktop.
Custom WordPress login logo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Chouinard\Logo; | |
/** | |
* Class MCLoginLogo | |
* @package Chouinard\Logo | |
*/ | |
class MCLoginLogo { | |
private $logo_path; | |
private $logo; | |
private $width; | |
private $height; | |
/** | |
* MCLoginLogo constructor. | |
*/ | |
public function __construct() { | |
// Set logo path. | |
$this->logo_path = '/login-logo.png'; | |
$this->width = '312px'; | |
$this->height = '100px'; | |
// Link login logo to home URL. | |
add_filter( | |
'login_headerurl', | |
[ $this, 'header_url' ] | |
); | |
// Add remaining filters only if we have a logo file. | |
if ( $this->logo = get_stylesheet_directory_uri() . $this->logo_path ) { | |
add_filter( | |
'login_headertext', | |
'__return_empty_string' | |
); | |
add_action( | |
'login_head', | |
[ $this, 'login_logo' ] | |
); | |
} | |
} | |
/** | |
* URL for logo anchor | |
* | |
* @param $url | |
* | |
* @return string | |
* @since 1.0.0 | |
*/ | |
public function header_url( $url ) { | |
return esc_url( home_url() ); | |
} | |
/** | |
* Output CSS for login logo | |
* | |
* @return void | |
* @since 1.0.0 | |
*/ | |
public function login_logo() { | |
if ( ! file_exists( get_stylesheet_directory() . $this->logo_path ) ) { | |
return; | |
} | |
?> | |
<style type="text/css"> | |
.login h1 a { | |
background-image: url(<?php echo $this->logo;?>); | |
background-size: contain; | |
background-repeat: no-repeat; | |
background-position: center center; | |
display: block; | |
overflow: hidden; | |
text-indent: -9999em; | |
width: <?php echo $this->width ?>; | |
height: <?php echo $this->height ?>; | |
} | |
</style> | |
<?php | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment