SlideShare a Scribd company logo
1 of 47
Download to read offline
Montreal WordCamp 2015
@MichaelBontyes
[i-am-a-shortcode]
A shortcode is a WordPress-specific code
that lets you do nifty things with very little effort.
Shortcodes can embed files or create objects that
would normally require lots of complicated,
ugly code in just one line.
Shortcode = shortcut.
https://en.support.wordpress.com/shortcodes/
// Use shortcode in a PHP file (outside the post editor).
echo do_shortcode( '[hello]' );
https://codex.wordpress.org/Function_Reference/do_shortcode
<?php
function hello() {
return "Hello WordCamp Mtl !";
}
add_shortcode( 'hello' , 'hello' );
​?>
Site / Page
Editor
Functions
Hello WordCamp Mtl !
[hello]
Wordpress.com … & WordCamp.org!
[gallery type=”rectangular”]
https://en.support.wordpress.com/shortcodes/
Jetpack.me
http://jetpack.me/support/subscriptions/
[jetpack_subscription_form]
Développeurs de thèmes
Développeurs de plugins
[geo_mashup_map]
https://github.com/cyberhobo/wordpress-geo-mashup/wiki/Getting-Started
Développeurs de plugins
[timeline-express]
https://github.com/EvanHerman/Timeline-Express
+
+
un peu de
Simple
add_shortcode()
Paramétrable
shortcode_atts()
Flexible
do_shortcode()
Complexe
ob_start()
add_shortcode()
Functions.php / includes / plugin Shortcode
1 <?php
Résultat
add_shortcode()
Functions.php / includes / plugin Shortcode
1
2
3
4
5
6
7
8
<?php
// Create the function for the shortcode
function message_maker() {
}
​?>
Résultat
add_shortcode()
Functions.php / includes / plugin Shortcode
1
2
3
4
5
6
7
8
9
10
<?php
// Create the function for the shortcode
function message_maker() {
return 'Hello WordCamp Mtl !';
}
​?>
Résultat
add_shortcode()
Functions.php / includes / plugin Shortcode
1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
// Create the function for the shortcode
function message_maker() {
return 'Hello WordCamp Mtl !';
}
// Register the Shortcode using the API
add_shortcode('message' , 'message_maker' );
​?>
Résultat
add_shortcode()
Functions.php / includes / plugin Shortcode
1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
// Create the function for the shortcode
function message_maker() {
return 'Hello WordCamp Mtl !';
}
// Register the Shortcode using the API
add_shortcode('message' , 'message_maker' );
​?>
[message]
Résultat
add_shortcode()
Functions.php / includes / plugin Shortcode
1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
// Create the function for the shortcode
function message_maker() {
return 'Hello WordCamp Mtl !';
}
// Register the Shortcode using the API
add_shortcode('message' , 'message_maker' );
​?>
[message]
Résultat
Hello WordCamp Mtl !
Functions.php / includes / plugin Shortcode
1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
// Create the function for the shortcode
function current_year() {
return date('Y');
}
// Register the Shortcode using the API
add_shortcode('year' , 'current_year' );
​?>
Résultat
Functions.php / includes / plugin Shortcode
1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
// Create the function for the shortcode
function current_year() {
return date('Y');
}
// Register the Shortcode using the API
add_shortcode('year' , 'current_year' );
​?>
[year]
Résultat
Functions.php / includes / plugin Shortcode
1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
// Create the function for the shortcode
function current_year() {
return date('Y');
}
// Register the Shortcode using the API
add_shortcode('year' , 'current_year' );
​?>
[year]
Résultat
2015
Simple
add_shortcode()
Paramétrable
shortcode_atts()
Flexible
do_shortcode()
Complexe
ob_start()
shortcode_atts()
Functions.php / includes / plugin Shortcode
1
2
3
4
5
6
7
8
9
<?php
// Create the shortcode function
function lorem_ipsum() {
}
// Register the Shortcode using the API
add_shortcode( 'lorem' , 'lorem_ipsum' );
?>
Résultat
shortcode_atts()
Functions.php / includes / plugin Shortcode
1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
// Create the shortcode function
function lorem_ipsum( $atts ) {
// Handling the Attributes
$a = shortcode_atts( array(
'sentences' => 1
), $atts );
}
// Register the Shortcode using the API
add_shortcode( 'lorem' , 'lorem_ipsum' );
?>
Résultat
shortcode_atts()
Functions.php / includes / plugin Shortcode
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
// Create the shortcode function
function lorem_ipsum( $atts ) {
// Handling the Attributes
$a = shortcode_atts( array(
'sentences' => 1
), $atts );
// Get the Lorem Ipsum
$lorem_ipsum = get_lorem_ipsum( $a['sentences'] );
return $lorem_ipsum;
}
// Register the Shortcode using the API
add_shortcode( 'lorem' , 'lorem_ipsum' );
?>
Résultat
shortcode_atts()
Functions.php / includes / plugin Shortcode
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
// Create the shortcode function
function lorem_ipsum( $atts ) {
// Handling the Attributes
$a = shortcode_atts( array(
'sentences' => 1
), $atts );
// Get the Lorem Ipsum
$lorem_ipsum = get_lorem_ipsum( $a['sentences'] );
return $lorem_ipsum;
}
// Register the Shortcode using the API
add_shortcode( 'lorem' , 'lorem_ipsum' );
?>
[lorem sentences=“2”]
Résultat
shortcode_atts()
Functions.php / includes / plugin Shortcode
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
// Create the shortcode function
function lorem_ipsum( $atts ) {
// Handling the Attributes
$a = shortcode_atts( array(
'sentences' => 1
), $atts );
// Get the Lorem Ipsum
$lorem_ipsum = get_lorem_ipsum( $a['sentences'] );
return $lorem_ipsum;
}
// Register the Shortcode using the API
add_shortcode( 'lorem' , 'lorem_ipsum' );
?>
[lorem sentences=“2”]
Résultat
Lorem ipsum dolor sit amet,
consectetur adipiscing elit.
Aliter enim explicari, quod
quaeritur, non potest.
shortcode_atts()
Functions.php / includes / plugin Shortcode
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
// Create the shortcode function
function get_thumbnail( $atts ) {
// Handling the Attributes
$a = shortcode_atts( array(
'size' => 'thumbnail',
'class' => 'circle'
), $atts );
// Get the Thumbnail
echo get_the_post_thumbnail(
get_option('page_on_front'), $a['size'],
array( 'class' => $a['class'] ) );
}
// Register the Shortcode using the API
add_shortcode( 'thumbnail' , 'get_thumbnail' );
?>
Résultat
shortcode_atts()
Functions.php / includes / plugin Shortcode
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
// Create the shortcode function
function get_thumbnail( $atts ) {
// Handling the Attributes
$a = shortcode_atts( array(
'size' => 'thumbnail',
'class' => 'circle'
), $atts );
// Get the Thumbnail
echo get_the_post_thumbnail(
get_option('page_on_front'), $a['size'],
array( 'class' => $a['class'] ) );
}
// Register the Shortcode using the API
add_shortcode( 'thumbnail' , 'get_thumbnail' );
?>
[thumbnail size=”medium”
class=“circle”]
Résultat
shortcode_atts()
Functions.php / includes / plugin Shortcode
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
// Create the shortcode function
function get_thumbnail( $atts ) {
// Handling the Attributes
$a = shortcode_atts( array(
'size' => 'thumbnail',
'class' => 'circle'
), $atts );
// Get the Thumbnail
return get_the_post_thumbnail(
get_option('page_on_front'), $a['size'],
array( 'class' => $a['class'] ) );
}
// Register the Shortcode using the API
add_shortcode( 'thumbnail' , 'get_thumbnail' );
?>
[thumbnail size=”medium”
class=“circle”]
Résultat
Simple
add_shortcode()
Paramétrable
shortcode_atts()
Flexible
do_shortcode()
Complexe
ob_start()
$content
Functions.php / includes / plugin Shortcode
1
2
3
4
5
6
7
8
9
10
11
<?php
// Create the function for the shortcode
function message_maker( $atts ) {
}
// Register the Shortcode using the API
add_shortcode('message' , 'message_maker' );
​?>
Résultat
$content
Functions.php / includes / plugin Shortcode
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
// Create the function for the shortcode
function message_maker( $atts, $content = null ) {
// Upper the $content
return strtoupper($content);
}
// Register the Shortcode using the API
add_shortcode('message' , 'message_maker' );
​?>
Résultat
$content
Functions.php / includes / plugin Shortcode
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
// Create the function for the shortcode
function message_maker( $atts, $content = null ) {
// Upper the $content
return strtoupper($content);
}
// Register the Shortcode using the API
add_shortcode('message' , 'message_maker' );
​?>
[message]
tout ce paragraphe
devrait être en
lettres capitales
[/message]
Résultat
$content
Functions.php / includes / plugin Shortcode
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
// Create the function for the shortcode
function message_maker( $atts, $content = null ) {
// Upper the $content
return strtoupper($content);
}
// Register the Shortcode using the API
add_shortcode('message' , 'message_maker' );
​?>
[message]
tout ce paragraphe
devrait être en
lettres capitales
[/message]
Résultat
TOUT CE PARAGRAPHE
DEVRAIT ÊTRE EN
LETTRES CAPITALES
do_shortcode()
Functions.php / includes / plugin Shortcode
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
// Create the function for the shortcode
function message_maker( $atts, $content = null ) {
// Upper the $content
return strtoupper(do_shortcode($content));
}
// Register the Shortcode using the API
add_shortcode('message' , 'message_maker' );
​?>
Résultat
do_shortcode()
Functions.php / includes / plugin Shortcode
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
// Create the function for the shortcode
function message_maker( $atts, $content = null ) {
// Upper the $content
return strtoupper(do_shortcode($content));
}
// Register the Shortcode using the API
add_shortcode('message' , 'message_maker' );
​?>
[message]
Hello WordCamp Montreal
[year] !
[/message]
Résultat
do_shortcode()
Functions.php / includes / plugin Shortcode
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
// Create the function for the shortcode
function message_maker( $atts, $content = null ) {
// Upper the $content
return strtoupper(do_shortcode($content));
}
// Register the Shortcode using the API
add_shortcode('message' , 'message_maker' );
​?>
[message]
Hello WordCamp Montreal
[year] !
[/message]
Résultat
HELLO WORDCAMP MONTREAL
2015 !
Simple
add_shortcode()
Paramétrable
shortcode_atts()
Flexible
do_shortcode()
Complexe
ob_start()
ob_start()
Functions.php / includes / plugin Shortcode
1
2
3
4
5
6
7
8
<?php
// Create the shortcode function
function message_maker() {
}
// Register the Shortcode using the API
add_shortcode('message' , 'message_maker' );
?>
Résultat
ob_start()
Functions.php / includes / plugin Shortcode
1
2
3
4
5
6
7
8
9
10
11
<?php
// Create the shortcode function
function message_maker() {
// Démarrage du buffer de sortie
ob_start();
}
// Register the Shortcode using the API
add_shortcode('message' , 'message_maker' );
?>
Résultat
ob_start()
Functions.php / includes / plugin Shortcode
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
// Create the shortcode function
function message_maker() {
// Démarrage du buffer de sortie
ob_start();
?>
<ul>
<li>Post title 1</li>
<li>Post title 2</li>
</ul>
<?php
}
// Register the Shortcode using the API
add_shortcode('message' , 'message_maker' );
?>
Résultat
ob_start()
Functions.php / includes / plugin Shortcode
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
// Create the shortcode function
function message_maker() {
// Démarrage du buffer de sortie
ob_start();
?>
<ul>
<li>Post title 1</li>
<li>Post title 2</li>
</ul>
<?php
// Lecture du buffer et effacement
return ob_get_clean();
}
Résultat
ob_start()
Functions.php / includes / plugin Shortcode
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
// Create the shortcode function
function message_maker() {
// Démarrage du buffer de sortie
ob_start();
?>
<ul>
<li>Post title 1</li>
<li>Post title 2</li>
</ul>
<?php
// Lecture du buffer et effacement
return ob_get_clean();
}
[message]
Résultat
ob_start()
Functions.php / includes / plugin Shortcode
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
// Create the shortcode function
function message_maker() {
// Démarrage du buffer de sortie
ob_start();
?>
<ul>
<li>Post title 1</li>
<li>Post title 2</li>
</ul>
<?php
// Lecture du buffer et effacement
return ob_get_clean();
}
[message]
Résultat
• Post Title 1
• Post Title 2
Functions.php / includes / plugin Shortcode
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
function show_posts( $atts, $content = null ) {
// Handling the Attributes
$a = shortcode_atts( array(
'type' => 'projects'
), $atts );
// Get the posts filtered by Post Type
$query = new WP_Query( array( 'post_type' => $a['type'] ) );
ob_start();
echo '<h1>'.do_shortcode($content).'</h1>';
echo '<ul>‘;
// The WP Query Loop
while ( $query->have_posts() ) {
$query->the_post(); ?>
<!-- Build the HTML -->
<li><?php the_title(); ?></li>
</ul>
<?php }
echo '</ul>‘;
// Restore the Global $post variable - Avoid WP Query inception conflict
wp_reset_postdata();
return ob_get_clean();
}
add_shortcode('posts' , 'show_posts' );
[posts type=“projects”]
Mes derniers projets :
[/posts]
Résultat
Mes derniers projets :
• Project Title 1
• Project Title 2
• Project Title 3
http://gndev.info/shortcodes-ultimate/
Shortcodes Ultimate for WordPress Including a custom
shortcode maker
API / Addon
https://github.com/gndev/shortcodes-ultimate
https://codex.wordpress.org/Shortcode_API
Wordpress Codex – Shortcode API
Montreal WordCamp 2015 | @MichaelBontyes
https://github.com/michaelbontyes/Presentation-Create-Your-Own-Shortcode
Presentation “How to create your own shortcode”

More Related Content

What's hot

Silex: From nothing to an API
Silex: From nothing to an APISilex: From nothing to an API
Silex: From nothing to an API
chrisdkemper
 

What's hot (20)

Creating REST Applications with the Slim Micro-Framework by Vikram Vaswani
Creating REST Applications with the Slim Micro-Framework by Vikram VaswaniCreating REST Applications with the Slim Micro-Framework by Vikram Vaswani
Creating REST Applications with the Slim Micro-Framework by Vikram Vaswani
 
Boost your angular app with web workers
Boost your angular app with web workersBoost your angular app with web workers
Boost your angular app with web workers
 
Symfony2 and AngularJS
Symfony2 and AngularJSSymfony2 and AngularJS
Symfony2 and AngularJS
 
SymfonyCon Berlin 2016 - Symfony Plugin for PhpStorm - 3 years later
SymfonyCon Berlin 2016 - Symfony Plugin for PhpStorm - 3 years laterSymfonyCon Berlin 2016 - Symfony Plugin for PhpStorm - 3 years later
SymfonyCon Berlin 2016 - Symfony Plugin for PhpStorm - 3 years later
 
Flutter State Management Using GetX.pdf
Flutter State Management Using GetX.pdfFlutter State Management Using GetX.pdf
Flutter State Management Using GetX.pdf
 
