What is shortcode?
How to create a shortcode
//Initialize function for shortcode
function create_shortcode() {
echo "Hello
World!";
}
// Create a shortcode named [test_shortcode] and execute the code
from the create_shortcode function
Add_shortcode ('test_shortcode',
'create_shortcode');
|
function create_shortcode_randompost() {
$random_query = new WP_Query(array(
'posts_per_page' => 10,
'orderby' => 'rand'
));
ob_start();
if ( $random_query->have_posts() ) :
"<ol>";
while ( $random_query->have_posts() ) :
$random_query->the_post();?>
<li><a href="<?php the_permalink(); ?>"><h5><?php the_title(); ?></h5></a></li>
<?php endwhile;
"</ol>";
endif;
$list_post = ob_get_contents(); //
Take the entire contents above the $
list_post variable to return
ob_end_clean();
return $list_post;
}
add_shortcode('random_post', 'create_shortcode_randompost');
|
|
ob_end_clean()
. Actually here I have to cache
anything I’ll wrap that part to use the function ob_get_contents() Go to the $ list_post variable, then
return this variable out because when you do shortcode you must use return. If
anyone has a better way, let me know.Create a shortcode using the parameter
function create_shortcode_ parameters1
($args, $content) {
return " this is number". $args[' parameters1'];
}
add_shortcode( 'shortcode_ parameters1',
'create_shortcode_ parameters1' );
|
function create_shortcode_tinhtong($args,
$content) {
$tong = $args['term1'] +
$args['term2'];
return "Total".$tong;
}
add_shortcode( 'total', 'create_shortcode_total' );
|
function create_shortcode_content($args,
$content) {
return strtoupper($content);
//Prints all the content in the shortcode }
add_shortcode('shortcode_content',
'create_shortcode_content');
|
How to write a shortcode into a PHP file
Examples of shortcode examples
Shortcode displays video from Youtube
01
|
<iframe src="//www.youtube.com/embed/0KJ60uJZ3-Q" height="480" width="640" allowfullscreen="" frameborder="0"></iframe>
|
function create_youtube_shortcode(
$args, $content ) {
$content = '<iframe
src="//www.youtube.com/embed/'.$args['id'].'" height="
'.$args['height'].'" width="'.$args['width'].'"
allowfullscreen="" frameborder="0"></iframe>';
return $content;
}
add_shortcode('youtube', 'create_youtube_shortcode');
|
Insert a full color notification box
function create_notification _shortcode($args,
$content) {
return "
<div class='notification '>".$content."</div>
";
}
add_shortcode( 'notification ', 'create_notification
_shortcode' );
|
.thongbao {
background: #585858;
padding: 1.5em 2em;
color: #FFF;
border: 1px solid #C7C7C7;
}
|
Shortcode retrieves Facebook information
function create_fbgraph_shortcode($args,
$content) {
$get_info =
wp_remote_get('https://graph.facebook.com/'.$args['username']);
$get_avatar =
"https://graph.facebook.com/".$args['username']."/picture?type=large";
$dc_info =
json_decode($get_info['body'], true);
$dc_avatar =
json_decode($get_avatar['data'], true);
// Make variables easy to handle.
$fb_id = $dc_info['id'];
$fb_username =
$dc_info['username'];
$fb_url = $dc_info['link'];
$fb_name =
$dc_info['first_name'];
// Write gender.
if ($dc_info['gender'] ==
'male') {
$fb_gender = "male";
} else if ($dc_info['gender'] == female) {
$fb_gender = "female";
} else {
$fb_gender = " Undefined!";
}
ob_start();?>
<div class="fb-info">
<h5>your information <?php echo $fb_name; ?></h5>
<div class="avatar"><img
alt="" src="<?php echo $get_avatar;
?>" /></div>
<div
class="info"><strong>your ID: </strong> <?php
echo $fb_id; ?>
<strong>Username: </strong> <?php echo $fb_username; ?>
<strong>sex: </strong> <?php echo $fb_gender; ?></div>
</div>
<?php
$result = ob_get_contents();
ob_end_clean();
return $result;
}
add_shortcode( 'fbgraph',
'create_fbgraph_shortcode' );
|
.fb-info {
overflow: hidden;
padding: 0.5em 1em;
background: #3B5998;
border: 1px solid #E8E8E8;
color: #FFF;
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
border-radius: 10px;
}
.fb-info .avatar {
width: 30%;
float: left;
margin-right: 5%;
padding: 10px;
background: #FFF;
border: 1px solid #F3F3F3;
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
border-radius: 10px;
}
.fb-info .info {
width: 60%;
float: right;
}
|