Template functions every MD theme developer should know
The hidden power of Marketers Delight comes with its rich development tools, and there’s no better way to dive in than with template functions. Here’s the best you need to know:
1. Get admin settings data
The custom options MD stores from the admin screen can be found in the md_setting()
function. To access option keys, follow the inputs below.
md_setting( $keys, $default );
- $keys (string|array) Optional. Returns full options array, or specified option key
- $default (string|bool|array) Optional. If no option found, return manual default value
Examples
Get primary color in MD > Site Design > Colors and set default color if option not set.
md_setting( array( ‘colors’, ‘site’, ‘primary’ ), ‘#000’ );
Get entire options group of MD share settings only.
md_setting( ‘share’ );
Get list of integrations data with variables.
$option = md_setting(); print_r( $option[‘integrations’][‘services’] );
2. Get post meta
You can retrieve and use the post meta data saved to any given post across post types that have MD settings added to the editor. This function usually finds the post ID on its own, and you can set a custom post ID if needed.
md_post_meta( $keys, $id, $default );
- $keys (string|array) Optional. Returns full options array, or specified option key
- $id (string|int) Optional. Post ID of any single post/page
- $default (string|bool|array) Optional. If no option found, return manual default value
Examples
Return all MD post meta of current page.
md_post_meta();
Gets post meta for any custom Floating Bars added to this page only.
md_post_meta( array( ‘optins’, ‘floating_bars’ ) );
Gets post meta specified by post ID.
md_post_meta( null, 907 );
3. Get term meta
Similar to post meta, except for getting terms data from categories and custom taxonomies. This function usually finds the term ID on its own, and you can set a custom post ID if needed.
md_term_meta( $keys, $id, $default );
- $keys (string|array) Optional. Returns full options array, or specified option key
- $id (string|int) Optional. Post ID of any single post/page
- $default (string|bool|array) Optional. If no option found, return manual default value
Examples
Return all MD term meta of current page.
md_term_meta();
Gets post meta for any custom Floating Bars added to this page only.
md_term_meta( array( ‘optins’, ‘floating_bars’ ) );
Gets post meta specified by post ID.
md_term_meta( null, 907 );
4. Get post meta or term meta
When the same options are used across post and term meta it can be useful to search the current template for either post meta to exist, only where appropriate.
md_meta( $keys, $id, $default );
Uses the same parameters as md_post_meta
and md_term_meta
, so apply parameters consistently.
5. MD conditional functions
Similar to WordPress conditional tags, MD comes with some time-saving helper functions that let you dig into the same precise layout controls in your own custom templates.
Check if has site logo
if ( md_has_logo() ) { echo "yes, logo is uploaded'; }
Check if page has sidebar
if ( md_has_sidebar() ) { add_action( 'md_hook_after_sidebar', 'my_custom_function_name' ); }
Check if page has headline
if ( md_has_headline() ) { add_action( 'md_hook_byline' 'my_custom_function_name' ); }
See full list in /marketers-delight/lib/functions/conditionals.php
6. Generate style tags
When combining color design options to style an element, it is useful to have a function that plugs in user’s custom color options and generates an inline style tag with background color, image, text color, and other strict styles.
md_style( $fields );
- $fields (array) Required. Returns full options array, or specified option key
Examples
Create style tag for custom post meta color options.
$bg_color = md_setting( array( 'my_custom_option', 'bg_color' ), '#000' ); $text_color = md_setting( array( 'my_custom_option', 'text_color' ), '#FFF' ); $style = md_style( array( 'bg_color' => $bg_color, 'text_color' => $text_color 'border' => array( 1, 'solid', '#00ffff' ) ) );
<div class="my-div"<?php echo $style; ?>> <p>Custom content here.</p> </div>
Once added to an HTML tag, $style will output to style="background-color: #000;color:FFF;"
Parameters
- bg_color => Add a background color code
- bg_image => Set a background image URL to element
- bg_size => Set
background-size
CSS to cover or other value - border (array) => Set border width, style, and color
- color => Set text color
- width => Set width in pixels (do not include px after number)
- width_unit => Change unit from pixels to em, %, rem, or other measurement
- max_width => Instead of setting a hard width, set
max-width
- height => Set height of element in pixels
6. Render font icons
As of MD5.2.3 you can use the md_icon
function to show font icons in templates. By default this function returns the icon template HTML and be customized to only return the class name.
The main reason for this function is to check if the current icon name exists in your MD icon library. If no icon found, this function will not render.
md_icon( $icon, $args );
- $icon (string) Required. Name of icon without
md-icon-
prefix - $args (array|bool) Optional. Set as array to use custom arguments. Simply return as true to render the class name of the icon only
Examples
Show book icon. This returns <i class="md-icon-book"></i>
md_icon( 'book' );
Return icon class name only, as in md-icon-book
md_icon( 'book', true );
Return icon markup with custom classes
md_icon( 'book', array( 'classes' => 'custom-icon-class' ) );
7. Rebuild the MD stylesheet
Each time you save MD settings or update to a new version, the MD stylesheet is dynamically rebuilt into the static style.css
file in the theme’s root directory.
To rebuild the MD stylesheet manually on your own actions use:
md_compile_css();
You should not let this function run on every page load and only use it for a one-time page refresh before removing it from your child theme. Works best to tie to a user action.
You can also run this by adding ?md=compile_css
to the end of the current URL (only admins can execute this) .