I use global variable $table_prefix to differ whether I work on Word Press or WPMU. I need this global variable for my plugin. But is there any better way to check whether your pluggin is working on Word Press or WPMU?
In WPMU a global variable named wpmu_version should be set.
for my checks I use a function I found
// from http://frumph.net/wordpress/wordpress-plugin-theme-check-for-multisitewpmu/
// check for multisite. Returns boolean
function check_this_is_multsite() {
global $wpmu_version;
if (function_exists('is_multisite')){
if (is_multisite()) {
return true;
}
if (!empty($wpmu_version)){
return true;
}
}
return false;
}
use it like this
if(check_this_is_multsite()){
// is on wpmu
} else {
// is on single
}
You could define a constant:
e.g.
define('ENVIRONMENT', 'WP');
define('ENVIRONMENT', 'WPMU');
Related
This is what I am using to check if an post value is empty or not in codeigniter.
if($this->input->post('inputEmail')) { }
Just wanted to know what is the best method to check it, the above mentioned method or
$temp = $this->input->post('inputEmail');
if(!empty($temp)) { }
Check with isset in php to check whether variable is set or not.
Try this:
if(isset($tmp))
{
//it is set
}
else
{
//not set
}
I would like to bypass core and plugin functions to customize them.
I didn't succeed to do it from template.
I try to add into my tpl_functions.php something like:
if (!function_exists('html_buildlist')) {
function html_buildlist($data,$class,$func,$lifunc='html_li_default',$forcewrapper=false){
// etc.
}
}
My first idea is to check if the page has been visited and then customize the indexmenu plugin.
For example, i make this function to check if a page has been visited:
function wt__pagevisited($id){
if ($id == null) {
global $INFO;
$id = $INFO['id'];
}
// get cookie session info
$crumbs = isset($_SESSION[DOKU_COOKIE]['bc']) ? $_SESSION[DOKU_COOKIE]['bc'] : array();
// check ID into breadcrumb
if( array_key_exists($id,$crumbs) ) {
return true;
}
return false;
}
Any help will be appreciated.
Thank you in advance.
Jean-baptiste
What you're asking has nothing to do with DokuWiki. You want to replace PHP functions. That's not possible without the help of certain PHP extensions. See Is it possible to replace a function in php (such as mail) and make it do something else? for more info.
I've got my login and session validity functions all set up and running.
What I would like to do is include this file at the beginning of every page and based on the output of this file it would either present the desired information or, if the user is not logged in simply show the login form (which is an include).
How would I go about doing this? I wouldn't mind using an IF statement to test the output of the include but I've no idea how to go about getting this input.
Currently the login/session functions return true or false based on what happens.
Thanks.
EDIT: This is some of the code used in my login/session check but I would like my main file to basically know if the included file (the code below) has returned true of false.
if ($req_method == "POST"){
$uName = mysql_real_escape_string($_POST['uName']);
$pWD = mysql_real_escape_string($_POST['pWD']);
if (login($uName, $pWD, $db) == true){
echo "true"; //Login Sucessful
return true;
} else {
echo "false";
return false;
}
} else {
if (session_check($db) == true){
return true;
} else {
return false;
}
}
You could mean
if (include 'session_check.php') { echo "yeah it included ok"; }
or
logincheck.php'
if (some condition) $session_check=true;
else $session_check=false;
someotherpage.php
include 'session_check.php';
if ($session_check) { echo "yes it's true"; }
OR you could be expecting logincheck.php to run and echo "true" in which case you're doing it wrong.
EDIT:
Yes it was the latter. You can't return something from an included file, it's procedure not a function. Do this instead and see above
if (session_check($db) == true){
$session_check=true;
} else {
$session_check=false;
}
Actually..
$session_check=session_check($db);
is enough
Depending on where you want to check this, you may need to declare global $session_check; or you could set a constant instead.
you could have an included file which sets a variable:
<?php
$allOk = true;
and check for it in you main file:
<?php
include "included.php";
if ($allOk) {
echo "go on";
} else {
echo "There's an issue";
}
Your question seems to display some confusion about how php includes work, so I'm going to explain them a little and I think that'll solve your problem.
When you include something in PHP, it is exactly like running the code on that page without an include, just like if you copied and pasted. So you can do this:
includeme.php
$hello = 'world';
main.php
include 'includeme.php';
print $hello;
and that will print 'world'.
Unlike other languages, there is also no restriction about where an include file is placed in PHP. So you can do this too:
if ($whatever = true) {
include 'includeme.php';
}
Now both of these are considered 'bad code'. The first because you are using the global scope to pass information around and the second because you are running globally scoped stuff in an include on purpose.
For 'good' code, all included files should be classes and you should create a new instance of that class and do stuff, but that is a different discussion.
I created a function to allow me to debug PHP scripts so long as a variable ($debug) is set to 1:
function debug($msg) {
if ($debug == 1) {
echo $msg;
} else {
return false;
}
}
That way, at the top of my script (before the functions.php file is called), I write:
$debug = 1;
to enable debugging, then:
debug("Function executed: " . $data);
at certain points so that I know string values/whatever at that point, with the desired response being the message displayed upon the screen.
However, regardless of what the value of the $debug string is, I never see any echo'd statements.
How can this be achieved?
Debug is not available to your function because it is out of scope. You either:
Need to pass it as a parameter
Use the global keyword to include it in your function (discouraged)
.
function debug($msg, $debug){
if($debug==1){
echo $msg;
} else {
return false;
}
}
or
function debug($msg){
global debug;
if($debug==1){
echo $msg;
} else {
return false;
}
}
It's difficult to say because you provided too few data.
The reason can be that your $debug variable is not known inside a function. Because using globals is not adviced, consider using constants define("DEBUG",1);.
EDIT
I presented within another question how I use a class for doing the same thing as class names are also globally accessed.
The global variable is not accessible to functions until you make it so. Eg:
function debug($msg( {
global $debug;
etc...
PS: Please, don't do this. Find a better way. Really.
$debug is your global variable, so it is not accessible in your function
There is also the possibility to declare a const, (and then just insure the namespace is correct), like this:
const debug = true;
function newPrint($msg) {
if (\debug === true) {
echo $msg;
} else {
return false;
}
}
\newPrint("heey");//will print "heey"
so just dont use a variable, but use a const.
I have a class that loads certain parts of my page. What I want to do is this
index.php
$obj->load('admin');
In my class in the constructor I have a check to see if the URI contains the segment /admin/ and if it does then it auto loads the admin page:
$this->load('admin');
now both work fine, I just realized that if I have both entries in my project then it will load the admin initialization twice.
Is there a way for my to check if the load() method has already been called and if the parameter is == to admin?
One way is to use static cache:
function load($what) {
static $loaded = array();
if (!isset($loaded[$what])) {
// Load..
// Mark as loaded
$loaded[$what] = true;
}
}
class Foo {
private $loaded = false;
function someFunc() {
$this->loaded = true;
}
function loaded() {
return $this->loaded;
}
}
May be Keep a object member variable variable and keep a flag when the load admin is called for first time. Check the flag for during the second call.
Easiest - create additional variable called say '$invokedAlready'. Set it first to 'false'. In your method place this:
if ( $invokedAlready )
{
// do sth
}
else
{
// first invocaion
// do sth else
$invokedFirst = true;
}