Add a Custom Shortcode for the WordPress Plug-in

If you want to add specific content from the program listings plug-in, but a shortcode does not exist, create one!

You add your own shortcode that hooks directly into our listing plugin, so you can create your own highly customized displays.

This will need to be done by your WordPress web developer, but here is a working sample of creating a custom shortcode.

This shortcode will list only the program details for a single program, using that program's ID. This method can be used to build custom HTML around program content to allow for full customization of the layout.

Here's an example that shows the addition of a custom shortcode named my_custom_program_shortcode that specifies Program ID 61.

This is what the viewer will see when visiting that page.

Here's the code that produces the sample above. Add this code to your functions.php file. You can use a WordPress plug-in like My Custom Functions to add your custom PHP code to your WordPress website.

// set up the shortcode hook
add_shortcode( 'my_custom_program_shortcode', 'my_custom_program_shortcode_handler' );

function my_custom_program_shortcode_handler($atts, $content = null) {
    $output = '';
    // make sure calling the api doesn't cause a fatal error if the RS_Connect_Api class is not found
    $rs_connect_api = class_exists('RS_Connect_Api') ? new RS_Connect_Api() : null;
    // setup the default attribute you can pass into the shortcode
    $atts_defaults = array (
        'program_id' => 0,
        // etc...
    );
    $args = shortcode_atts($atts_defaults, $atts);
    $program_id = $args['program_id'];

    // make sure we have the $rs_connect_api class and a program ID
    if (! empty($rs_connect_api) && $program_id) {
        // get the program data from the api
        $program = $rs_connect_api::get_program($program_id);
        // set up variables with fallbacks for all the program data.
        // these are the variables we need to recreate the program details section of the program listings page
        $program_title = ! empty($program->title) ? $program->title : '';
        $date_title = ! empty($program->date_title) ? $program->date_title : 'Date & Time Details:';
        $location_title = ! empty($program->location_title) ? $program->location_title : 'Location:';
        $address_title = ! empty($program->address_title) ? $program->address_title : 'Address:';
        $photo_url = ! empty($program->photo_details->large->url) ? $program->photo_details->large->url : '';
        $early_bird_discount =! empty($program->early_bird_discount) ? $program->early_bird_discount : '';
        $price_details = ! empty($program->price_details) ? $program->price_details : '';
        $date_time = ! empty($program->date_time) ? $program->date_time : '';
        $location = ! empty($program->location) ? $program->location : '';
        $address = ! empty($program->address) ? $program->address : '';
        $contact = ! empty($program->contact) ? $program->contact : '';
        $custom = ! empty($program->custom) ? $program->custom : '';
        $email = ! empty($program->email) ? $program->email : '';
        $registration_action = ! empty($program->registration_action) ? $program->registration_action : '';

        // start output buffering
        ob_start();

        ?>

        <div class="entry-content">
            <div class="rs-program-meta">
                <?php if ($photo_url) : ?>
                <div class="rs-program-photo">
                    <img src="<?php echo $photo_url; ?>">
                </div>
            <?php endif; ?>

            <div class="rs-meta-content-container">

                <?php if ($early_bird_discount && empty($hide_discount)) : ?>
                    <div class="rs-program-early-bird-discount rs-highlight"><?php echo $early_bird_discount; ?></div>
                <?php endif; ?>

                <?php // Pricing ?>
                <?php if ($price_details) : ?>
                    <div class="rs-program-price"><?php echo $price_details ?></div>
                <?php endif; ?>

                <?php // Datetime details ?>
                <?php if ($date_time) : ?>
                    <p class="rs-program-datetime"><span class="rs-program-label"><?php echo $date_title; ?></span> <?php echo $date_time; ?></p>
                <?php endif; ?>

                <?php // Location ?>
                <?php if ($location) : ?>
                    <p class="rs-program-location"><span class="rs-program-label"><?php echo $location_title; ?></span> <?php echo $location; ?></p>
                <?php endif; ?>

                <?php // Address ?>
                <?php if ($address) : ?>
                    <p class="rs-program-address"><span class="rs-program-label"><?php echo $address_title; ?></span> <?php echo $address; ?></p>
                <?php endif; ?>

                <?php // Contact details ?>
                <?php if ($contact) : ?>
                    <p class="rs-program-contact"><?php echo $contact ?></p>
                <?php endif; ?>

                <?php // Custom fields ?>
                <?php if ($custom) : ?>
                    <div class="rs-program-custom-wrap"><?php echo $custom; ?></div>
                <?php endif; ?>

                <?php if ($email && empty($options['rs_template']['hide_contact_button'])) : ?>
                    <?php if (! empty($options['rs_template']['contact_button_text'])) {
                        $contact_button_text = $options['rs_template']['contact_button_text'];
                    } else {
                        $contact_button_text = 'Email us about program';
                    } ?>
                    <a href="mailto:<?php echo $email; ?>?subject=An inquiry about <?php echo $program_title; ?>" class="rs-button"><?php echo $contact_button_text; ?></a>
                <?php endif; ?>

                <div class="rs-regsitration-wrap"><?php echo $registration_action; ?></div>
            </div>
        </div>

        <?php

        // get the output buffering as a variable
        $output = ob_get_clean();
    }

    // return the output as the content for the shortcode
    return $output;
}

 


rg-favicon-32 We'd love to hear from you. Did you find this article helpful? Are you aware of any additional information we should add? Would you like to suggest a topic for another article? If so, please contact your friendly support guru.