Caldera Learn - LoopConf WP API + Angular FTW Workshop
Caldera Learn - LoopConf WP API + Angular FTW WorkshopCaldera Learn - LoopConf WP API + Angular FTW Workshop
Caldera Learn - LoopConf WP API + Angular FTW Workshop
 
Keeping the frontend under control with Symfony and Webpack
Keeping the frontend under control with Symfony and WebpackKeeping the frontend under control with Symfony and Webpack
Keeping the frontend under control with Symfony and Webpack
 
Complex Sites with Silex
Complex Sites with SilexComplex Sites with Silex
Complex Sites with Silex
 
Single Page Web Apps As WordPress Admin Interfaces Using AngularJS & The Word...
Single Page Web Apps As WordPress Admin Interfaces Using AngularJS & The Word...Single Page Web Apps As WordPress Admin Interfaces Using AngularJS & The Word...
Single Page Web Apps As WordPress Admin Interfaces Using AngularJS & The Word...
 
REST API for your WP7 App
REST API for your WP7 AppREST API for your WP7 App
REST API for your WP7 App
 
Build a video chat application with twilio, rails, and javascript (part 1)
Build a video chat application with twilio, rails, and javascript (part 1)Build a video chat application with twilio, rails, and javascript (part 1)
Build a video chat application with twilio, rails, and javascript (part 1)
 
