Skip to content

Instantly share code, notes, and snippets.

@aaronsummers
Last active March 22, 2018 12:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaronsummers/c8ccfd4b457790c8295bf72977e9b802 to your computer and use it in GitHub Desktop.
Save aaronsummers/c8ccfd4b457790c8295bf72977e9b802 to your computer and use it in GitHub Desktop.
Creating a shortcode with html tags and meta content
<?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 &amp; Conditions</a>';
}
add_shortcode('terms', 'terms_shortcode');
<?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');
@aaronsummers
Copy link
Author

aaronsummers commented Feb 8, 2018

The main point here is to use ob_start() and ob_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. )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment