2016-04-28 83 views
0

我使用的是Wordpress 4.5。在较早的时候,我使用本地服务器来存储图像(包括精选图像,例如:wp-content/uploads/2016/03/aaabbbccc.jpg)。现在我想将它移动到AWS S3 Bucket和Cloudfront,并且我不想在Wordpress的数据库中进行更改,并决定在每次从数据库检索时编写插件以加入我的云端域(替换当前域)到wp-content链接如:https://xxyyzzqqaa.cloudfront.net/wp-content/uploads/2016/03/aaabbbccc.jpgWordpress - 如何在加载之前修改精选图片的URI

我用一个动作钩在我的插件:

add_action('the_post', 'change_featured_image'); 

现在我不知道下一步该怎么做?请给我一些建议,在此先感谢!

回答

0

我发现了我的问题的解决方案,这很容易,但我是WP的新手,所以我看不到这一点。

在你的插件,或者你想抓住这个事件,当WP负载,添加此过滤器,其中:
的add_filter( 'post_thumbnail_html', 'change_featured_image');
有了:

  • post_thumbnail_html是集结在WP的过滤器。
  • change_featured_image是post_thumbnail_html返回结果之前的函数,用户自定义并运行。

我在这个函数中有些困惑,我不知道变量是否能够获得价值。最后,我在WP API文档中发现它,每个变量都使用内置过滤器,您也可以使用它。

这里的示例代码:

function change_featured_image($attr) 
{ 

    global $wpdb; 

    $opt = get_option('s3dcs_status');//My value in `wp_options` 
    if(empty($opt)) echo $attr; 
    else{ 
     /// Preparing variables 
     $pattern = '~(http.*\.)(jpe?g|png|[tg]iff?|svg)~i'; 
      $m = preg_match_all($pattern,$attro,$matches); 
     $il = $matches[0][0]; 
     $tail = explode("wp-content", $il)[1]; 
     $s3dcs_remote_link = get_option('s3dcs_cfs3in') . "/wp-content" . $tail; 
       echo str_replace($il, $s3dcs_remote_link, $attr) ; 
    } 
} 

这就是全部。如果你有问题,请在这里发帖给大家可以知道。

相关问题