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?

5
  • what does the rendered css look like (right click -> view source)? Commented Oct 19, 2018 at 22:30
  • shouldn't url have ' around the path? Commented Oct 19, 2018 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. Commented Oct 19, 2018 at 22:36
  • Ok, so how do I get the URI of the plugin path? Commented Oct 19, 2018 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 Commented Oct 19, 2018 at 23:08

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.