I started this markdown file as a place to collect information about Wordpress and PHP when I started programming a Wordpress plugin for the first time. I decided to put out everything I could find in this gist. I will update it occassionally with code examples so I can track my progress in developing Wordpress plugins (or just the one I'm working on at work).
- For OSX: MAMP
- Wordpress.org
- For FTP: FileZilla
- Wordpress.org Codex: Plugins. Introduction
- Wordpress.org Codex: Plugin Resources
- Beginner’s Guide To WordPress Plugin Development
- WordPress Essentials: How To Create A WordPress Plugin
- Getting Started with WordPress Plugin Development: The Ultimate Guide
- How To Create A WordPress Plugin
WP Hooks === Conditionals where you can insert some function (actions and filters). Adam Brown has a list of hooks you can develop on top of.
- Wordpress Admin Styles
- Action Hooks: Run your own code when an event occurs
- Filter Hooks: Modify existing code within Wordpress
Four scalar types:
Two compound types:
And finally two special types:
Constants are ALL_CAPS and do not start with a '$'.
<?php
define('YEAR', 2014);
define('JOB_TITLE', 'Teacher');
?>
Arrays can be initiated two ways.
<?php
$array_example = array(); // or
$array_example = [];
// adding to array (like push() in JS)
$array_example[] = 'Amber';
?>
Associative arrays are like hashes in Ruby or dictionaries in Python.
<?php
$another_array = array(
'chris' => 'blue',
'tom' => 'green',
'jim' => 'brown'
);
echo $another_array['chris']; // returns 'blue'
?>
gettype()
- Returns type of the variable. PHP Docs- Strings are 0 based index
<?php
$variable = "Hello World."
echo $variable{0} // "H"
?>
Concatenation uses the .
symbol.
<?php
$assignment = "hello" . "world";
echo $assignment // "hello world"
?>
Casting, or typecasting, can allow you to assign a type to your variable.
<?php
$variable_one = 1;
$variable_two = (string) $variable_one;
echo $variable_two // "1" as a string
?>
isset()
- Returns true of false depending if variable is set. PHP Docs
<?php
if (/* conditional */) {
// do something
} elseif (/* another conditional */) {
// do something
} else {
// do something else
}
?>
For loop example.
<ul>
<?php
for($counter=0; $counter < 2; $counter++) {
echo "<li>" . $counter . "<li>";
}
?>
</ul>
ForEach loop example.
<?php
forEach($social_icons as $icon) {
?>
<li><a href=""><span class="icon <?php echo $icon ?>"></a></li>
<?php
}
?>
Function scope is isolated within its block. If we're going to use a global variable (or outside of the function scope), it must be added into the function scope with a global statement.
<?php
$current_user = 'Jeremy';
function hello() {
global $current_user; // Without this statement, $current_user can not be used within the function scope
// do something
}
?>
Arguments can be passed by reference. This means if you want to change the argument variable outside the function scope, you need to add an &
in front of the argument. By default, arguments are passed as values.
<?php
function add_some_extra(&$string)
{
$string .= 'and something extra.';
}
$str = 'This is a string, ';
add_some_extra($str);
echo $str; // outputs 'This is a string, and something extra.'
?>
Default arguments can also be passed.
<?php
function hello($name = 'friend') {
echo 'Hi, $name';
}
hello(); // by default, 'Hi, friend'
?>
Returns are not implicit. If no return is passed, NULL will be passed back.
Variable Functions - calling variables given a string variable value. Very valuable for callbacks.
<?php
function add_up($1, $b) {
return $a + $b;
}
$func = 'add_up';
$num = $func(5, 10);
echo $num; // 15
?>
Closures are anonymous functions, also useful for callbacks. The use
keyword is important here.
<?php
$name = 'friend';
$greet = function() use ($name) {
echo 'Hello $name.';
}
$greet(); // 'Hello friend.'
?>
Accessing its properties (the class public variable) within the class, you need to use an object operator ->
. $this->//Property of class
Visibility:
public
private
protected
- Members declared protected can be accessed only within the class itself and by inherited and parent classes.
Constructors:
<?php
class Person {
public $name;
function __construct($args) {
$this->name = $args['name'];
}
public function showName() {
echo $this->name;
}
}
$carrot_top = new Person(array(
'name' => 'Carrot Top'
));
$carrot_top->showName(); // 'Carrot Top'
?>
It turns out, you use the -> for dynamic methods while you use :: for static method. Usually explicit in the function name. public static function
.
strlen
String Lengthsubstr
Substringstrpos
String position. Returnsbool(false)
if string passed is not found.
echo
- Output one or more stringsprint_r
- Prints arrayprintf
- Output a formatted stringfprintf
- Write a formatted string to a streamsprintf
- Return a formatted stringprint_r
- Variable handling function - Prints human-readable information about a variablevar_dump
- Variable handling function - Returns information about a variable. PHP Docs
array_keys
Returns all the keys or a subset of keys of an arrayarray_walk
Callback applied to elements in a given array
With APIs, there is no try/catch for cUrl since it's based on the C Library. StackOverflow
<?php
$options[CURLOPT_URL] = 'https://[domain]/test.htm';
$options[CURLOPT_PORT] = 443;
$options[CURLOPT_FRESH_CONNECT] = true;
$options[CURLOPT_FOLLOWLOCATION] = false;
$options[CURLOPT_FAILONERROR] = true;
$options[CURLOPT_RETURNTRANSFER] = true; // curl_exec will not return true if you use this, it will instead return the request body
$options[CURLOPT_TIMEOUT] = 10;
// Preset $response var to false and output
$fb = "";
$response = false;// don't quote booleans
echo '<p class="response1">'.$response.'</p>';
$curl = curl_init();
curl_setopt_array($curl, $options);
// If curl request returns a value, I set it to the var here.
// If the file isn't found (server offline), the try/catch fails and var should stay as false.
$fb = curl_exec($curl);
curl_close($curl);
if($fb !== false) {
echo '<p class="response2">'.$fb.'</p>';
$response = $fb;
}
// If cURL was successful, $response should now be true, otherwise it will have stayed false.
echo '<p class="response3">'.$response.'</p>';