su_shortcode_meta

The shortcodes-ultimate su shortcode meta function.

Published Date - February 15, 2021

Description

su_shortcode_meta( $atts, $content );

Parameters

  1. $atts

  2. $content


Usage

The following example is for adding a hook callback.

if ( !function_exists( 'su_shortcode_meta' ) ) {
    require_once ABSPATH . PLUGINDIR . 'shortcodes-ultimateshortcodes-ultimate/includes/shortcodes/meta.php';
}

// The input(s). 
$atts = null;
$content = null;

// NOTICE! Understand what this does before running.
$result = su_shortcode_meta( $atts, $content );
            

Defined

The function is defined in the following location(s).

shortcodes-ultimate/includes/shortcodes/meta.php

function su_shortcode_meta( $atts = null, $content = null ) {
	$atts = shortcode_atts( array(
			'key'     => '',
			'default' => '',
			'before'  => '',
			'after'   => '',
			'post_id' => '',
			'filter'  => ''
		), $atts, 'meta' );
	// Define current post ID
	if ( !$atts['post_id'] ) $atts['post_id'] = get_the_ID();
	// Check post ID
	if ( !is_numeric( $atts['post_id'] ) || $atts['post_id'] < 1 ) return sprintf( '<p class="su-error">Meta: %s</p>', __( 'post ID is incorrect', 'shortcodes-ultimate' ) );
	// Check key name
	if ( !$atts['key'] ) return sprintf( '<p class="su-error">Meta: %s</p>', __( 'please specify meta key name', 'shortcodes-ultimate' ) );
	// Get the meta
	$meta = get_post_meta( $atts['post_id'], $atts['key'], true );
	// Set default value if meta is empty
	if ( !$meta ) $meta = $atts['default'];
	// Apply cutom filter
	if (
		$atts['filter'] &&
		su_is_filter_safe( $atts['filter'] ) &&
		function_exists( $atts['filter'] )
	) {
		$meta = call_user_func( $atts['filter'], $meta );
	}
	// Return result
	return ( $meta ) ? $atts['before'] . $meta . $atts['after'] : '';
}