SlideShare a Scribd company logo
for
Speaking the language of WordPress
2018
Alena Holligan
PHP Teacher at Treehouse
PHPDX User Group Leader
Web Developer for 20 years
https://github.com/sketchings/
wp-random-quote-playground
PART 1: Basics of PHP
Variables
Conditionals
Functions
PART 2: Build A Plugin
Minimum Viable Plugin (MVP)
Interacting with WordPress
Adding a Shortcode
Basics of PHP
Cany Arrays
$mm1 = [
'orange','orange','orange','red','yellow','yellow',
'yellow','green', 'green', 'green', 'blue', 'blue'
];
var_dump(array_count_values($mm1));
$mm2 = [
'yellow', 'green', 'green', 'green', 'blue', 'blue',
'orange', 'orange', 'orange', 'red', 'red', 'blue'
];
$bag = [$mm1, $mm2];
Variables: scalar (boolean, integer, float,
string)
$workday = false; // boolean
$hour = 6; // integer
$price = 15.25; // float
$quote = "Your future is created by what you do today,"
. " not tomorrow."; // string
$quote .= " — Robert Kiyosaki"; // concatenation
define( 'FORCE_SSL_ADMIN', true); // doesn’t change
PHP Basics
<?php
/*
Multi Line Comment
Used for documentation
*/
$total = (6*3) + (12/4) - 2.5;// math 18.5
echo "This will display $total value on page.";// 18.5
echo 'This will display $total name on page.';// $total
var_dump($total, FORCE_SSL_ADMIN);
// float(18.5) bool(true)
// leave off the closing tag if ending a file
?>
Variables: array
$motivation = array(
"You don’t have to be great to start...",
"Set a goal that makes you want to jump...",
"You’ll never change your life until...",
"This is not just another day, this is...",
"Leave your ego at the door every morning...",
);
$motivation[] = $quote;
echo $motivation[0];
Functions
Functions: Build-In
string substr — Return part of a string
int strlen — Get string length
string trim — Strip whitespace (or other characters) from the beginning
and end of a string (also rtrim & ltrim)
int strpos — Find position of first occurrence of substring in string
string strtolower — Make a string lowercase (also strtoupper)
mixed str_replace — Replace all occurrences of the search string with
the replacement string
Array Functions
array array_merge — Merge one or more arrays
bool in_array — Checks if a value exists in an array
array array_keys — Return all the keys (or subset) of an array
Functions: User Defined
function sketchings_quotes ( $category = null )
{
$motivation = array(...);
$encouragement = array(...);
if ( in_array(strtolower(trim($category)),
array('motivation', 'motivated', 'motivate')) ) {
return $motivation;
}
if ( in_array(strtolower(trim($category)),
array('encouragement','encourage','encouraging')) ) {
return $encouragement;
}
return array_merge($motivation, $encouragement);
}
Conditionals
function sketchings_random_quote ( $category = null )
{
$hour = date('H');
if (!empty($category)) {
$quotes = sketchings_quotes($category);
} elseif ($hour > 4 && $hour < 17) {
$quotes = sketchings_quotes('motivation');
} else {
$quotes = sketchings_quotes('encouragement');
}
$index = array_rand($quotes);
return $quotes[$index];
}
Loops
for − loops through a block of code a specified number of times.
while − loops through a block of code if and as long as a specified condition is
true.
do...while − loops through a block of code once, and then repeats the loop as
long as a special condition is true.
foreach − loops through a block of code for each element in an array.
Loops: foreach
function sketchings_tooltip($content) {
$search = array(' motivation',' encouragement', '...');
foreach ($search as $word) {
$content = str_ireplace(
$word,
' <span class="tooltip">' . $word
. '<span class="tooltiptext">'
. sketchings_random_quote($word)
. '</span></span>',
$content
);
}
return $content;
}
Build a Plugin
Minimum Viable Plugin (MVP)
File: sketchings-random-quote-playground.php
<?php
/*
Plugin Name: WP Random Quote Playground
*/
Interacting with WordPress
Themes Look and feel of the site
Plugins Added functionality of the site
Hooks Themes and Plugins “Hook” into Core using “Actions” and “Filters”
Actions Add or Remove Functionality
Filters Change Data
Actions: Add or Remove Functionality
function sketchings_random_quote_styles() {
wp_register_style(
'quote-styles',
plugin_dir_url( __FILE__ ) . 'css/styles.css'
);
wp_enqueue_style( 'quote-styles' );
}
add_action( 'wp_head', 'sketchings_random_quote_styles' );
Filters: Change Data
function sketchings_tooltip($content) {
$search = array(' motivation',' encouragement', '...');
foreach ($search as $word) {
$content = str_ireplace(
$word,
' <span class="tooltip">' . trim($word)
. '<span class="tooltiptext">'
. sketchings_random_quote($word)
. '</span></span>',
$content
);
}
return $content;
}
add_filter ('the_content', 'sketchings_tooltip');
Shortcode: Add to post or widgets
function sketchings_random_quote_shortcode( $atts ) {
return sketchings_random_quote( $atts['category'] );
}
add_shortcode(
'random-quote',
'Sketchings_random_quote_shortcode'
);
// to use in page or widget
[random-quote category = "motivation"]
Resources
PHP
DOCS: php.net
phptherightway.com
teamtreehouse.com
Local Server: MAMP
WordPress
DOCS: codex.wordpress.org
wpmudev.org
wpshout.com
SmashingMagazine.com
Where to find Alena:
TeamTreehouse.com
Meetup.com: PDX-PHP
Sketchings.com
Twitter: @sketchings
GitHub: sketchings
https://github.com/sketchings/wp-random-quote-playground

More Related Content

What's hot (20)

ODP
Moose talk at FOSDEM 2011 (Perl devroom)
xSawyer
 
PDF
The Perl6 Type System
abrummett
 
PDF
Simple Ways To Be A Better Programmer (OSCON 2007)
Michael Schwern
 
PPTX
PHP Functions & Arrays
Henry Osborne
 
PDF
Benchmarking Perl (Chicago UniForum 2006)
brian d foy
 
PDF
OO Perl with Moose
Nelo Onyiah
 
PDF
WordPress Cuztom Helper
slicejack
 
PDF
8時間耐久CakePHP2 勉強会
Yusuke Ando
 
PPT
Class 4 - PHP Arrays
Ahmed Swilam
 
PDF
Sorting arrays in PHP
Vineet Kumar Saini
 
PDF
Introdução ao Perl 6
garux
 
PDF
Php array
Nikul Shah
 
PDF
Perl.Hacks.On.Vim
Lin Yo-An
 
PPT
Class 5 - PHP Strings
Ahmed Swilam
 
PDF
Creating a compiler in Perl 6
Andrew Shitov
 
KEY
Introduction to Perl Best Practices
José Castro
 
PDF
Good Evils In Perl
Kang-min Liu
 
PDF
Magicke metody v Pythonu
Jirka Vejrazka
 
PDF
Arrays in PHP
Vineet Kumar Saini
 
PDF
DBIx::Class beginners
leo lapworth
 
Moose talk at FOSDEM 2011 (Perl devroom)
xSawyer
 
The Perl6 Type System
abrummett
 
Simple Ways To Be A Better Programmer (OSCON 2007)
Michael Schwern
 
PHP Functions & Arrays
Henry Osborne
 
Benchmarking Perl (Chicago UniForum 2006)
brian d foy
 
OO Perl with Moose
Nelo Onyiah
 
WordPress Cuztom Helper
slicejack
 
8時間耐久CakePHP2 勉強会
Yusuke Ando
 
Class 4 - PHP Arrays
Ahmed Swilam
 
Sorting arrays in PHP
Vineet Kumar Saini
 
Introdução ao Perl 6
garux
 
Php array
Nikul Shah
 
Perl.Hacks.On.Vim
Lin Yo-An
 
Class 5 - PHP Strings
Ahmed Swilam
 
Creating a compiler in Perl 6
Andrew Shitov
 
Introduction to Perl Best Practices
José Castro
 
Good Evils In Perl
Kang-min Liu
 
Magicke metody v Pythonu
Jirka Vejrazka
 
Arrays in PHP
Vineet Kumar Saini
 
DBIx::Class beginners
leo lapworth
 

Similar to WordCamp Portland 2018: PHP for WordPress (20)

KEY
Can't Miss Features of PHP 5.3 and 5.4
Jeff Carouth
 
KEY
Spl Not A Bridge Too Far phpNW09
Michelangelo van Dam
 
PPTX
Chapter 2 wbp.pptx
40NehaPagariya
 
