0

I need to add a php code in a wordpress widget called PHP Code Widget which only asks for proper enclosing:

All PHP code must be enclosed in the standard php opening and closing tags ( <?php and ?> ) for it to be recognized and executed.

The code must use the current page title slug (i will use PAGES not posts) to look if in the upload folder there is any .jpg image named after the current page title SLUG. If there is such image, then it shows it:

<img src="current-page-name-slug.jpg" alt="current page title" height="image-height" width="image-width">

As you can see it must also use current page title for the image alt. And if possible, to retrieve image height and width and put them in their html parts. However this last one is not a must as i can manually resize all images to the same size and leave those fixed.

So the more or less pseudo code would be something like:

php start
IF file exists uploads/current-page-slug.jpg
then
<img src="current-page-name-slug.jpg" alt="current page title" height="image-height" width="image-width">
else
do nothing
php end

Ive read a bit about the wordpress loop, and im not sure if current page slug and current page title are reacheable from a sidebar widget.

Give me a hand please.

Update:

I have tried bklynM code. It is not working and when looking in the page sourcecode to see what is going this is what i find:

<aside id="execphp-3" class="widget widget_execphp"><h3 class="widget-title">Image:</h3> <div class="execphpwidget"><img src="//www.yourdomain.com/wp-content/uploads/$slug" title="<?php the_title_attribute(); ?>" alt="" ></div> </aside>

(ive edited domain name to keep it private) In my understanding the code is mostly not being evaluated. Base url is evaluated, but the:

$slug and

are untouched. Do you have any idea if maybe the plugin is preventing it from working. Also i use w3c total cache. Ive also updated my question to let clear that the img folder is fixed and images are not attached to posts in any way, only bulk uploaded through ftp to that folder. This is a way to programatically look if there is an image with the same permalink name (also called slug) that the page (again i dont use posts, i only use pages).What may am i missing here.

Update for bklinM:

Thank you very much this is working. However it does not excludes when there is no file so it shows an error on those situations. Ive read through link ive tried to add an else, to change it to a if_file_exists, to an is_file, and many other but they only break the code. Do you have any idea how to change the code so it does work only when it finds an image or an existing url to that file, so when there is no file no img html is shown. – Zacocom Zaccom

0

EDITED ANSWER - First get the full file path to your image and store it to a variable $filename. Use file_exists to check if image exists, and output the image and with it the title of your image. If the condition fails, there is no output. https://codex.wordpress.org/Function_Reference/wp_upload_dir

 <?php

    global $post;
    $slug = get_post( $post )->post_name;  //get current page slug
    $image_name = $post->post_title;  //get page name
    $upload_dir = wp_upload_dir(); 
    //file_exists needs to check full file path of your hard drive instead of url:
    $filename = $_SERVER['DOCUMENT_ROOT']."/wp-content/uploads/" . $slug. '.jpg'; 
    //echo $filename; 

    if (file_exists($filename)) {  //output only when matched
      echo '<img src="'. $upload_dir['baseurl']."/" . $slug.'.jpg'.'"  title="'.$image_name.'" alt="" >';  
    } else {}

    clearstatcache();  //clear cache results of file_exists
   ?>
  • I have tried the code. It is not working and when looking in the page sourcecode to see what is going this is what i find: <aside id="execphp-3" class="widget widget_execphp"><h3 class="widget-title">Image:</h3> <div class="execphpwidget"><img src="//www.yourdomain.com/wp-content/uploads/$slug" title="<?php the_title_attribute(); ?>" alt="" ></div> </aside> – Zacocom Zaccom Sep 6 '15 at 1:46
  • Read my answer as a question update on the top. – Zacocom Zaccom Sep 6 '15 at 2:00
  • I just edited my answer. $slug is being evaluated correctly now. – amespower Sep 6 '15 at 2:33
  • you can't have php open/close tags in an echo statement. also, you need to set the echo argument of the_title_attribute to false so it returns its value. – Milo Sep 6 '15 at 3:20
  • @Milo I had overlooked testing the_title_attribute earlier. This is outputting correctly now, see edit. Thanks. – amespower Sep 6 '15 at 15:02
0

get_queried_object() will give you an object containing the data for the current page, including slug and title.

You can use the code from this answer to get the attachment ID from the URL, which you can then use to get the attachment's metadata, which will give you the height and width.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.