Symfony Guard Authentication: Fun with API Token, Social Login, JWT and more
Symfony Guard Authentication: Fun with API Token, Social Login, JWT and moreSymfony Guard Authentication: Fun with API Token, Social Login, JWT and more
Symfony Guard Authentication: Fun with API Token, Social Login, JWT and more
 
Laravel 101
Laravel 101Laravel 101
Laravel 101
 
Symfony 2
Symfony 2Symfony 2
Symfony 2
 
Rails antipatterns
Rails antipatternsRails antipatterns
Rails antipatterns
 
Rails antipattern-public
Rails antipattern-publicRails antipattern-public
Rails antipattern-public
 
Silex: From nothing to an API
Silex: From nothing to an APISilex: From nothing to an API
Silex: From nothing to an API
 
Elefrant [ng-Poznan]
Elefrant [ng-Poznan]Elefrant [ng-Poznan]
Elefrant [ng-Poznan]
 
Using Renderless Components in Vue.js during your software development.
Using Renderless Components in Vue.js during your software development.Using Renderless Components in Vue.js during your software development.
Using Renderless Components in Vue.js during your software development.
 
Introduction to AngularJS For WordPress Developers
Introduction to AngularJS For WordPress DevelopersIntroduction to AngularJS For WordPress Developers
Introduction to AngularJS For WordPress Developers
 

Similar to WCMTL 15 - Create your own shortcode (Fr)

Speed up your developments with Symfony2
Speed up your developments with Symfony2Speed up your developments with Symfony2
Speed up your developments with Symfony2
Hugo Hamon
 
Intro To Mvc Development In Php
Intro To Mvc Development In PhpIntro To Mvc Development In Php
Intro To Mvc Development In Php
funkatron
 
Benefit of CodeIgniter php framework
Benefit of CodeIgniter php frameworkBenefit of CodeIgniter php framework
Benefit of CodeIgniter php framework
Bo-Yi Wu
 

Similar to WCMTL 15 - Create your own shortcode (Fr) (20)

Extend sdk
Extend sdkExtend sdk
Extend sdk
 
Introduction to angular js
Introduction to angular jsIntroduction to angular js
Introduction to angular js
 
Extend sdk
Extend sdkExtend sdk
Extend sdk
 
HTTP Middlewares in PHP by Eugene Dounar
HTTP Middlewares in PHP by Eugene DounarHTTP Middlewares in PHP by Eugene Dounar
HTTP Middlewares in PHP by Eugene Dounar
 
[Bristol WordPress] Supercharging WordPress Development
[Bristol WordPress] Supercharging WordPress Development[Bristol WordPress] Supercharging WordPress Development
[Bristol WordPress] Supercharging WordPress Development
 
Building Cloud Castles
Building Cloud CastlesBuilding Cloud Castles
Building Cloud Castles
 
WordCamp Montreal 2016 WP-API + React with server rendering
WordCamp Montreal 2016  WP-API + React with server renderingWordCamp Montreal 2016  WP-API + React with server rendering
WordCamp Montreal 2016 WP-API + React with server rendering
 
Introduction to Zend Framework
Introduction to Zend FrameworkIntroduction to Zend Framework
Introduction to Zend Framework
 
Speed up your developments with Symfony2
Speed up your developments with Symfony2Speed up your developments with Symfony2
Speed up your developments with Symfony2
 
Shortcodes In-Depth
Shortcodes In-DepthShortcodes In-Depth
Shortcodes In-Depth
 
CodeIgniter PHP MVC Framework
CodeIgniter PHP MVC FrameworkCodeIgniter PHP MVC Framework
CodeIgniter PHP MVC Framework
 
Intro To Mvc Development In Php
Intro To Mvc Development In PhpIntro To Mvc Development In Php
Intro To Mvc Development In Php
 
Mojolicious
MojoliciousMojolicious
Mojolicious
 
Ch ch-changes cake php2
Ch ch-changes cake php2Ch ch-changes cake php2
Ch ch-changes cake php2
 
Codegnitorppt
CodegnitorpptCodegnitorppt
Codegnitorppt
 
Benefit of CodeIgniter php framework
Benefit of CodeIgniter php frameworkBenefit of CodeIgniter php framework
Benefit of CodeIgniter php framework
 
Using Geeklog as a Web Application Framework
Using Geeklog as a Web Application FrameworkUsing Geeklog as a Web Application Framework
Using Geeklog as a Web Application Framework
 
Ruby on Rails vs ASP.NET MVC
Ruby on Rails vs ASP.NET MVCRuby on Rails vs ASP.NET MVC
Ruby on Rails vs ASP.NET MVC
 
angular fundamentals.pdf
angular fundamentals.pdfangular fundamentals.pdf
angular fundamentals.pdf
 
Lean Php Presentation
Lean Php PresentationLean Php Presentation
Lean Php Presentation
 

Recently uploaded

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Recently uploaded (20)

