duanjucong3124 2011-10-28 17:06 采纳率: 100%
浏览 45
已采纳

登录后在第一页加载时向wordpress网站添加代码

I'm writing a wordpress plugin where I need to inject a beacon (HTML image tag) into the page on the first page load after the user logs in - i.e. the page they are redirected to. Subsequent page views of the same or other pages should not include this beacon code.

I've tried several ways to do this.

I've added a function to the wp_login hook. From this function I've tried adding another function to the wp_footer hook to output the content.

// in my class constructor
add_action('wp_login', array($this, 'login'));

public function login() {
    add_action('wp_footer', array($this, 'add_beacon'));
}

This didn't work so I tried setting both hooks up front and setting a global variable to indicate first load. That wasn't picked up by my second function and neither was a constant.

// in my class constructor
add_action('wp_login', array($this, 'login'));
add_action('wp_footer', array($this, 'add_beacon'));

public function login() {
    define('FIRST_RUN', true);
}

public function add_beacon() {
    if (defined('FIRST_RUN')) {
        // output beacon
    }
}

Both hook functions fire correctly but whenever I try to make the output of content in the second dependent on the first hook function firing I get no output.

Any ideas how I can do this? Perhaps there's some way I can check without using the wp_login hook at all.

  • 写回答

1条回答 默认 最新

  • dongpaozhi5734 2011-10-28 21:53
    关注

    Try using a per session cookie.

    The code would be something like:

    function my_init() { 
      if ( is_user_logged_in() ) {  
        if (!isset($_COOKIE['beacon'])) {  
          //Track user code / add wp_footer action
          setcookie ('beacon' 'tracked'); 
        }
      }
    }
    add_action('init','my_init');
    

    (Code not tested.. hopefully you get the idea).

    If you're not happy with the cookie, you could store last page loaded by user in the usermeta table... however, you'd have to make a call as to how long since last page load is considered a new session.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况