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
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
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
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???
Exemple
Exemple
page / post / custom post type
widget
******* 5’ *******
TOUT gratuit ou open source!
******* 10’ *******
Gratuit ! Pas de plugin en plus !
Type et caracteristiques des SHORTCODES
Function principale en VERT
Dans FUNCTIONS, INC ou PLUGIN
PRECEDURALE OU EN ORIENTE OBJET
ON ENREGISTRE LE SHORTCODE
Shortcode dans la page de la homepage par exemple
Exemple du copyright ou signature en bas de page
******* 15’ *******
Example du lorem ipsum pour l’integrateur et son CSS
Get_lorem autre function utilisant l’API lorem ipsum
Et si je dois prendre en compte tout un paragraphes ou plusieurs??
Mise en capitales
Comme des balises HTML – CLOSING TAG
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
******* 20’ *******
example de la LOOP
Attention au wp_reset_query
Buffer / tampon / temporisateur de sortie
Pas specifique a API shortcode wordpress! Mais utile!