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条)

报告相同问题?