PDF
Mirror, mirror on the wall - Building a new PHP reflection library (Nomad PHP...
James Titcumb
 
PDF
Python basic
Saifuddin Kaijar
 
PPTX
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...
Dhivyaa C.R
 
PPTX
UNIT IV (4).pptx
DrDhivyaaCRAssistant
 
PDF
PHP Conference Asia 2016
Britta Alex
 
PPT
Propel sfugmd
iKlaus
 
PPTX
Open Source Search: An Analysis
Justin Finkelstein
 
PPTX
Php functions
JIGAR MAKHIJA
 
PDF
Nickolay Shmalenuk.Render api eng.DrupalCamp Kyiv 2011
camp_drupal_ua
 
PDF
PHP for Python Developers
Carlos Vences
 
PDF
How to write code you won't hate tomorrow
Pete McFarlane
 
PPTX
Php & my sql
Norhisyam Dasuki
 
KEY
Intermediate PHP
Bradley Holt
 
PDF
Separation of concerns - DPC12
Stephan Hochdörfer
 
PPT
Php my sql - functions - arrays - tutorial - programmerblog.net
Programmer Blog
 
PPT
Arrays in php
Laiby Thomas
 
ODP
Intro to The PHP SPL
Chris Tankersley
 
Can't Miss Features of PHP 5.3 and 5.4
Jeff Carouth
 
Spl Not A Bridge Too Far phpNW09
Michelangelo van Dam
 
Chapter 2 wbp.pptx
40NehaPagariya
 
Mirror, mirror on the wall - Building a new PHP reflection library (Nomad PHP...
James Titcumb
 
Python basic
Saifuddin Kaijar
 
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...
Dhivyaa C.R
 
UNIT IV (4).pptx
DrDhivyaaCRAssistant
 
PHP Conference Asia 2016
Britta Alex
 
Propel sfugmd
iKlaus
 
Open Source Search: An Analysis
Justin Finkelstein
 
Php functions
JIGAR MAKHIJA
 
Nickolay Shmalenuk.Render api eng.DrupalCamp Kyiv 2011
camp_drupal_ua
 
PHP for Python Developers
Carlos Vences
 
How to write code you won't hate tomorrow
Pete McFarlane
 
Php & my sql
Norhisyam Dasuki
 
Intermediate PHP
Bradley Holt
 
Separation of concerns - DPC12
Stephan Hochdörfer
 
Php my sql - functions - arrays - tutorial - programmerblog.net
Programmer Blog
 
Arrays in php
Laiby Thomas
 
Intro to The PHP SPL
Chris Tankersley
 
Ad

More from Alena Holligan (20)

PDF
2023 Longhorn PHP - Learn to Succeed .pdf
Alena Holligan
 
PDF
Environmental variables
Alena Holligan
 
PDF
Dev parent
Alena Holligan
 
PDF
Dependency Injection
Alena Holligan
 
PDF
Dependency Management
Alena Holligan
 
PDF
Experiential Project Design
Alena Holligan
 
PDF
Reduce Reuse Refactor
Alena Holligan
 
PDF
Organization Patterns: MVC
Alena Holligan
 
PDF
When & Why: Interfaces, abstract classes, traits
Alena Holligan
 
PDF
Object Features
Alena Holligan
 
PDF
Obect-Oriented Collaboration
Alena Holligan
 
PDF
Let's Talk Scope
Alena Holligan
 
PDF
Demystifying Object-Oriented Programming #phpbnl18
Alena Holligan
 
PDF
Exploiting the Brain for Fun and Profit
Alena Holligan
 
PDF
Environmental Variables
Alena Holligan
 
PDF
Learn to succeed
Alena Holligan
 
PDF
Demystifying oop
Alena Holligan
 
PDF
Demystifying Object-Oriented Programming - PHP[tek] 2017
Alena Holligan
 
PDF
Demystifying Object-Oriented Programming - PHP UK Conference 2017
Alena Holligan
 
PDF
Exploiting the Brain for Fun & Profit #zendcon2016
Alena Holligan
 
2023 Longhorn PHP - Learn to Succeed .pdf
Alena Holligan
 
Environmental variables
Alena Holligan
 
Dev parent
Alena Holligan
 
Dependency Injection
Alena Holligan
 
Dependency Management
Alena Holligan
 
Experiential Project Design
Alena Holligan
 
Reduce Reuse Refactor
Alena Holligan
 
Organization Patterns: MVC
Alena Holligan
 
When & Why: Interfaces, abstract classes, traits
Alena Holligan
 
Object Features
Alena Holligan
 
Obect-Oriented Collaboration
Alena Holligan
 
Let's Talk Scope
Alena Holligan
 
Demystifying Object-Oriented Programming #phpbnl18
Alena Holligan
 
Exploiting the Brain for Fun and Profit
Alena Holligan
 
Environmental Variables
Alena Holligan
 
Learn to succeed
Alena Holligan
 
Demystifying oop
Alena Holligan
 
Demystifying Object-Oriented Programming - PHP[tek] 2017
Alena Holligan
 
Demystifying Object-Oriented Programming - PHP UK Conference 2017
Alena Holligan
 
Exploiting the Brain for Fun & Profit #zendcon2016
Alena Holligan
 
Ad

Recently uploaded (20)

PPTX
Simple and concise overview about Quantum computing..pptx
mughal641
 
PPTX
AVL ( audio, visuals or led ), technology.
Rajeshwri Panchal
 
PDF
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
PDF
Generative AI vs Predictive AI-The Ultimate Comparison Guide
Lily Clark
 
PDF
OFFOFFBOX™ – A New Era for African Film | Startup Presentation
ambaicciwalkerbrian
 
PDF
The Future of Mobile Is Context-Aware—Are You Ready?
iProgrammer Solutions Private Limited
 
PDF
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
PDF
Per Axbom: The spectacular lies of maps
Nexer Digital
 
PDF
RAT Builders - How to Catch Them All [DeepSec 2024]
malmoeb
 
PDF
How ETL Control Logic Keeps Your Pipelines Safe and Reliable.pdf
Stryv Solutions Pvt. Ltd.
 
PPTX
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
 
PPTX
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
PDF
State-Dependent Conformal Perception Bounds for Neuro-Symbolic Verification
Ivan Ruchkin
 
PPTX
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
PDF
OpenInfra ID 2025 - Are Containers Dying? Rethinking Isolation with MicroVMs.pdf
Muhammad Yuga Nugraha
 
PDF
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
PPTX
AI Code Generation Risks (Ramkumar Dilli, CIO, Myridius)
Priyanka Aash
 
PDF
Brief History of Internet - Early Days of Internet
sutharharshit158
 
PPTX
Machine Learning Benefits Across Industries
SynapseIndia
 
PPTX
Agile Chennai 18-19 July 2025 | Workshop - Enhancing Agile Collaboration with...
AgileNetwork
 
Simple and concise overview about Quantum computing..pptx
mughal641
 
AVL ( audio, visuals or led ), technology.
Rajeshwri Panchal
 
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
Generative AI vs Predictive AI-The Ultimate Comparison Guide
Lily Clark
 
OFFOFFBOX™ – A New Era for African Film | Startup Presentation
ambaicciwalkerbrian
 
The Future of Mobile Is Context-Aware—Are You Ready?
iProgrammer Solutions Private Limited
 
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
Per Axbom: The spectacular lies of maps
Nexer Digital
 
RAT Builders - How to Catch Them All [DeepSec 2024]
malmoeb
 
How ETL Control Logic Keeps Your Pipelines Safe and Reliable.pdf
Stryv Solutions Pvt. Ltd.
 
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
 
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
State-Dependent Conformal Perception Bounds for Neuro-Symbolic Verification
Ivan Ruchkin
 
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
OpenInfra ID 2025 - Are Containers Dying? Rethinking Isolation with MicroVMs.pdf
Muhammad Yuga Nugraha
 
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
AI Code Generation Risks (Ramkumar Dilli, CIO, Myridius)
Priyanka Aash
 
Brief History of Internet - Early Days of Internet
sutharharshit158
 
Machine Learning Benefits Across Industries
SynapseIndia
 
Agile Chennai 18-19 July 2025 | Workshop - Enhancing Agile Collaboration with...
AgileNetwork
 

WordCamp Portland 2018: PHP for WordPress

  • 1. for Speaking the language of WordPress 2018
  • 2. Alena Holligan PHP Teacher at Treehouse PHPDX User Group Leader Web Developer for 20 years https://github.com/sketchings/ wp-random-quote-playground
  • 3. PART 1: Basics of PHP Variables Conditionals Functions
  • 4. PART 2: Build A Plugin Minimum Viable Plugin (MVP) Interacting with WordPress Adding a Shortcode
  • 6. Cany Arrays $mm1 = [ 'orange','orange','orange','red','yellow','yellow', 'yellow','green', 'green', 'green', 'blue', 'blue' ]; var_dump(array_count_values($mm1)); $mm2 = [ 'yellow', 'green', 'green', 'green', 'blue', 'blue', 'orange', 'orange', 'orange', 'red', 'red', 'blue' ]; $bag = [$mm1, $mm2];
  • 7. Variables: scalar (boolean, integer, float, string) $workday = false; // boolean $hour = 6; // integer $price = 15.25; // float $quote = "Your future is created by what you do today," . " not tomorrow."; // string $quote .= " — Robert Kiyosaki"; // concatenation define( 'FORCE_SSL_ADMIN', true); // doesn’t change
  • 8. PHP Basics <?php /* Multi Line Comment Used for documentation */ $total = (6*3) + (12/4) - 2.5;// math 18.5 echo "This will display $total value on page.";// 18.5 echo 'This will display $total name on page.';// $total var_dump($total, FORCE_SSL_ADMIN); // float(18.5) bool(true) // leave off the closing tag if ending a file ?>
  • 9. Variables: array $motivation = array( "You don’t have to be great to start...", "Set a goal that makes you want to jump...", "You’ll never change your life until...", "This is not just another day, this is...", "Leave your ego at the door every morning...", ); $motivation[] = $quote; echo $motivation[0];
  • 11. Functions: Build-In string substr — Return part of a string int strlen — Get string length string trim — Strip whitespace (or other characters) from the beginning and end of a string (also rtrim & ltrim) int strpos — Find position of first occurrence of substring in string string strtolower — Make a string lowercase (also strtoupper) mixed str_replace — Replace all occurrences of the search string with the replacement string Array Functions array array_merge — Merge one or more arrays bool in_array — Checks if a value exists in an array array array_keys — Return all the keys (or subset) of an array
  • 12. Functions: User Defined function sketchings_quotes ( $category = null ) { $motivation = array(...); $encouragement = array(...); if ( in_array(strtolower(trim($category)), array('motivation', 'motivated', 'motivate')) ) { return $motivation; } if ( in_array(strtolower(trim($category)), array('encouragement','encourage','encouraging')) ) { return $encouragement; } return array_merge($motivation, $encouragement); }
  • 13. Conditionals function sketchings_random_quote ( $category = null ) { $hour = date('H'); if (!empty($category)) { $quotes = sketchings_quotes($category); } elseif ($hour > 4 && $hour < 17) { $quotes = sketchings_quotes('motivation'); } else { $quotes = sketchings_quotes('encouragement'); } $index = array_rand($quotes); return $quotes[$index]; }
  • 14. Loops for − loops through a block of code a specified number of times. while − loops through a block of code if and as long as a specified condition is true. do...while − loops through a block of code once, and then repeats the loop as long as a special condition is true. foreach − loops through a block of code for each element in an array.
  • 15. Loops: foreach function sketchings_tooltip($content) { $search = array(' motivation',' encouragement', '...'); foreach ($search as $word) { $content = str_ireplace( $word, ' <span class="tooltip">' . $word . '<span class="tooltiptext">' . sketchings_random_quote($word) . '</span></span>', $content ); } return $content; }
  • 17. Minimum Viable Plugin (MVP) File: sketchings-random-quote-playground.php <?php /* Plugin Name: WP Random Quote Playground */
  • 18. Interacting with WordPress Themes Look and feel of the site Plugins Added functionality of the site Hooks Themes and Plugins “Hook” into Core using “Actions” and “Filters” Actions Add or Remove Functionality Filters Change Data
  • 19. Actions: Add or Remove Functionality function sketchings_random_quote_styles() { wp_register_style( 'quote-styles', plugin_dir_url( __FILE__ ) . 'css/styles.css' ); wp_enqueue_style( 'quote-styles' ); } add_action( 'wp_head', 'sketchings_random_quote_styles' );
  • 20. Filters: Change Data function sketchings_tooltip($content) { $search = array(' motivation',' encouragement', '...'); foreach ($search as $word) { $content = str_ireplace( $word, ' <span class="tooltip">' . trim($word) . '<span class="tooltiptext">' . sketchings_random_quote($word) . '</span></span>', $content ); } return $content; } add_filter ('the_content', 'sketchings_tooltip');
  • 21. Shortcode: Add to post or widgets function sketchings_random_quote_shortcode( $atts ) { return sketchings_random_quote( $atts['category'] ); } add_shortcode( 'random-quote', 'Sketchings_random_quote_shortcode' ); // to use in page or widget [random-quote category = "motivation"]
  • 22. Resources PHP DOCS: php.net phptherightway.com teamtreehouse.com Local Server: MAMP WordPress DOCS: codex.wordpress.org wpmudev.org wpshout.com SmashingMagazine.com
  • 23. Where to find Alena: TeamTreehouse.com Meetup.com: PDX-PHP Sketchings.com Twitter: @sketchings GitHub: sketchings https://github.com/sketchings/wp-random-quote-playground