26

Using Chrome you can right click - Inspect element to see the html code behind that element:

Chrome inspect element example

I wonder how to find which php file has generated that html. Maybe there is some tool to put a breakpoint into the html so the php server will stop when trying to generate it again?

I'm using WordPress locally. I'm on Ubuntu 14.04LMS. I'm using Sublime Text 3 with XDebug.


UPDATE 1

A WordPress plugin able to put, in every file, something like the following would be a good solution (I think from my beginner's viewpoint):

echo '<!-- name_of_the_php_file.php -->';
4
  • 1
    When inspecting an element with chrome (or other) developer tools, it might not be part of the actual html. It might also come from javascript
    – Gerd K
    Dec 21, 2014 at 15:01
  • In your sublime text editor use command+shift+f to find in all file i Yo Quiero!. i am sure it work fine Dec 22, 2014 at 11:14
  • 1
    @amitrh, WordPress usually stores that kind of text in the database, not the php file
    – chelder
    Dec 28, 2014 at 16:32
  • What about trying to find the source files of someone else's website? Dec 17, 2018 at 17:23

8 Answers 8

22
+100

Use this plugin "what the file" to find out the php file. Then rest it is to find out a function or something else by CTRL + F in any editor.

I want to add one more plugin which is not exactly for this purpose but it is very popular among WordPress developers and it solves this problem very well. The plugin is Query Monitor.

1
  • This plugin worked perfectly. An actually time saver. Thank you and welcome to stackoverflow Rituparna Sonowal!
    – chelder
    Dec 24, 2014 at 23:53
5

Put an echo in your PHP pages to print their name in an HTML comment based on whether a session variable is set. Then set that session variable and enjoy.

if(isset($_SESSION['printPageName']) && $_SESSION['printPageName']=='true')
{
   echo '<!-- ' . $_SERVER['PHP_SELF'] . ' -->';
}

The reason I say to use a session variable is so you can create some mechanism to where you can occasionally look at this for debug purposes, but users don't normally encounter it.

4
  • I put it in the header.html of the theme. Unfortunately, the output says: <!-- /websiteurl/index.php -->. index.php has the following line of code: require( dirname( __FILE__ ) . '/wp-blog-header.php' );
    – chelder
    Dec 12, 2014 at 21:48
  • 1
    $_SERVER['PHP_SELF'] will be the file actually being called by the user. So in that case I guess you'd have to hardcode the name in header.php echo '<!-- header.php -->'; or something like that. Dec 12, 2014 at 22:59
  • 1
    Yeah, that's a nice solution. Actually it would be nice a WordPress plugin to add that code at the beginning of every php file. Not a task for me (yet, as I'm starting with WordPress). Maybe in the future if there is not anything already done. Actually, I'll keep this question open a couple of days to motivate other solutions okay? Thank you!
    – chelder
    Dec 13, 2014 at 0:46
  • 2
    You could always just put in <?php echo "<!-- ".__FILE__."-->"; ?> on all your template files
    – Orangepill
    Dec 20, 2014 at 6:25
5

