WordPress - Help Reading php Code
WordPress is written in
- php
- JavaScript
- css - StyleSheets
- sql - for database access
- html
- jQuery (a JavaScript library)
You will need to know some of each of these to reverse engineer much of anything.
This page deals with php.
I have been programming for a number of years and, basically, I can read almost anything.
To write "new" code, I normally just look around, copy something that looks close,
and make a few mods.
However, I have found WordPress particularly challenging.
Normal php is pretty easy (it is almost c-like), but ...
well, that is why this page exits.
Basics
| Arrays
| Functions
| WordPress specific conventions
| if statements
| Objects
Basics
echo 'any string';
echo 'a string'.$some_variable.'another string'; // a dot concatenates 2 strings
$var1 .= 'some string'; // concatenate a string to the existing variable
|
echo writes a string to the "current output" which is the newly created web page.
Unfortunately, the "current" web page is frequently replaced with another page
before you are able to read your debug messages.
Careful -
single and double quotes give different results.
echo 'any string\n'; // this prints the \n without translation
echo "any string\n"; // this converts the \n to a newline character
echo 'a string $some_variable another string'; // this prints exactly what is typed
echo "a string $some_variable another string"; // this replaces $some_variable with the variable's value
|
String concatenation is always a problem.
- Most languages use a plus sign
| str1 + str2
| - VisualBasic uses an ampersand
| str1 & str2
| - php uses a period
| $str1 . $str2
| | | |
As far as I can tell, spaces are never required before or after the period.
All php code is enclosed between tags, any text not between those tags is copied
directly to the web page
If you want to stop a function, use return, not exit.
return; // this works
exit; // this causes weird problems that are hard to debug
|
- Apparently, all variables start with a dollar sign ($varname)
- There is no variable type casting, everything is a variant
- classes are supported
Arrays
Arrays have
their own rules
and
a number of functions.
$some_array['any_string'] = 0; // context addressable array (associative array)
// Create an array with 3 elements
$defaults = array('before' => '', 'after' => '', 'echo' => true);
// Create an empty array
$defaults = array();
// Operate on each item in the array
foreach ( (array) $keys as $key ) {
//your code here
}
// Alternate colon syntax
foreach($images as $image):
echo wp_get_attachment_image($image->ID, 'medium');
endforeach;
// When working with an array of arrays
foreach ( (array) $keys as $key => $value ) {
//your code here
//$key contains the string values of the indices
}
foreach ( (array) array_keys($data) as $field ) {
//$field contains the string values of the indices
}
|
To remove a single element from an array, use the following
unset($data['id']); // not in the array reference
|
Functions
- All functions and classes in PHP have the global scope
- Function names are not case-sensitive
function unique_name($parm1, &$parm2, ..., $parmx = 'default'){
// your code here
$parm1 = 'abc'; // this will not be seen in the calling code
$parm2 = 'xyz'; // this will be seen in the calling code
return $some_value; // another way to return a value (or an array, or an object)
}
|
By default, all parameters are passed by value (meaning that a copy is passed).
If you want to be able to edit the variable, then it must be passed by reference
(meaning that you want to pass a pointer).
In php, an ampersand (&) indicates that the reference to (address of) the variable
is being passed.
The ampersand (&) can be used in either the calling statement, or in the function definition (preferred).
For instance, if you want to be able to edit an array's contents, then it must be passed by reference.
$book_info = array(); // create an empty array
$this->set_array( $book_info, $form_data, 'title'); // passed by value, changes are lost
$this->set_array(&$book_info, $form_data, 'title'); // passed by reference, changes are available in calling routine
function set_array($an_array, $source, $field){
$an_array[$field] = !isset($source['lib_events_'.$field])? NULL : $source['lib_events_'.$field];
}
|
Additional info:
Function arguments
Returning values
WordPress specific conventions
There are a few very confusing commands used everywhere.
After a while, I discovered that they are a part of the WordPress library.
wp-includes/l10n.php the WordPress Translation API
|
These functions translate the text into the appropriate language.
// this does a simple translate and echo
// The following first translates - using __(xx) - the displayed part of the text and
// then adds the link info - using printf(xx) - that must not be translated
// The "%s" is replaced with the link
to post a comment.', 'atahualpa'),
'')?>
|
These are how the functions are implemented
function __( $text, $domain = 'default' ) {
return translate( $text, $domain );
}
function _e( $text, $domain = 'default' ) {
echo translate( $text, $domain );
}
|
There are a lot more, but these are the ones I saw everywhere.
* if * statements
Most of the php code I've seen uses
standard c-syntax for if statements.
if ( false == $last_bar ) // simple if..else
return $string;
else
return substr( $string, 0, $last_bar );
|
While that works, I suggest ALWAYS using the block format.
if ( false == $last_bar ){ // the curly braces define blocks
return $string;
} else {
return substr( $string, 0, $last_bar );
}
|
Either else if () or elseif () can be used.
Some files use an alternate colon syntax (instead of curly braces)
if ('open' == $post->comment_status) :
//your code here
else :
//your code here
endif;
|
This immediate or in-line syntax is also used a lot -
a value is returned based on the conditional statement (inside the parentheses).
The first element is returned (or executed) if the condition is true, otherwise the second element is returned.
(a>5) ? 'good' : 'bad'; // return one of the strings
(a>5) ? function_1() : function_2(); // call one of the functions
|
Objects
In most of the code I've seen, all the function names are global.
However, objects allow you to encapsulate those and leave only the
instance name as a global.
class xyz_unique {
function xyz_unique() { // class constructor
// code goes here
}
function any_name() { // member function (method), name is only class unique
// code goes here
}
} // end of class definition
$xyz_unique = new xyz_unique(); // creates an instance and executes the constructor
class WP_Scripts extends WP_Dependencies { // how to inherit from a base class
}
|
In most object oriented languages, the member methods can call each other without any special syntax.
However, php requires $this->
any_name(); // fails, function not found
$this->any_name(); // works
|
When calling a member function from outside the instance, use
$xyz_unique->any_name(); // instance_name->member_name();
|
Author:
Robert Clemenzi