Categories

Thursday, March 28, 2024
#919814419350 therichposts@gmail.com
Wordpress HooksWordpress Tricks

Best Practices – WordPress Plugin Making Part 2

Best Practices – WordPress Plugin Making Part 2

Hello to all, welcome to therichpost.com. In this post, I will continue with, Best Practices – WordPress Plugin Making Part 2.

Here is the first part : Best Practices WordPress Plugin Making

In this plugin making code, I am creating page in wordpress admin pages section during plugin activation and I think, this is awesome.

In this post, I am also telling wordpress function for creating page in wordpress admin pages section.

 

Here is the plugin files structure:

 

Best Practices – WordPress Plugin Making Part 2

 

Here is the working and tested code and you need to add into plugin files:

 

1. Here is the code for wc-rent-products.php file, you need to add:

<?php
/**
 * Plugin Name: Test Plugin
 * Plugin URI: 
 * Description: 
 *
 */

define( 'RENTAL_PLUGIN_DIR' , plugin_dir_path( __FILE__ ));
if(!class_exists('RentProduct')){
  require_once( RENTAL_PLUGIN_DIR. 'class.rental.php' );
}

 //Function For Creating New Page With Content
 function add_my_custom_page() {
        $PageGuid = site_url() . "/my-page-req1";
        $my_post  = array( 'post_title'     => 'My page Reql',
                           'post_type'      => 'page',
                           'post_name'      => 'my-page',
                           'post_content'   => 'This is my page reql.',
                           'post_status'    => 'publish',
                           'comment_status' => 'closed',
                           'ping_status'    => 'closed',
                           'post_author'    => 1,
                           'menu_order'     => 0,
                           'guid'           => $PageGuid );
        $page_exists = get_page_by_title( $my_post['post_title'], OBJECT );
    if( count($page_exists) == 0 ) {
    		 $PageID = wp_insert_post( $my_post, FALSE ); // Get Post ID - FALSE to return 0 
    	}
        }
        register_activation_hook( __FILE__, 'add_my_custom_page'); 
?>

2. Here is the code for class.rental.php file:

You can add, more code in it, I am just add this file for WordPress plugin standard(You can remove this file also):

<?php

/*
Plugin classes
*/

class RentProduct{

  public static function init() {
        //Add Your More Code
    }
   
}
add_action( 'init', array( 'RentProduct', 'init' ));
?>

If you have any query related to this post, then do comment or ask question.

Thank you,

Chamkila,

TheRichPost

therichpost
the authortherichpost
Hello to all. Welcome to therichpost.com. Myself Ajay Malhotra and I am freelance full stack developer. I love coding. I know WordPress, Core php, Angularjs, Angular 14, Angular 15, Angular 16, Angular 17, Bootstrap 5, Nodejs, Laravel, Codeigniter, Shopify, Squarespace, jQuery, Google Map Api, Vuejs, Reactjs, Big commerce etc.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.