WP API Posts Plugin

This is a little plugin I wrote while working with Jon Becker to build out the Ed Leadership Hub site.

Essentially, we wanted students to be able to fill out a quick form and build out a profile page. As is my my wont, I went the Gravity Forms route. They could give a short bio, twitter account, and their portfolio URL. We’ll be tying in the the posts via Feed WordPress but it seemed like more hassle than it was worth to map the author id to this page. Pretty fun to be able to build out a solution on-the-fly and since all students were going to be in rampages I didn’t have to worry about WP not being upgraded or running from a non-HTTPS server.

This chunk of the plugin builds out the HTML from the shortcode. You can see the data-url, data-num elements being populated. There’s a few other things in there I haven’t yet activated.

function altlab_getpost_shortcode( $atts, $content = null ) {
    extract(shortcode_atts( array(
         'url' => '', //author id - sep multiple w commas       
         'display' => '', //defaults to list but grid with thumbnail featured images    
         'number' => ''   
    ), $atts));         
    if($url){
        $url = 'data-url="'.$url.'"';
    }    
    if($number){
        $num = 'data-num="'.$number.'"';
    } else {
        $num = 'data-num="10"';
    }
     if($display){
        $num = 'data-display="'.$display.'"';
    } else {
        $display = 'data-display="list"';
    }
    //$html = '<ul id="altlab-getposts" class="container" ' . $url . ' ' .  $num . ' ' . $display .'></ul>';
    $html = '<ul id="altlab-getposts" class="container" ' . $url . ' ' . $num . '></ul>';
    return  $html;
}
add_shortcode( 'get-posts', 'altlab_getpost_shortcode' );

Using data elements is the easiest way I’ve seen to pass variables to the javascript . . . the javascript looks for the altlab-getsposts id and then pulls the data elements from it. Once again, there’s some extra stuff in there for down the road that I haven’t implemented here.

function getResourceRestrictions(){
    var element = document.getElementById('altlab-getposts'); 
    if(element.dataset.cats){
      var cats = '&categories='+element.dataset.cats;
    } else {
      cats = "";
    }
    if(element.dataset.authors){
      var authors = '&author='+element.dataset.authors;
    }else {
      authors = "";
    }
    if(element.dataset.num){
      var num = '&per_page='+element.dataset.num;
    } else {
      num = "&per_page=10";
    }
    return cats + authors + num;
}  

Comments on this post

  1. Alan Levine (@cogdog) said on August 6, 2017 at 10:57 pm

    I have seen that Fake person dude somewhere before.

    How are you embedding your code in the posts? Are they Github gists? Asking for a frie— myself.

  2. Tom Woodward said on August 7, 2017 at 7:32 am

    Nothing so fancy (although I did pursue that at one point), this is just syntax highlighter evolved which lets me do various shortcodes for various languages. [php][javascript] etc.

Leave a Reply

Trackbacks and Pingbacks on this post

No trackbacks.

TrackBack URL