admin管理员组

文章数量:1332383

I hope you can help me. How do I have to change the file path to get an specific image from my upload folder in my plugin/shortcode? I have added the following lines to my childs theme - functions.php but it doesn't work.

add_shortcode( 'webcamtime', 'webcamtime_timestamp' );
function webcamtime_init (){
    function webcamtime_timestamp() {
        $file = '/wp-content/uploads/webcam/webcamimage.jpg';
        $filename = basename($file);
        if (file_exists($filename)) {
            return "Last modified: " . date ("F d Y H:i:s.", filemtime($filename));
 }
}
}
add_action('init', 'webcamtime_init');

Thank you in advance!!

I hope you can help me. How do I have to change the file path to get an specific image from my upload folder in my plugin/shortcode? I have added the following lines to my childs theme - functions.php but it doesn't work.

add_shortcode( 'webcamtime', 'webcamtime_timestamp' );
function webcamtime_init (){
    function webcamtime_timestamp() {
        $file = '/wp-content/uploads/webcam/webcamimage.jpg';
        $filename = basename($file);
        if (file_exists($filename)) {
            return "Last modified: " . date ("F d Y H:i:s.", filemtime($filename));
 }
}
}
add_action('init', 'webcamtime_init');

Thank you in advance!!

Share Improve this question asked Jul 2, 2020 at 23:18 user190998user190998 2
  • 1 why don't you put the image within your child-theme folder? – Sabbir Hasan Commented Jul 2, 2020 at 23:51
  • Please specify what "it doesn't work" means. Also, the basename function only gives you the last bit (filename) of the full path to your file. It's no surprise that the file_exists function using that, will return false. You'll have to specify the full path to the file for that to work. – Coen Jacobs Commented Jul 5, 2020 at 6:52
Add a comment  | 

1 Answer 1

Reset to default 0

Use the ABSPATH constant defined in the wp-config.php:

$filename = ABSPATH . 'wp-content/uploads/webcam/webcamimage.jpg';

You don't have to use basename() function since it would return only the last component from the full file path, i.e. webcamimage.jpg, and it isn't what you want to use with filemtime() function.

本文标签: pluginsImage path in childs theme