[插件]Wordpress第一次访问的特定模板(并编写cookie)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[插件]Wordpress第一次访问的特定模板(并编写cookie)相关的知识,希望对你有一定的参考价值。

  1. <?php
  2. /*
  3. Plugin Name: first-visit-template
  4. Description: Loads a welcome.php template in theme directory if it is the first visit (the function is not lauched if you're logged as admin), and writes a cookie so it's shown only once.
  5. Author: G.Lachance
  6. Author URI: http://tinyurl.com/9wb48o
  7. Credits: Based upon the "Welcome Visitor! Reloaded" plugin from Alaeddin, which is largely based upon the original Welcome Visitor! plugin by Kaf (http://guff.szub.net/2006/04/12/welcome-visitor/)
  8. which was released under the GNU General Public License.
  9. Version: 1
  10.  
  11. */
  12.  
  13. //for testing your template, uncomment line 30.
  14.  
  15.  
  16. function welcome_visitor_reloaded() {
  17.  
  18. if(is_new_visitor()) {
  19. $tpl_file = TEMPLATEPATH . '/welcome.php';
  20. if ( file_exists($tpl_file) ) {
  21. include($tpl_file);
  22. }
  23. }
  24. }
  25.  
  26. function is_new_visitor()
  27. {
  28.  
  29. //return true; //uncomment this for testing.
  30.  
  31. global $visits;
  32.  
  33. if (!is_admin())
  34. {
  35. if (isset($_COOKIE['visits']))
  36. $visits = $_COOKIE['visits'] + 1;
  37. else
  38. $visits = 1;
  39.  
  40. $url = parse_url(get_option('home'));
  41. setcookie('visits', $visits, time()+60*60*24*365, $url['path'] . '/');
  42. }
  43. else return false;
  44.  
  45. return $visits == 1;
  46. }
  47.  
  48. ///
  49. add_action('template_redirect', 'welcome_visitor_reloaded');
  50.  
  51.  
  52. ?>

以上是关于[插件]Wordpress第一次访问的特定模板(并编写cookie)的主要内容,如果未能解决你的问题,请参考以下文章

Wordpress自定义模板,用于特定类别的Feed

Wordpress 在特定页面/帖子上禁用插件

WordPress的Jetpack插件更新

树莓派/Debian WordPress 更新并安装插件

将 PHP 页面添加到 Wordpress 模板

如何检查 Wordpress 高级自定义字段插件中的字段组?