Skip to content

Instantly share code, notes, and snippets.

@pepebe
Last active August 12, 2023 08:56
Show Gist options
  • Save pepebe/1f43ee42444618fa63a96708cc523251 to your computer and use it in GitHub Desktop.
Save pepebe/1f43ee42444618fa63a96708cc523251 to your computer and use it in GitHub Desktop.
Wordpress shortcode: Render all posts by a certain user

shortcode

[wsrn_autorPosts user="11"]

Note: user must contain a valid userid

Note:

html is rendered by a small function that replaces a placeholder inside swirly brackets with a value from an array.

Values

$props = array(
  'title' => "Brave new world"
);

Template

$tpl = "<h1>{{title}}</h1>";

Function call

$html = renderHTML($tpl, $props);

Result

<h1>Brave new world</h1>

to-dos:

  • add sanity check for the userid
  • finish debugging placeholders
  • add pagination (limit, offset, logic)

nice-to-haves

  • steal output filters from modx
<?php
function renderHTML($tpl, $props){
$chunk = $tpl;
foreach($props as $key => $value){
$chunk = str_replace("{{".$key."}}",$value, $chunk);
}
return $chunk;
}
function wsrn_autorPosts(){
/*
[wsrn_autorPosts user="11"]
*/
$time_start = microtime(true);
$defaults = array(
'user' => 11
,'debug' => false
);
$props = shortcode_atts($defaults, $atts);
//$props['debug'] = true;
$msg = array(
'shortcode' => array(
'name' => 'wsrn_autorenHeader'
,'execution_time' => ''
,'props' => $props
)
,'response' => array()
);
$user = $props['user'];
$debug = $props['debug'];
$args = array(
'author' => $user,
'orderby' => 'post_date',
'order' => 'ASC',
'posts_per_page' => -1 // no limit
);
$posts = get_posts( $args );
$total = count($posts);
// Vorlagen definieren.
//Alles was ersetzt werden soll in doppelten Klammern markieren.
//Bsp. {{title}} oder {{date}}
$output = '';
foreach($posts as $post){
$postArr = json_decode(json_encode($post),true);
unset($postArr['post_content']);
$postId = $postArr['ID'];
/*
* render categories
**/
$tplCategory = '
<li class="meta-categories" data-type="simple" data-idx="{{i}}">
<a href="{{url}}" rel="tag" alt="{{alt}}" class="ct-term-{{id}}">
{{text}}
</a>
</li>
';
$categories = get_the_category($postId);
$catlist = '';
if ( ! empty( $categories ) ) {
$i = 0;
$categorynames = array();
foreach( $categories as $category ) {
$i++;
$categorynames[] = $category->slug;
$data = array(
'i' => $i
,'id' => $category->term_id
,'url' => esc_url( get_category_link( $category->term_id ) )
,'alt' => esc_attr( sprintf( __( 'View all posts in %s', 'textdomain' ), $category->name ) )
,'text' => esc_html( $category->name )
);
$catlist .= renderHTML($tplCategory, $data);
}
}
/*
* Render articles
* tpl: $tplArticle
**/
$tplArticle = '
<article
id="post-{{id}}"
class="post-{{id}} entry-card post type-post status-publish format-standard has-post-thumbnail hentry {{categorynames}}">
<a class="ct-image-container boundless-image"
href="{{permalink}}"
aria-label="{{title}}"
tabindex="-1"
>
{{thumbnail}}
</a>
<ul class="entry-meta" data-type="simple:slash">
{{categories}}
</ul>
<h2 class="entry-title">
<a href="{{permalink}}" rel="bookmark">{{title}}</a>
</h2>
<ul class="entry-meta" data-type="simple:slash">
<li class="meta-date" itemprop="datePublished">
<time class="ct-meta-element-date" datetime="{datetime}">{{date}}</time>
</li>
</ul>
<div class="entry-excerpt">{{excerpt}}</div>
<a
class="entry-button" data-type="simple"
href="{{permalink}}"
>
Weiterlesen
<span class="screen-reader-text">
{{title}}
</span>
</a>
</article>
';
$data = array(
'id' => $postArr['ID']
,'title' => $postArr['post_title']
,'date' => date('d.m.Y',strtotime($postArr['post_date']))
,'datetime' => date('c',strtotime($postArr['post_date']))
,'excerpt' => $postArr['post_excerpt']
,'permalink' => get_permalink($postId)
,'thumbnail' => get_the_post_thumbnail($postId, '400x300' ,array('class' => 'img-fluid'))
,'categories' => $catlist
,'categorynames' => implode(' ' , $categorynames)
);
$output .= renderHTML($tplArticle, $data);
}
/*
* Render $tplWrapper
*/
$tplWrapper = '
<div data-prefix="categories">
<h3>Beiträge ({{total}})</h3>
<div class="entries" data-archive="default" data-layout="grid" data-cards="boxed">
{{output}}
</div>
</div>
';
$data = array(
'output' => $output
,'total' => $total
);
$output = renderHTML($tplWrapper, $data);
$time_end = microtime(true);
$execution_time = ($time_end - $time_start);
$msg['shortcode']['execution_time'] = $execution_time;
return $output;
}
add_shortcode('wsrn_autorPosts', 'wsrn_autorPosts');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment