Last active
March 22, 2018 12:08
-
-
Save aaronsummers/c8ccfd4b457790c8295bf72977e9b802 to your computer and use it in GitHub Desktop.
Creating a shortcode with html tags and meta content
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function terms_shortcode($atts) { | |
ob_start(); | |
$atts = shortcode_atts( | |
array( | |
'page_id' => '', | |
'prefix' => '', | |
), $atts | |
); | |
return '<a href="' . get_the_permalink( $atts['page_id'] ) . '">'. $atts['prefix'] .' Terms & Conditions</a>'; | |
} | |
add_shortcode('terms', 'terms_shortcode'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Build a shortcode from the metabox.io metaboxes | |
function metabox_shortcode() { | |
ob_start(); | |
$post_id = 18; | |
global $prefix; | |
$account_page_student_title = rwmb_meta("{$prefix}account_page_student_title", OBJECT, $post_id); | |
$account_page_student_desc = rwmb_meta("{$prefix}account_page_student_desc", OBJECT, $post_id); | |
?> | |
<h2><?php echo $account_page_student_title; ?></h2> | |
<div><?php echo $account_page_student_desc; ?></div> | |
<?php | |
$account_page_student = ob_get_clean(); | |
return $account_page_student; | |
} | |
add_shortcode('student_info', 'metabox_shortcode'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The main point here is to use
ob_start()
andob_get_clean()
to output the HTML.We could use the
global $post; $post_id = $post->ID;
to get the id from the current page, rather then grabbing content from a specific page by an absolute id. ( obviously this only works if we're using the shortcode on the same page as the custom field. )