Unfortunately with wordpress you need to do some digging:

  1. Is it a page/ post / custom post (i.e. you can find the page in the admin section and edit it)
  2. If yes - look at the template assigned (posts don't usually have a template select field unless custom code added) and find the matching page template in your theme (page-whatever.php)(it might not be called what you see in the dropdown field, but in its notes it will have the name you see), or if a post you are looking for single.php or if custom post look for single-whatever.php.
  3. The above is default and will cover 60% of situations. But if you add a plugin and it adds new pages, the page template will be in the plugin and the real fun starts.
  4. If a plugin is developed with user mods in mind, it should have its pages in 1 location or in a functional set folder. Have a look for the above files in these locations if they exist or in the general plugin folders.
  5. If still no luck, have a look for add_action('save_post', 'whatever'); there may be code stating return $template , this will prob also have the post type in the function. Look for the path its calling
  6. Another way to add a page is through a url rewrite (search through the files for rewrite). It will contain a path somewhere to the file (there are a couple of methods of doing this so look them up so you will recognise them).

If you think you have found the correct template, try a echo to see if it appears on screen. If yes you have found the template, look at what you have follow the includes/ requires to appropiate .php files, you'll find your html somewhere either as html or php code creating html.

hopefully you find what you are looking for, but there are no rules really and other methods can exist including htaccess rewrites to point to php file, etc or a functions file creating pages on the fly

This might be a good point to mention if you ever create a entire plugin, store files logically and include from a file controller or plugin file. Store pages in a pages folder so they are easy to find.

Good luck!

update: filter to output path on template files

add_filter( 'template_include', 'show_path', 99 );

function show_path( $template ) {

            if($template):
                echo $template;
            else:
                echo "template path not found";
            endif;

    return $template;
}
12
  • well, a plugin to add the piece of code that @developerwjk has suggested in every php file would solve this common "problem". What do you think?
    – chelder
    Dec 13, 2014 at 0:53
  • 1
    hmm you could open every file and write it in with php i suppose, or do it manually, but honestly what i do is the above, i will find template files fairly quickly and then follow require/includes. You dont need a plugin for what he suggests, just use this filter to spit out the path codex.wordpress.org/Plugin_API/Filter_Reference/… again, this only works for pages/posts within db, it will fail for rewrites, etc and rewrites are used a lot for pages like payments pages, logins, etc.
    – David
    Dec 13, 2014 at 1:06
  • wow wow that plugin seems a nice (though as you said partial) solution. I don't know how to use it at all. I think I should create a new plugin with that example code, right? Then within the function portfolio_page_template, using the function single_tag_title() somehow. Is it what you mean? (I'm beginning to touch the source code of WordPress)
    – chelder
    Dec 13, 2014 at 13:18
  • i updated a filter to try. Put it in your theme function files or plugin file, etc
    – David
    Dec 13, 2014 at 17:39
  • I'm not sure how to use that piece of code. I put it within the index.php of the theme with no change. How to use it? Should I create a new public with that exact piece of code inside. Sorry I'm just starting with WordPress!
    – chelder
    Dec 14, 2014 at 21:00
2

Hope to find this plugin useful. use to search in files amd wasting enough time. Also i use What the file plugin as suggested before, it is a great way. But i think combining the String Locator this will have extra fun! String Locator

2
  • Thank you for sharing and welcome to stackoverflow! :)
    – chelder
    Nov 9, 2015 at 16:46
  • I didn't find about the plugin here, but after 4 hours looking for a link to change the href, I found about String Locator plugin. And I had to thank it somewhere, since I'm not registered in wordpress.org, I thought I'd upvote and leave a Thank You in stackoverflow. Don't waste any more time looking for hidden code
    – achasinh
    Oct 6, 2017 at 17:51
1

Another possibility would be to copy an id or class from the html. Then search that string through the source code of the WordPress web application to put a breakpoint at every place where the string appears.

However, the string could be in the database. In that case, this method won't work.

1

First tack a backup of whole websiter, suppose /my-website

1.Right click on element for that you want to find php code

2.Cope CLASS or ID selector of that line

3.Search that CLASS or ID selector in editor inside /my-website folder, for this notepad++ and Dreamweaver editor is best.

1

Start page code

<?php
ob_start();
?>

HTML content

...
...

<sapan>Original text</span>

....
...

end page code.

<?php
/*    get and delete current buffer      &&  start a new buffer */
if ((($html = ob_get_clean()) !== false) && (ob_start() === true))
{
    echo preg_replace('~<sapan>Original text</span>~i', '<!-- name_of_the_php_file.php -->', $html, 1);
}
?>

Note: Sory, my English not good.

1

Not all data is stored in html or php files in Wordpress.

It may be possible that the string you are looking for is coming from database since wordpress uses database to store variable data.

Please search database also

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.