php – 如何创建一个有效的内容过滤器的某些帖子?
|
我把这个帖子标记为WordPress,但是我并不完全确定这是WordPress的特定的,所以我发布在StackOverflow而不是WPSE.该解决方案不一定是WordPress特定的,只是
PHP.
情景 我们的网站是围绕我们的个人资料.正如你所说,它们是网站的面包和黄油. 我希望实现的是,在提及另一个物种或词汇表条目的每个物种资料中,我可以用链接替换这些词,例如你会看到here.理想情况下,我也会这样发生在新闻,文章和博客帖子. 我们有近1400种物种资料和1700个词汇表.我们的物种资料通常很冗长,最后我们的物种资料总计超过170万字的信息. 我正在尝试什么 另外,在我的WordPress主题的functions.php中,我有以下内容: # ==============================================================================================
# [Filter]
#
# Every hour,using WP_Cron,`my_updated_posts` is checked. If there are new Post IDs in there,# it will run a filter on all of the post's content. The filter will search for Glossary terms
# and scientific species names. If found,it will replace those names with links including a
# pop-up.
include "filter.php";
# ==============================================================================================
# When saving a post (new or edited),check to make sure it isn't a revision then add its ID
# to `my_updated_posts`.
add_action( 'save_post','my_set_content_filter' );
function my_set_content_filter( $post_id ) {
if ( !wp_is_post_revision( $post_id ) ) {
$post_type = get_post_type( $post_id );
if ( $post_type == "species" || ( $post_type == "post" && in_category( "articles",$post_id ) ) || ( $post_type == "post" && in_category( "blogs",$post_id ) ) ) {
//get the previous value
$ids = get_option( 'my_updated_posts' );
//add new value if necessary
if( !in_array( $post_id,$ids ) ) {
$ids[] = $post_id;
update_option( 'my_updated_posts',$ids );
}
}
}
}
# ==============================================================================================
# Add the filter to WP_Cron.
add_action( 'my_filter_posts_content','my_filter_content' );
if( !wp_next_scheduled( 'my_filter_posts_content' ) ) {
wp_schedule_event( time(),'hourly','my_filter_posts_content' );
}
# ==============================================================================================
# Run the filter.
function my_filter_content() {
//check to see if posts need to be parsed
if ( !get_option( 'my_updated_posts' ) )
return false;
//parse posts
$ids = get_option( 'my_updated_posts' );
update_option( 'error_check',$ids );
foreach( $ids as $v ) {
if ( get_post_status( $v ) == 'publish' )
run_filter( $v );
update_option( 'error_check',"filter has run at least once" );
}
//make sure no values have been added while loop was running
$id_recheck = get_option( 'my_updated_posts' );
my_close_out_filter( $ids,$id_recheck );
//once all options,including any added during the running of what could be a long cronjob are done,remove the value and close out
delete_option( 'my_updated_posts' );
update_option( 'error_check','working m8' );
return true;
}
# ==============================================================================================
# A "difference" function to make sure no new posts have been added to `my_updated_posts` whilst
# the potentially time-consuming filter was running.
function my_close_out_filter( $beginning_array,$end_array ) {
$diff = array_diff( $beginning_array,$end_array );
if( !empty ( $diff ) ) {
foreach( $diff as $v ) {
run_filter( $v );
}
}
my_close_out_filter( $end_array,get_option( 'my_updated_posts' ) );
}
这个工作方式(希望)由代码的意见描述,是每个WordPress每小时运行一个cron工作(这就像一个假cron – 在用户点击上工作,但并不重要,因为时机不是重要的),它运行上面找到的过滤器. 每小时运行一次的理由是,如果我们在每个职位被保存时试图运行它,这将对作者造成不利影响.一旦我们收到客人的作者,这显然不是一个可以接受的方式. 问题… 不幸的是,诊断问题是非常困难的(我可以看到),感谢它在后台运行,只有在小时的基础上运行.我一直在尝试使用WordPress的update_option函数(它基本上写了一个简单的数据库值)进行错误检查,但我没有太多的运气 – 说实话,我很困惑的是,问题在哪里. 我们最终将网站存在,无需此过滤器正常工作.有时它似乎工作,有时它不.因此,我们现在有很多物种资料没有被正确过滤. 我想要什么 是Cron工作的答案吗?我可以设置每天运行的.php文件,这不会是一个问题.如何确定哪些职位需要过滤?它在运行时对服务器有什么影响? 或者,是WordPress管理页面的答案吗?如果我知道如何做到这一点,那么使用AJAX(允许我选择帖子来运行过滤器)的页面的一些东西将是完美的.有一个名为AJAX Regenerate Thumbnails的插件,这样做可能是最有效的? 注意事项 >受影响/读/写数据库/信息的大小 这是一个相当复杂的问题,我不可避免地(在过程中我被同事分心了大约18次),省略了一些细节.请随时查询我的进一步信息. 提前致谢,
创建配置文件时执行此操作.
尝试扭转整个过程.而不是检查内容的单词,检查内容的单词的单词. >打破内容发表的内容(空格) 即使你搬出了十万个单词,你也可以轻松保持在1秒以内.我已经做了这个,没有缓存单词列表,以前的贝叶斯过滤器. 有了较小的列表,即使是贪婪而且收集不符合“小丑”的单词将会捕捉到“小丑泥ach”,所产生的较小的列表应该只有几到几十个字与链接.这将不需要时间去做一个文字的查找和替换. 以上并没有真正解决您对旧版配置文件的关注.你没有说出确实有多少,只是有很多的文本,它是在1400到3100(两个项目)放在一起.如果你有这个信息,你可以根据受欢迎程度来做这些较旧的内容.或输入的日期,最新的.不管最好的方法是编写一个脚本来暂停PHP的时间限制,只需批量运行所有帖子的加载/进程/保存.如果每个人需要大约1秒钟(可能要少得多,但最糟糕的情况),你说的是3100秒,这是一个小于一小时. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
