Home>Support>Custom Post Type Builder Plugin

Custom Post Type Builder Plugin

Notice: This thread is over two years old; the information may be outdated. Please consider creating a new thread if you require free support. If you have an active SiteOrigin Premium license, you can email our premium support desk at [email protected].

This is not really a question, I wanted to share my solution with others. If you know a better way to do this please comment. I built a custom post type for our locations. I then was entering the location information in each post. It was going to be a tedious process because I wanted to have an email button, a clickable phone, and an address link to Google maps. What I did was leverage the custom fields to store these values. Then I created a simple shortcode plugin that would access the custom fields and format the information as links and buttons. This makes it simple for anyone to add or change information because they don’t have to mess with links. The other advantage is I can make changes to the layout in the post type then restore each post and it is all good.

Here is my shortcode


function msc_shortcodes_init() {
    
    add_shortcode('msc_dealer','msc_dealer_func');
    
}

function msc_dealer_func($attr) {
    
    $task = $attr['task'];
    
    $post = get_post();
    
    $pID = $post->ID;
    
    $pURL =  get_permalink(661); //661 is the id of the contact form_option
    
    if($task == 'contact-link') {
        
        
       return '<a class="msc-email-button" href="'.$pURL.'?mscpid='.$pID.'">Email Us</a>';
        
    }
    
    if($task == 'email') {
        
        if(isset($_GET['mscpid']) && !empty($_GET['mscpid'])) {
    
            $pID = $_GET['mscpid'];

            $email = get_post_meta($pID,'msc-email');

            $pURL =  $email[0];

            return $pURL;
        }
        else {
            return get_option('admin_email');
        } 

    }
    if($task == 'location') {
        
        if(isset($_GET['mscpid']) && !empty($_GET['mscpid'])) {
        
            $pID = $_GET['mscpid'];

            $location = get_the_excerpt($pID);

            return 'You are contacting the '.$location.' location.';
        }
        else {

            return '<h4><i>You are contacting the .... location.</i></h4>';

        }

    }
    if($task == 'address') {
    
        $address = get_post_meta($pID,'msc-address');
        
        if(empty($address)) {
            
            return false;
        }
        else {
            $address = $address[0];

            $addressStr = str_replace(' ','+',$address);

            $addressStr = str_replace("nr",'+',$address);

            $address = str_replace("n",'<br>',$address);

            $map_address = '<a href="https://www.google.com/maps/place/'.$addressStr.'" target="_blank">'.$address.'</a>';

            return $map_address;
        }

    }
    if($task == 'phone1') {
        
        $phone1 = get_post_meta($pID,'msc-phone-1');
        
        if(empty($phone1[0])) {
            return false;
        }
        else {
            return '<a href="tel:'.$phone1[0].'">'.$phone1[0].'</a>';
        }
    }
    if($task == 'phone2') {
        
        $phone2 = get_post_meta($pID,'msc-phone-2');
        
        if(empty($phone2[0])) {
            return false;
        }
        else {
            return '<a href="tel:'.$phone2[0].'">'.$phone2[0].'</a>';
        }
    }
    if($task == 'location-name') {
        
        $location = get_the_excerpt($pID);
        
        return $location;
        
    }
    if($task == 'hours') {
        
       $hours = get_post_meta($pID,'msc-hours');
        
       if(empty($hours)) {
           return false;
       }
       else {
           
           $hours = explode("n",$hours[0]);
           
           $firstline = str_replace('[','</b>[',$hours[0]);
           
           $secondline = str_replace('[','</b>[',$hours[1]);
           
           return '<b>'.$firstline."<br>".'<b>'.$secondline;
           
       }
        
        
    }
    
}

add_action('init', 'msc_shortcodes_init');

I can then just place a shortcode [msc_dealer task=”address”] in the spot where I want. Once this shortcode is placed in the custom post type it never needs to be touched. A person just has to select the custom field for address and enter it and the page is updated. Hope this makes sense and is a help.

This is our free support forum. Replies can take several days. If you need fast email support, please purchase a SiteOrigin Premium license.

  1. 7 years, 9 months ago Alex S
    Hi, I Work Here

    Hi Mark,

    Cool stuff! :)

    Maybe consider using the SiteOrigin Widgets Bundle Framework for your next project as it’ll give you a nice interface for your clients. :)

Replies on this thread are closed. Please create a new thread if you have a question, or purchase a SiteOrigin Premium license if you need one-on-one email support.

Get The Most Out of SiteOrigin with SiteOrigin Premium

Find Out More