How to develop plugin for WordPress?

0
420

In fact, it would take no more than 10 minutes to write a simple WordPress plugin. In this article, we’ll look at how to write a simple plugin to display the last three articles anywhere on the site using a shortcode.

 

First, in the project folder /wp-content/plugins/, create the name of the plugin folder, for example, ms-plugin. Inside this folder, create 2 files, the first ms_plugin.php is the code handler, the second ms.css is the css styles.

 

The contents of the ms_plugin.php file:

<?php
/**
 * Plugin Name: MS plugin posts view
 * Plugin URI: http://www.mywebsite.com
 * Description: Plugin post views
 * Version: 1.0
 * Author: Sergey Mak
 * Author URI: http://www.mywebsite.com
 */

function ms_posts($content) {
    global $post;
    $out_stat = '';
    $posts = get_posts( array(
        'numberposts' => 3, // number of output records
        'category'    => 0,
        'orderby'     => 'date', // sort by date
        'order'       => 'ASC', // descending
        'include'     => array(),
        'exclude'     => array(),
        'meta_key'    => '',
        'meta_value'  =>'',
        'post_type'   => 'post',
        'suppress_filters' => true, // suppression of work of filters of change of SQL query
    ) );
    add_image_size( 'spec_thumb', 360, 240, true );   // record thumbnail size
    $out1 = '<div class="container"><div class="row eee"><div class="posts-block" style="display: flex; flex-wrap: wrap; justify-content: space-between;">';

    foreach( $posts as $post ) {
        $link = get_the_permalink();
        //$img = get_the_post_thumbnail('spec_thumb');
        $img = get_the_post_thumbnail_url() ;
        $out_stat .= '<div class="col-md-4">
                            <div class="img-post"><img src="'.$img.'" alt=""></div>
                            <h3><a href="'.$link.'">'.get_the_title().'</a></h3>
                            <p>'.get_the_date().'</p>
                            <p>'.get_the_excerpt().'</p>
                      </div>';
    }
    $out2 = '</div></div></div>';
    return $out1.$out_stat.$out2;
}
add_shortcode( 'ms_posts', 'ms_posts' );
function art_add_css(){
    wp_register_style( 'ivs_style', plugins_url( '/ms.css', __FILE__)) ;
    wp_enqueue_style('ivs_style');
}

add_action('wp_enqueue_scripts', 'art_add_css');

The contents of the ms.css file (here you can add any styles for the plugin):

.img-post {
    border: #000 1px solid;
}

As a result, if you paste the shortcode [ms_posts] anywhere, the latest news will be displayed on the site:

 

 

You can also connect js libraries to this plugin in order to make a news slider or carousel. As a result, in the future you will spend no more than 10 minutes to create a plugin for WP.

LEAVE A REPLY

Please enter your comment!
Please enter your name here