Skip to content

Instantly share code, notes, and snippets.

@jonathanbossenger
Created October 5, 2023 13:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonathanbossenger/3322c247bdd2b2143bb7755325b0bd14 to your computer and use it in GitHub Desktop.
Save jonathanbossenger/3322c247bdd2b2143bb7755325b0bd14 to your computer and use it in GitHub Desktop.
<?php
/**
* Plugin Name: WP Learn Compatibility
* Description: Learn to test a plugin for PHP Version Compatibility
* Version: 1.0.1
*/
/**
* Posts fetcher class
*/
class post_fetcher {
protected $posts;
public function post_fetcher() {
$this->posts = get_posts();
}
public function fetch_posts() {
$post_html = '<div class="post">';
foreach ( $this->posts as $post ) {
if ( property_exists( $post, 'post_title' ) ) {
$post_html .= sprintf(
'<h4><a href="%s">%s</a></h4>',
get_permalink( $post->ID ),
$post->post_title
);
}
}
$post_html .= '</div>';
return $post_html;
}
}
/**
* Shortcode to render posts
* Uses the post_fetcher class
*/
add_shortcode( 'wp_learn_php_compatibility', 'wp_learn_php_compatibility_shortcode_render' );
function wp_learn_php_compatibility_shortcode_render() {
$post_fetcher = new post_fetcher();
$post_html = $post_fetcher->fetch_posts();
return $post_html;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment