Thursday, January 18, 2018

Wordpress Plugin Creation

Step1(make the plugin appear in admin panel

//developed based on https://code.tutsplus.com/tutorials/create-a-custom-wordpress-plugin-from-scratch--net-2668
a. create a plugin file
b.add the follwing code on top

<?php
    /*
    Plugin Name: Plugin name as to be displayed in admin panel
    Plugin URI: http://www.outsource-online.net
    Description: Plugin for (description as to be shown in admin panel )
    Author: Sreekanth Dayanand
    Version: 1.0
    Author URI: http://www.outsource-online.net
    */
?>

Step 2(create an admin page and a menu item for it)

a.use the 'action_hook' 'admin_menu'
//list of action_hooks https://codex.wordpress.org/Plugin_API/Action_Reference
add_action('admin_menu', 'osol_plugin_actions' /* call back function */);

OR if it a class method

add_action( 'admin_menu', array( $classObj, 'osol_plugin_admin_menu' ) );

b. and the call back function

function osol_plugin_admin_menu()
{
//add_options_page( string $page_title, string $menu_title, string $capability, string $menu_slug, callable $function = '' )
add_options_page("OSOL Plugin Config", "OSOL Plugin Config", 1, "osol_plugin_config_main", "config_main");
}//function osol_plugin_admin_menu()

c. add the call back function
public function configMain()
{
require_once(RETSOW_GSMLS_VIEWS_FOLDER.DS.'admin'.DS.'config_main.php');
}//private function configMain()

d.Activate the plugin(VERY IMPORTANT)

Step 3 (adding frontend pages)

//https://wordpress.stackexchange.com/questions/3396/create-custom-page-templates-with-plugins
add_filter( 'page_template', 'wpa3396_page_template' );// only available for 'pages' added from admin panel
function wpa3396_page_template( $page_template )
{
    if ( is_page( 'new-page-test' ) )
{
        $page_template = dirname( __FILE__ ) . '/custom-page-template.php';
    }
    return $page_template;
}

Step 4 (programaticaly change title, meta etc)

//https://stackoverflow.com/questions/772510/wordpress-filter-to-modify-final-html-output
function callback($buffer) {
  //modify buffer here, and then return the updated code
  preg_match("/<title>([^<]*)<\/title>/",$buffer,$match);
$new_title = "Buffer test".$match[1];

$buffer = preg_replace("/<title>([^<]+)<\/title>/","<title>$new_title</title>",$buffer);
  return str_replace('___RESTWSO_REPLACABLE___', "buffer replace test",$buffer);
}

function buffer_start() { ob_start("callback"); }

function buffer_end() { ob_end_flush(); }

add_action('after_setup_theme', 'buffer_start');
add_action('wp_footer', 'buffer_end');

No comments:

Post a Comment