dsj8000 2016-08-17 14:32
浏览 55
已采纳

Wordpress - 使变量可用于所有主题文件(没有Globals)

I'm developing a Wordpress theme and I'm trying to store some of my variables in a function to re-use it among my theme files, without writing the same variables hundreds of time and without make them global. I've read that's a bad practice.

For now I'm using add_action but without results. Having in my functions.php

add_action( 'run_pms_variables', 'pms_variables_function' );
function pms_variables_function ($uid ) {
    $the_group_uid = isset($_COOKIE["v_group_id"]) && !empty($_COOKIE["v_group_id"]) ? trim(strip_tags($_COOKIE["v_group_id"])) : "";       
    $session_detail = vpb_get_the_user_detail($uid);
    $session_username = strlen($session_detail[0]) > 40 ? ucfirst(substr($session_detail[0],0,40)) : ucfirst($session_detail[0]);
    $session_uid = $session_detail[1];
    $session_email = $session_detail[2];
    $session_photo = $session_detail[3];
    $session_country = $session_detail[4];
    //$session_usernames = explode(' ', $session_detail[0]);
    $session_firstname = get_user_meta($uid,'first_name', true );
    $session_lastname = get_user_meta($uid,'last_name', true );
}

and using eventually in my files like:

do_action('run_pms_variables', $uid );

I mean, can you address me to the right method? Thanks.

  • 写回答

3条回答 默认 最新

  • duanjiong5023 2016-08-17 14:41
    关注

    Write a class, declare a global object of that class, save all your variables as attribute/property of that class.

    Now from anywhere just get the global object.

    You have all variables stored inside that.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥20 为什么我写出来的绘图程序是这样的,有没有lao哥改一下
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号