Tuesday, 10 March 2015

How do I get wordpress global variable and functions in custom file [SOLVED]

How do I get wordpress global variable and functions in custom file


Problem:
I have create a file i.e mytestfile.php in root folder of wordpress blog.
Full path of mytestfile.php is http://www.example.com/blog/mytestfile.php.
Now, I want to access all global variables, functions like $wp_query, query_posts.



Solution:
As you have created mytestfile.php in blog root folder.
Just add the following line in top. This will load wordpress blog and you can access global variables.
require( 'wp-load.php' );
global $wp_query;
Now, you can access $wp_query, query_posts.



Difference between in_array and array_key_exists in php with example

Difference between in_array and array_key_exists in php with example

in_array: Checks if a value exists in an array OR not.
bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] );

Parameters
needle: The searched value.
haystack: The array.
strict: If the third parameter strict is set to TRUE then the in_array() function will also check the types of the needle in the haystack.

Return: Returns TRUE if needle is found in the array, FALSE otherwise.

Example:
$array = array("Web", "Technology", "Experts", "Notes");
if (in_array("Technology", $array)) {
    echo "Record Found in value";
}



array_key_exists: Checks if the given key/index exists in the array.
bool array_key_exists ( mixed $key , array $array );
Parameters key: Value to check.
array: An array with keys to check.

Return: Returns TRUE on success or FALSE on failure.

Example:
$searchArray = array('Web' => 1, 'Technology' => 4);
if (array_key_exists('Web', $searchArray)) {
    echo "Record Found in key";
}