Skip to content

Instantly share code, notes, and snippets.

@tripflex
Created October 24, 2017 00:50
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 tripflex/b439499dfbb10624eea32761f63ee7d8 to your computer and use it in GitHub Desktop.
Save tripflex/b439499dfbb10624eea32761f63ee7d8 to your computer and use it in GitHub Desktop.
Custom shortcode generated using dynamic value from listing in WP Job Manager Field Editor (example is for AccessPress Twitter Feed Pro)
<?php
add_shortcode( 'wpjmfe_ap_twitter_feed', 'smyles_custom_ap_twitter_feed' );
function smyles_custom_ap_twitter_feed( $atts ) {
// Empty output by default
$output = '';
// This should be the actual shortcode to call
$actual_shortcode = 'ap-twitter-feed-pro';
// Get "dynamic" value to use in shortcode from listing
$twitter = get_custom_field( 'company_twitter' );
// This should be an array of attributes to use when building the shortcode
$attributes = array(
'template' => 'template-7',
'username' => $twitter,
'total_feeds' => '3',
'follow_button' => 'true'
);
// We only want to output if there is a value for the dynamic part
if ( ! empty( $twitter ) ) {
// First open our new shortcode
$generated_shortcode = '[' . $actual_shortcode;
// Next add all attributes to it
foreach( $attributes as $attribute => $attribute_value ){
$generated_shortcode .= " {$attribute}='{$attribute_value}'";
}
// Close generated shortcode
$generated_shortcode .= ']';
// generate content with do_shortcode()
$output = do_shortcode( $generated_shortcode );
}
return $output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment