This thread is over two years old and may be outdated. Please create a new thread if you need help, or email us if you have an active Premium license.

Custom Post Type Builder Plugin

8 years ago · Last reply by Alex S 8 years ago

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",'
',$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."
".'<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.

Need fast email support? Get SiteOrigin Premium

Replies

1
  1. Alex S Staff 8 years, 4 months ago

    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.

Have a different question or issue?

Start New Thread