我正在创建一个新的Wordpress网站,除了视频等其他媒体外,该网站还将包含数千本在线书籍和文章供阅读。
我不想通过粘贴数万页的文本来扩充我的数据库,所以我想将书籍章节创建为html,然后将这些html文件嵌入Wordpress帖子中。我相信这将大大减少数据库大小。
我上传了一个测试html文件here. 浏览器可以读取该文件,因此文件中没有错误。
我安装了最新版本的Gutenberg,并尝试将此url嵌入Embed URL 阻止,但当我单击Embed, 我收到以下消息:Sorry, we could not embed that content.
我按照答案this StackExchange question (question #238330), 和the the answer to this other StackExchange question (question #277163), 但是没有运气。
当我在中使用代码时question #238330 answer, 我可以从中嵌入视频forbes.com 使用Gutenberg和默认WordPress post编辑器成功。
然而,当我将代码更改为从我的网站读取时,什么都没有发生(即url在帖子中显示为一行文本),所以很明显我在代码中做了一些错误的事情。
以下是我修改的代码:
/**
* Embed support for Coptic Treasures Texts
*
* Usage Example:
*
* https://coptic-treasures.com/html-test-filed/02.html
*/
add_action( \'init\', function()
{
wp_embed_register_handler(
\'coptic-treasures\',
\'#http://www\\.coptic-treasures\\.com/?#i\',
\'wp_embed_handler_coptic_treasures\'
);
} );
function wp_embed_handler_coptic_treasures( $matches, $attr, $url, $rawattr )
{
$embed = sprintf(
\'<iframe class="coptic-treasures-texts" src="https://coptic-treasures.com/html-test-filed/%1$s.html" width="600" height="400" frameborder="0" scrolling="no"></iframe>\',
esc_attr( $matches[1] )
);
return apply_filters( \'embed_coptic_treasures\', $embed, $matches, $attr, $url, $rawattr );
}
My questions are:
1-有人能告诉我修改后的代码中有哪些错误吗?
2-我可以在没有iFrame的情况下嵌入吗?我的网站响应迅速,我希望文本在页面中无缝,并能被谷歌发现。
我以前尝试过一个插件,它可以获得所需的结果,但最终没有使用它,因为它的代码中有一个错误,如果一个url错误,就会导致消耗所有服务器资源。它也是一个未维护的插件。
提前谢谢。
EDIT
我对代码进行了一些修改,现在它正在嵌入文本,然而,我仍然面临代码中的一个问题。
问题是,对于我来说,要嵌入此url:https://coptic-treasures.com/html-test-filed/02.html, 我必须使用这个代码https://coptic-treasures.com/02.html
这是因为这部分html-test-filed/ 添加到我正在使用的函数中。我试图用变量替换它,但未能成功完成。
我希望能够更改嵌入文件的url。
谢谢
/**
* Embed support for Coptic Treasures Texts
*
* Usage Example:
*
* https://coptic-treasures.com/html-test-filed/02.html
*/
add_action( \'init\', function()
{
wp_embed_register_handler(
\'coptic-treasures\',
\'#https://coptic-treasures.com/([a-z0-9_-]+)\\.html$#i\',
\'wp_embed_handler_coptic_treasures\'
);
} );
function wp_embed_handler_coptic_treasures( $matches, $attr, $url, $rawattr )
{
$embed = sprintf(
\'<iframe class="coptic-treasures-texts" src="https://coptic-treasures.com/html-test-filed/%1$s.html" width="1200" height="1200" frameborder="0" scrolling="no"></iframe>\',
esc_attr( $matches[1] )
);
return apply_filters( \'embed_coptic_treasures\', $embed, $matches, $attr, $url, $rawattr );
}
最合适的回答,由SO网友:birgire 整理而成
修复regex模式以匹配以下类型的url:
https://coptic-treasures.com/
{Some string with a mix of a-z letters and hyphen}/{Some number}.html
如以下示例:
https://coptic-treasures.com/html-test-filed/02.html
您可以尝试这种模式:
\'#https://coptic-treasures.com/([a-z-]+)/([0-9]+)\\.html$#i\'
然后必须相应地更新iframe输出:
... src="https://coptic-treasures.com/%1$s/%2$s.html" ...
具有相应的匹配项。
演示下面是一个测试运行:

这将产生:

一些注释
据我所知,嵌入编写了一些post元数据,因此您的方法无法避免数据库操作。
但许多WordPress安装有数以万计的非层次化自定义帖子,没有重大问题,所以我不排除这个选项。请确保您有一个好的托管提供商,该提供商为您提供最新的PHP版本等。如果搜索速度太慢,则有一些第三方解决方案和服务可用。
请注意,WordPress管理界面有这么多分层帖子,这可能是一个问题,因为父下拉菜单获取所有页面。
在WordPress后端编辑数据(利用修订版和分类法)也可能更容易,而不是每次都找到相应的裸文件,而是打开它并以这种方式进行修改。除非它是从您现有的另一个系统导出的。
另一种方法可以使用,例如Vue/React/。。。JS并将纯数据提取为json(p)资源,并根据需要对其进行格式化,但我不确定这种情况下搜索引擎的可见性。