飞龙博客

feilong.org 前端开发教程

define global varations wordpress theme php

飞龙更新于 2010-11-04 03:58 加入书签 CTRL+D 有 24 个朋友来过

When custom wordpress theme,feilong wants to repeatly use some values, such as the category id on category.php,the first category id or all category ids on single.php.If you do not want to use custom post type, instead,you should often use a post's top category id or slug to determine "the type of a post".

To avoid duplicate definition of these basic values,feilong would like adopt a function to define these common or public variables in functions.php first. But how to customize functions to define global variables in a wordpress theme? Feilong test here: http://feilong.org/wordpress-define-global-var-en edited on 2010-11-02. I hope it useful for you,my friends.

One, usage of '&'

<?php
$a = '5';
$b = &$a;
$a = '8'; //reset $a
echo $b; //output 8
unset($a); //unset $a
echo $b; //output 8
$a = '9';//difine $a twice
echo $b; //output 8
?>

Two, usage of valuable

<?php
$name = 'feilong.org'; //no &
$my = $name; //no &
$addr = &$name; //&
$name = 'feilong'; //reset $name
echo $name; //feilong
echo $my; //feilong.org
echo $addr; //feilong
$addr = 'jiaju99'; //$addr changes along with $name for use of "&"
echo $name; //jiaju99
echo $addr; //jiaju99
?>

Three, valuable and function

<?php
$zhe = 'feilong';
function zhege() {
echo $zhe; //no display of $zhe
}
zhege();//nono
echo $zhe; //outof function,$zhe output "feilong"
 
$na = 'org';
function nage($na) {// $na is valid
echo $na; //org
}
nage($na);//use $na
?>

Four, dominate valuable

<?php
$var = 'mysite';
$$var = 'aneave.com'; //the same as $$var = $mysite
echo $var; //mysite
echo $$var; //aneave.com
echo $mysite; //aneave.com
?>

Five, globals
including Super globals,which are built-in variables that are always available in all scopes

1, Super globals
$GLOBALS — References all variables available in global scope
$_SERVER — Server and execution environment information
$_GET — HTTP GET variables
$_POST — HTTP POST variables
$_FILES — HTTP File Upload variables
$_REQUEST — HTTP Request variables
$_SESSION — Session variables
$_ENV — Environment variables
$_COOKIE — HTTP Cookies

2, Other globals
$php_errormsg — The previous error message
$HTTP_RAW_POST_DATA — Raw POST data
$http_response_header — HTTP response headers
$argc — The number of arguments passed to script
$argv — Array of arguments passed to script

Six, use gloabl valuables in wordpress themes

1, In fuctions.php, some testing snippets

<?php
 
//1. define common function in functions.php use '&'
function goodsid(&$a) {
$a = '5';
}
//define getgoodsid().no global appears
function getgoodsid($b) {
return $b;
}
 
//2. or define setgoodsid() directly? is it safe?
function setgoodsid() {
$goodsid = '5';//inside funtion
return $goodsid;
}
 
//3. or use global? someone said it's not good
$mygoodsid= '5'; //out of function
function get_global_goodsid() {
global $mygoodsid;
return $mygoodsid;
}
 
//4.this time feilong use either global or '&amp;',BUT static var!
function mylove() {
static $love = 'yinlinghua.com'; //inside function, static declare $love is a static var
return $love;
}
 
//5. use GLOBALS
$girlname = 'yilulu';
function girl() {
return $GLOBALS["girlname"]; //$GLOBALES is a global array
}
 
?>

2, Then in other template file for example sidebar.php of your wordpress theme,retrieve the global values

<?php
 
//1. let $c has the value defined in goods()
goodsid($c);
//then echo $c
echo $c; //5
//or,then echo getgoodsid()
echo getgoodsid($c); //also 5
 
//2. try setgoodsid(),perhaps it will not safe?
echo setgoodsid();//output 5,seems ok?
 
//3. use global, seems ok?
echo get_global_goodsid();
global $mygoodsid;echo $mygoodsid;//also ok
 
//4. echo static var function
echo mylove();//seems best?
 
//5. $GLOBALS another way
echo girl();
echo $GLOBALS["girlname"]; //or,echo the value of key 'girlname'
global $girlname;echo $girlname;//or,global and echo $girlname
 
?>

Ok,blog() and getblog() just the same cat and dog!Welcome for a better fuction to define common or global varations in php!

维护小站,感谢赞赏。
联系飞龙,请转淘宝

飞龙初发:
2010-11-04 03:58
本文更新网址:
https://feilong.org/wordpress-define-global-var-en

所在目录: wordpress

新作:

旧文:

  • html css js
  • angular react vue
  • flutter python