Skip to content

Instantly share code, notes, and snippets.

@jermspeaks
Last active July 20, 2022 23:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jermspeaks/a0d3c4983c363962011c to your computer and use it in GitHub Desktop.
Save jermspeaks/a0d3c4983c363962011c to your computer and use it in GitHub Desktop.
Resources collected for understanding Wordpress

Wordpress & PHP Resources

Background

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).

Wordpress 101

Wordpress Environment

Learning Wordpress

Wordpress Plugins

Wordpress Plugin Resources

Connecting to APIs

Hooks

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 Template Tags

Loops (i.e. WP_Query)

Wordpress Coding Standards

Wordpress Debugging

Dev Project Management

PHP Programming

PHP Types

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"
?>

PHP Operators

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
?>

PHP Conditional syntax:

  • 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
    }
?>

PHP Looping

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
    }
?>

PHP function

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.'
?>

Objects

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'
?>

Classes

It turns out, you use the -> for dynamic methods while you use :: for static method. Usually explicit in the function name. public static function.

Built-in Functions

Strings

  • strlen String Length
  • substr Substring
  • strpos String position. Returns bool(false) if string passed is not found.

Printing in PHP

  • echo - Output one or more strings
  • print_r - Prints array
  • printf - Output a formatted string
  • fprintf - Write a formatted string to a stream
  • sprintf - Return a formatted string
  • print_r - Variable handling function - Prints human-readable information about a variable
  • var_dump - Variable handling function - Returns information about a variable. PHP Docs

Arrays

  • array_keys Returns all the keys or a subset of keys of an array
  • array_walk Callback applied to elements in a given array

Error Handling

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>';

TinyMCE Plugin Development

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment