找出哪个 php 文件在 WordPress 中生成了 html 代码行的最佳方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27451369/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best way to find out which php file has generated an html line of code in WordPress
提问by chelder
Using Chrome you can right click - Inspect element to see the html code behind that element:
使用 Chrome,您可以右键单击 - 检查元素以查看该元素背后的 html 代码:
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?
我想知道如何找到哪个 php 文件生成了那个 html。也许有一些工具可以在 html 中放置一个断点,以便 php 服务器在尝试再次生成它时会停止?
I'm using WordPress locally. I'm on Ubuntu 14.04LMS. I'm using Sublime Text 3 with XDebug.
我在本地使用 WordPress。我在 Ubuntu 14.04LMS 上。我在 XDebug 中使用 Sublime Text 3。
UPDATE 1
更新 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):
一个 WordPress 插件能够在每个文件中放入类似以下内容的内容将是一个很好的解决方案(我认为从我初学者的角度来看):
echo '<!-- name_of_the_php_file.php -->';
采纳答案by Rituparna sonowal
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.
使用这个插件“ what the file”找出php文件。然后剩下的就是在任何编辑器中通过 CTRL + F 找出一个函数或其他东西。
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.
我想再添加一个插件,它不完全是为了这个目的,但它在 WordPress 开发人员中非常受欢迎,它很好地解决了这个问题。该插件是Query Monitor。
回答by developerwjk
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.
根据是否设置了会话变量,在 PHP 页面中放置一个 echo 以在 HTML 注释中打印它们的名称。然后设置该会话变量并享受。
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.
我说使用会话变量的原因是您可以创建一些机制,以便您偶尔可以出于调试目的查看它,但用户通常不会遇到它。
回答by David
Unfortunately with wordpress you need to do some digging:
不幸的是,使用 wordpress 你需要做一些挖掘:
- Is it a page/ post / custom post (i.e. you can find the page in the admin section and edit it)
- If yes - look the 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.
- 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.
- 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.
- If still no luck, have a look for
add_action('save_post', 'whatever');
there may be code statingreturn $template
, this will prob also have the post type in the function. Look for the path its calling - 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).
- 它是一个页面/帖子/自定义帖子(即您可以在管理部分找到该页面并进行编辑)
- 如果是 - 查看分配的模板(除非添加自定义代码,否则帖子通常没有模板选择字段)并在您的主题中找到匹配的页面模板(page-whatever.php)(它可能不会被称为您在下拉字段,但在其注释中,它将包含您看到的名称),或者如果您正在寻找 single.php 的帖子,或者如果自定义帖子寻找 single-whatever.php。
- 以上是默认值,将涵盖 60% 的情况。但是如果你添加一个插件并添加新页面,页面模板将在插件中,真正的乐趣开始了。
- 如果插件是在考虑到用户 mod 的情况下开发的,则它的页面应该位于 1 个位置或功能集文件夹中。如果存在或在常规插件文件夹中,请查看这些位置中的上述文件。
- 如果仍然没有运气,看看
add_action('save_post', 'whatever');
可能有代码说明return $template
,这可能在函数中也有post类型。寻找它调用的路径 - 添加页面的另一种方法是通过 url 重写(搜索文件进行重写)。它将包含文件某处的路径(有几种方法可以这样做,因此请查找它们以便您识别它们)。
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.
如果您认为您找到了正确的模板,请尝试回显以查看它是否出现在屏幕上。如果是,您已经找到了模板,请查看您遵循包含/要求来处理 .php 文件的内容,您会在某处找到您的 html,无论是作为 html 还是 php 代码创建 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
希望你能找到你要找的东西,但实际上并没有规则,其他方法也可以存在,包括 htaccess 重写以指向 php 文件等或动态创建页面的函数文件
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.
如果您曾经创建过一个完整的插件、逻辑地存储文件并从文件控制器或插件文件中包含,这可能是一个值得提及的地方。将页面存储在 pages 文件夹中,以便于查找。
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;
}
回答by StefanoWP
Hope to find this plugin useful. use to search in files amd wasting enough time. Also i use What the fileplugin as suggested before, it is a great way. But i think combining the String Locator this will have extra fun! String Locator
希望这个插件有用。用于在文件中搜索并浪费足够的时间。我也使用了之前建议的文件插件,这是一个很好的方法。但我认为结合 String Locator 这会带来额外的乐趣! 字符串定位器
回答by KawaiKx
Not all data is stored in html or php files in Wordpress.
并非所有数据都存储在 Wordpress 的 html 或 php 文件中。
It may be possible that the string you are looking for is coming from database since wordpress uses database to store variable data.
您要查找的字符串可能来自数据库,因为 wordpress 使用数据库来存储变量数据。
Please search database also
也请搜索数据库
回答by jay.jivani
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.
首先备份整个网站,假设 /my-website
1.右键单击要查找php代码的
元素 2.Cope CLASS或该行的
ID选择器 3.在/my-website文件夹内的编辑器中搜索该CLASS或ID选择器,对于这个notepad++和Dreamweaver编辑器是最好的。
回答by Buse G?nen
Start page code
起始页代码
<?php
ob_start();
?>
HTML content
HTML 内容
...
...
<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.
注意:对不起,我的英语不好。
回答by chelder
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.
另一种可能性是从 html 中复制一个 id 或 class。然后通过 WordPress Web 应用程序的源代码搜索该字符串,在该字符串出现的每个位置放置一个断点。
However, the string could be in the database. In that case, this method won't work.
但是,该字符串可能在数据库中。在这种情况下,此方法将不起作用。