0

I have the following function that I'm trying to use to get an image in the same directory as the plugin but inside the images folder.

function my_login_logo() { ?>
<style type="text/css">
    #login h1 a, .login h1 a {
        background-image: url(<?php echo dirname(__FILE__); ?>/images/logo3.png);
    height:65px;
    width:320px;
    background-size: 320px 65px;
    background-repeat: no-repeat;
        padding-bottom: 30px;
    }
</style>

its is taken from the following, which works fine. yes "/" works.

function my_login_logo() { ?>
<style type="text/css">
    #login h1 a, .login h1 a {
        background-image: url(<?php echo get_stylesheet_directory_uri(); ?>/images/site-login-logo.png);
    height:65px;
    width:320px;
    background-size: 320px 65px;
    background-repeat: no-repeat;
        padding-bottom: 30px;
    }
</style>

What am I doing wrong?

Theo E
  • 77
  • 2
  • 11
  • what does the rendered css look like (right click -> view source)? – Jeff Oct 19 '18 at 22:30
  • shouldn't url have `'` around the path? – Jeff Oct 19 '18 at 22:33
  • 1
    With `dirname(__FILE__)` you get the absolute path from the server's file system root (e.g. `/var/www/html/images/logo3.png`). To use it in CSS you need the relative path from the webserver's document root, which is a different path. For example, if the image is in `/var/www/html/images/logo3.png` and the webserver's document root is `/var/www/html`, you need to use `/images/logo3.png` in CSS. – rickdenhaan Oct 19 '18 at 22:36
  • Ok, so how do I get the URI of the plugin path? – Theo E Oct 19 '18 at 22:46
  • I'm not sure if Wordpress has a function for it. If not, you can try one of the methods in the answers to [this question](https://stackoverflow.com/q/1240462/1941241) – rickdenhaan Oct 19 '18 at 23:08

1 Answers1

0

Is this what you are looking for?

https://developer.wordpress.org/reference/functions/plugin_dir_path/

https://codex.wordpress.org/Function_Reference/plugins_url

Maybe next time also point out whats the result of the 'not working' one

Why its not working?

Dirname will return abs path, which if you try to access from browser will not resolve. The path you want is relative to wp root.

Nikko Khresna
  • 1,024
  • 8
  • 10