[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 

WCMTL 15 - Create your own shortcode (Fr)

  • 2. [i-am-a-shortcode] A shortcode is a WordPress-specific code that lets you do nifty things with very little effort. Shortcodes can embed files or create objects that would normally require lots of complicated, ugly code in just one line. Shortcode = shortcut. https://en.support.wordpress.com/shortcodes/
  • 3. // Use shortcode in a PHP file (outside the post editor). echo do_shortcode( '[hello]' ); https://codex.wordpress.org/Function_Reference/do_shortcode
  • 4. <?php function hello() { return "Hello WordCamp Mtl !"; } add_shortcode( 'hello' , 'hello' ); ​?> Site / Page Editor Functions Hello WordCamp Mtl ! [hello]
  • 5. Wordpress.com … & WordCamp.org! [gallery type=”rectangular”] https://en.support.wordpress.com/shortcodes/
  • 10.
  • 13. add_shortcode() Functions.php / includes / plugin Shortcode 1 <?php Résultat
  • 14. add_shortcode() Functions.php / includes / plugin Shortcode 1 2 3 4 5 6 7 8 <?php // Create the function for the shortcode function message_maker() { } ​?> Résultat
  • 15. add_shortcode() Functions.php / includes / plugin Shortcode 1 2 3 4 5 6 7 8 9 10 <?php // Create the function for the shortcode function message_maker() { return 'Hello WordCamp Mtl !'; } ​?> Résultat
  • 16. add_shortcode() Functions.php / includes / plugin Shortcode 1 2 3 4 5 6 7 8 9 10 11 12 13 <?php // Create the function for the shortcode function message_maker() { return 'Hello WordCamp Mtl !'; } // Register the Shortcode using the API add_shortcode('message' , 'message_maker' ); ​?> Résultat
  • 17. add_shortcode() Functions.php / includes / plugin Shortcode 1 2 3 4 5 6 7 8 9 10 11 12 13 <?php // Create the function for the shortcode function message_maker() { return 'Hello WordCamp Mtl !'; } // Register the Shortcode using the API add_shortcode('message' , 'message_maker' ); ​?> [message] Résultat
  • 18. add_shortcode() Functions.php / includes / plugin Shortcode 1 2 3 4 5 6 7 8 9 10 11 12 13 <?php // Create the function for the shortcode function message_maker() { return 'Hello WordCamp Mtl !'; } // Register the Shortcode using the API add_shortcode('message' , 'message_maker' ); ​?> [message] Résultat Hello WordCamp Mtl !
  • 19. Functions.php / includes / plugin Shortcode 1 2 3 4 5 6 7 8 9 10 11 12 13 <?php // Create the function for the shortcode function current_year() { return date('Y'); } // Register the Shortcode using the API add_shortcode('year' , 'current_year' ); ​?> Résultat
  • 20. Functions.php / includes / plugin Shortcode 1 2 3 4 5 6 7 8 9 10 11 12 13 <?php // Create the function for the shortcode function current_year() { return date('Y'); } // Register the Shortcode using the API add_shortcode('year' , 'current_year' ); ​?> [year] Résultat
  • 21. Functions.php / includes / plugin Shortcode 1 2 3 4 5 6 7 8 9 10 11 12 13 <?php // Create the function for the shortcode function current_year() { return date('Y'); } // Register the Shortcode using the API add_shortcode('year' , 'current_year' ); ​?> [year] Résultat 2015
  • 23. shortcode_atts() Functions.php / includes / plugin Shortcode 1 2 3 4 5 6 7 8 9 <?php // Create the shortcode function function lorem_ipsum() { } // Register the Shortcode using the API add_shortcode( 'lorem' , 'lorem_ipsum' ); ?> Résultat
  • 24. shortcode_atts() Functions.php / includes / plugin Shortcode 1 2 3 4 5 6 7 8 9 10 11 12 13 <?php // Create the shortcode function function lorem_ipsum( $atts ) { // Handling the Attributes $a = shortcode_atts( array( 'sentences' => 1 ), $atts ); } // Register the Shortcode using the API add_shortcode( 'lorem' , 'lorem_ipsum' ); ?> Résultat
  • 25. shortcode_atts() Functions.php / includes / plugin Shortcode 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 <?php // Create the shortcode function function lorem_ipsum( $atts ) { // Handling the Attributes $a = shortcode_atts( array( 'sentences' => 1 ), $atts ); // Get the Lorem Ipsum $lorem_ipsum = get_lorem_ipsum( $a['sentences'] ); return $lorem_ipsum; } // Register the Shortcode using the API add_shortcode( 'lorem' , 'lorem_ipsum' ); ?> Résultat
  • 26. shortcode_atts() Functions.php / includes / plugin Shortcode 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 <?php // Create the shortcode function function lorem_ipsum( $atts ) { // Handling the Attributes $a = shortcode_atts( array( 'sentences' => 1 ), $atts ); // Get the Lorem Ipsum $lorem_ipsum = get_lorem_ipsum( $a['sentences'] ); return $lorem_ipsum; } // Register the Shortcode using the API add_shortcode( 'lorem' , 'lorem_ipsum' ); ?> [lorem sentences=“2”] Résultat
  • 27. shortcode_atts() Functions.php / includes / plugin Shortcode 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 <?php // Create the shortcode function function lorem_ipsum( $atts ) { // Handling the Attributes $a = shortcode_atts( array( 'sentences' => 1 ), $atts ); // Get the Lorem Ipsum $lorem_ipsum = get_lorem_ipsum( $a['sentences'] ); return $lorem_ipsum; } // Register the Shortcode using the API add_shortcode( 'lorem' , 'lorem_ipsum' ); ?> [lorem sentences=“2”] Résultat Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliter enim explicari, quod quaeritur, non potest.
  • 28. shortcode_atts() Functions.php / includes / plugin Shortcode 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 <?php // Create the shortcode function function get_thumbnail( $atts ) { // Handling the Attributes $a = shortcode_atts( array( 'size' => 'thumbnail', 'class' => 'circle' ), $atts ); // Get the Thumbnail echo get_the_post_thumbnail( get_option('page_on_front'), $a['size'], array( 'class' => $a['class'] ) ); } // Register the Shortcode using the API add_shortcode( 'thumbnail' , 'get_thumbnail' ); ?> Résultat
  • 29. shortcode_atts() Functions.php / includes / plugin Shortcode 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 <?php // Create the shortcode function function get_thumbnail( $atts ) { // Handling the Attributes $a = shortcode_atts( array( 'size' => 'thumbnail', 'class' => 'circle' ), $atts ); // Get the Thumbnail echo get_the_post_thumbnail( get_option('page_on_front'), $a['size'], array( 'class' => $a['class'] ) ); } // Register the Shortcode using the API add_shortcode( 'thumbnail' , 'get_thumbnail' ); ?> [thumbnail size=”medium” class=“circle”] Résultat
  • 30. shortcode_atts() Functions.php / includes / plugin Shortcode 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 <?php // Create the shortcode function function get_thumbnail( $atts ) { // Handling the Attributes $a = shortcode_atts( array( 'size' => 'thumbnail', 'class' => 'circle' ), $atts ); // Get the Thumbnail return get_the_post_thumbnail( get_option('page_on_front'), $a['size'], array( 'class' => $a['class'] ) ); } // Register the Shortcode using the API add_shortcode( 'thumbnail' , 'get_thumbnail' ); ?> [thumbnail size=”medium” class=“circle”] Résultat
  • 32. $content Functions.php / includes / plugin Shortcode 1 2 3 4 5 6 7 8 9 10 11 <?php // Create the function for the shortcode function message_maker( $atts ) { } // Register the Shortcode using the API add_shortcode('message' , 'message_maker' ); ​?> Résultat
  • 33. $content Functions.php / includes / plugin Shortcode 1 2 3 4 5 6 7 8 9 10 11 12 13 14 <?php // Create the function for the shortcode function message_maker( $atts, $content = null ) { // Upper the $content return strtoupper($content); } // Register the Shortcode using the API add_shortcode('message' , 'message_maker' ); ​?> Résultat
  • 34. $content Functions.php / includes / plugin Shortcode 1 2 3 4 5 6 7 8 9 10 11 12 13 14 <?php // Create the function for the shortcode function message_maker( $atts, $content = null ) { // Upper the $content return strtoupper($content); } // Register the Shortcode using the API add_shortcode('message' , 'message_maker' ); ​?> [message] tout ce paragraphe devrait être en lettres capitales [/message] Résultat
  • 35. $content Functions.php / includes / plugin Shortcode 1 2 3 4 5 6 7 8 9 10 11 12 13 14 <?php // Create the function for the shortcode function message_maker( $atts, $content = null ) { // Upper the $content return strtoupper($content); } // Register the Shortcode using the API add_shortcode('message' , 'message_maker' ); ​?> [message] tout ce paragraphe devrait être en lettres capitales [/message] Résultat TOUT CE PARAGRAPHE DEVRAIT ÊTRE EN LETTRES CAPITALES
  • 36. do_shortcode() Functions.php / includes / plugin Shortcode 1 2 3 4 5 6 7 8 9 10 11 12 13 14 <?php // Create the function for the shortcode function message_maker( $atts, $content = null ) { // Upper the $content return strtoupper(do_shortcode($content)); } // Register the Shortcode using the API add_shortcode('message' , 'message_maker' ); ​?> Résultat
  • 37. do_shortcode() Functions.php / includes / plugin Shortcode 1 2 3 4 5 6 7 8 9 10 11 12 13 14 <?php // Create the function for the shortcode function message_maker( $atts, $content = null ) { // Upper the $content return strtoupper(do_shortcode($content)); } // Register the Shortcode using the API add_shortcode('message' , 'message_maker' ); ​?> [message] Hello WordCamp Montreal [year] ! [/message] Résultat
  • 38. do_shortcode() Functions.php / includes / plugin Shortcode 1 2 3 4 5 6 7 8 9 10 11 12 13 14 <?php // Create the function for the shortcode function message_maker( $atts, $content = null ) { // Upper the $content return strtoupper(do_shortcode($content)); } // Register the Shortcode using the API add_shortcode('message' , 'message_maker' ); ​?> [message] Hello WordCamp Montreal [year] ! [/message] Résultat HELLO WORDCAMP MONTREAL 2015 !
  • 40. ob_start() Functions.php / includes / plugin Shortcode 1 2 3 4 5 6 7 8 <?php // Create the shortcode function function message_maker() { } // Register the Shortcode using the API add_shortcode('message' , 'message_maker' ); ?> Résultat
  • 41. ob_start() Functions.php / includes / plugin Shortcode 1 2 3 4 5 6 7 8 9 10 11 <?php // Create the shortcode function function message_maker() { // Démarrage du buffer de sortie ob_start(); } // Register the Shortcode using the API add_shortcode('message' , 'message_maker' ); ?> Résultat
  • 42. ob_start() Functions.php / includes / plugin Shortcode 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 <?php // Create the shortcode function function message_maker() { // Démarrage du buffer de sortie ob_start(); ?> <ul> <li>Post title 1</li> <li>Post title 2</li> </ul> <?php } // Register the Shortcode using the API add_shortcode('message' , 'message_maker' ); ?> Résultat
  • 43. ob_start() Functions.php / includes / plugin Shortcode 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 <?php // Create the shortcode function function message_maker() { // Démarrage du buffer de sortie ob_start(); ?> <ul> <li>Post title 1</li> <li>Post title 2</li> </ul> <?php // Lecture du buffer et effacement return ob_get_clean(); } Résultat
  • 44. ob_start() Functions.php / includes / plugin Shortcode 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 <?php // Create the shortcode function function message_maker() { // Démarrage du buffer de sortie ob_start(); ?> <ul> <li>Post title 1</li> <li>Post title 2</li> </ul> <?php // Lecture du buffer et effacement return ob_get_clean(); } [message] Résultat
  • 45. ob_start() Functions.php / includes / plugin Shortcode 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 <?php // Create the shortcode function function message_maker() { // Démarrage du buffer de sortie ob_start(); ?> <ul> <li>Post title 1</li> <li>Post title 2</li> </ul> <?php // Lecture du buffer et effacement return ob_get_clean(); } [message] Résultat • Post Title 1 • Post Title 2
  • 46. Functions.php / includes / plugin Shortcode 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 function show_posts( $atts, $content = null ) { // Handling the Attributes $a = shortcode_atts( array( 'type' => 'projects' ), $atts ); // Get the posts filtered by Post Type $query = new WP_Query( array( 'post_type' => $a['type'] ) ); ob_start(); echo '<h1>'.do_shortcode($content).'</h1>'; echo '<ul>‘; // The WP Query Loop while ( $query->have_posts() ) { $query->the_post(); ?> <!-- Build the HTML --> <li><?php the_title(); ?></li> </ul> <?php } echo '</ul>‘; // Restore the Global $post variable - Avoid WP Query inception conflict wp_reset_postdata(); return ob_get_clean(); } add_shortcode('posts' , 'show_posts' ); [posts type=“projects”] Mes derniers projets : [/posts] Résultat Mes derniers projets : • Project Title 1 • Project Title 2 • Project Title 3
  • 47. http://gndev.info/shortcodes-ultimate/ Shortcodes Ultimate for WordPress Including a custom shortcode maker API / Addon https://github.com/gndev/shortcodes-ultimate https://codex.wordpress.org/Shortcode_API Wordpress Codex – Shortcode API Montreal WordCamp 2015 | @MichaelBontyes https://github.com/michaelbontyes/Presentation-Create-Your-Own-Shortcode Presentation “How to create your own shortcode”

Editor's Notes

  1. 5’ Introduction : - Remerciements – 1ere fois – team - On va parler des shortcodes en 3 parties : intro, code, q-a -> question voisins si pas voisin stopper moi! Sondage MAIS premiere question , c’est quoi un SHORTCODE???
  2. Exemple
  3. Exemple
  4. page / post / custom post type widget
  5. ******* 5’ ******* TOUT gratuit ou open source!
  6. ******* 10’ ******* Gratuit ! Pas de plugin en plus !
  7. Type et caracteristiques des SHORTCODES
  8. Function principale en VERT Dans FUNCTIONS, INC ou PLUGIN PRECEDURALE OU EN ORIENTE OBJET
  9. ON ENREGISTRE LE SHORTCODE
  10. Shortcode dans la page de la homepage par exemple
  11. Exemple du copyright ou signature en bas de page
  12. ******* 15’ *******
  13. Example du lorem ipsum pour l’integrateur et son CSS
  14. Get_lorem autre function utilisant l’API lorem ipsum
  15. Et si je dois prendre en compte tout un paragraphes ou plusieurs??
  16. Mise en capitales
  17. Comme des balises HTML – CLOSING TAG
  18. ET si on compliquait un peu??? Exemple de texte en capital avec la date ou une signature a la fin Ou une image de homepage
  19. ******* 20’ *******
  20. example de la LOOP Attention au wp_reset_query
  21. Buffer / tampon / temporisateur de sortie Pas specifique a API shortcode wordpress! Mais utile!
  22. Type de posts Un titre Du HTML Reset post data