Skip to content

Instantly share code, notes, and snippets.

@alanef
Created June 16, 2023 21:23
Show Gist options
  • Save alanef/87300ec1f8c46a040dee87e7faa44620 to your computer and use it in GitHub Desktop.
Save alanef/87300ec1f8c46a040dee87e7faa44620 to your computer and use it in GitHub Desktop.
Get custom field shortcode
<?php
/*
* Plugin Name: Get Custom Field Shortcode
* Description: Display custom fields in your WordPress site
* Version: 1.0
*/
/**
* Get Custom Field Shortcode
*
* @param array $atts
*
* @return string
*
* usage: [get_custom_field custom_field="my_custom_field"]
*/
add_shortcode( 'get_custom_field', function( $atts) {
$atts = shortcode_atts( array(
'custom_ield' => '',
), $atts, 'get_custom_field' );
$custom_field = $atts['custom_field'];
$custom_field_value = get_post_meta( get_the_ID(), $custom_field, true );
return wp_kses_post($custom_field_value);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment