Plugins Development

Learning Plugins Development: Part 2

This WordPress plugin creates a shortcode [show_posts] that displays a list of the latest posts with their titles linked to the post pages. It fetches and lists up to 10 posts ordered by their ID in ascending order.

The output will be like this

Steps:

  1. create a folder named “test-plugin” inside plugins folder
  2. create a php file named “test-plugin.php” inside that “test-plugin” folder
  3. in that test-plugin.php put this code
<?php
/*
Plugin Name: Test Plugin
Plugin URI: https://facebook.com
Author: Maya Nova
Author URI: https://google.com
Description: This is a test plugin
  
function show_posts_function()
{
    ob_start();
    $args = array(
        'post_type' => 'post',
        'posts_per_page' => 10,
        'orderby' => 'ID',
        'order' => 'ASC',

    );
    $the_query = new WP_Query($args);

    if ($the_query->have_posts()) {

        $output = '<ul>';
        while ($the_query->have_posts()) {
            $the_query->the_post();
            $output .= '<li><a href="' . get_the_permalink() . '">' . get_the_title() . '</a></li>';
        }

        $output .= '</ul>';
    }
    return $output;
    ob_get_clean();
}
add_shortcode("show_posts", "show_posts_function");
 
 

Leave a comment