How to Create Your Next Web Application on WordPress

If you have decided to code your next web application on top of WordPress framework, following are some of key considerations that would help you to get started quickly:

  1. Database interactivity from your custom pages
  2. Creating custom pages
  3. Retrieving GET parameters
  4. URL Rewrite

Following is detail on all of the above points:

  • Database Interactivity: Having your custom pages interact with the database is one of the key. Following are tips to get set with database connectivity from custom pages.The database information including host, database name, username and password is all configured in the wp-config.php in the root folder. All you got to do is include wp-config.php file in your database wrapper class file and use the variable such as DB_HOST, DB_NAME, DB_USER, DB_PASSWORD.Following code/commands can be used in your class file to interact with database:… include following near the file inclusion code. I defined ROOT variable for $_SERVER[ ‘DOCUMENT_ROOT’ ]. You may choose different method for the same.

    define( ‘ROOT’, $_SERVER[‘DOCUMENT_ROOT’]  );

    include_once( ROOT.”/wp-config.php” );

    …later in the code use following command:
    $conn = mysql_connect( DB_HOST, DB_USER, DB_PASSWORD );
    @mysql_select_db( DB_NAME, $conn );
    $result = @mysql_query( $query, $conn );

    The recommended method would be to encapsulate the database code in a class such as DBHandler.php, create an instance of DBHandler, pass the query as $query, and return the $result.

  • Custom pages: WordPress helps you to add the content pages from its dashboard. However, your web application may require custom content to be pulled from database and displayed in customized manner. Following are some of the Key aspects of custom webpages:
    1. Create custom classes for wrapping business and data logic.
    2. Start the custom PHP page with following code: <?php /* Template Name: PageName */ ?>
    3. Put header and footer code on top and bottom of the page such as following:
      get_header();

      get_footer();
      In case, you want to have sidebar included in the page, you may want to instead, have following as footer:get_sidebar( ‘PageName’ );
      get_footer();
  • Retrieving GET parameters: Include custom classes and call the method to retrieve the data. In case, you are expecting the parameters from $_GET, you may have to do a set of things to be able to receive parameters from $_GET. The content for same is out of scope for this blog and shall be presented in later blogs. In brief, following code would be placed in your custom page if, say, you are expecting value for “ptype” parameter:

    global $wp_query;
    $type = $wp_query->query_vars[‘ptype’];

    In functions.php, add following code:

    add_filter( ‘rewrite_rules_array’,’my_insert_rewrite_rules’ );

    function my_insert_query_vars( $vars )
    {    
        array_push($vars, ‘ptype’);
        return $vars;
    }

  • URL Rewrite: For URL rewriting, one needs to add methods in add_filter and add_action method and add corresponding methods such as following in functions.php code.

    add_filter( ‘rewrite_rules_array’,’my_insert_rewrite_rules’ );

    add_action( ‘wp_loaded’,’my_flush_rules’ );
    // flush_rules() if our rules are not yet included

    function my_flush_rules(){
        $rules = get_option( ‘rewrite_rules’ );    if ( ! isset( $rules[‘^company-investments/([^/]*)$’] ) || !isset( $rules[‘^(investments/tags)/([a-zA-Z][^/]*)$’] ) ) {
            global $wp_rewrite;
               $wp_rewrite->flush_rules();
        }
    }// Adding a new rule

    function my_insert_rewrite_rules( $rules )
    {
        $newrules = array();
        $newrules[‘^(company-investments)/([^/]*)$’] = ‘index.php?pagename=$matches[1]&subject=$matches[2]’;
            $newrules[‘^(investments/tags)/([a-zA-Z][^/]*)$’] = ‘index.php?pagename=$matches[1]&tkey=$matches[2]’;
        return $newrules + $rules;
    }

    Check the rules related with company-investments in both my_flush_rules and my_insert_rewrite_rules method.
Ajitesh Kumar

Ajitesh Kumar

I have been recently working in the area of Data analytics including Data Science and Machine Learning / Deep Learning. I am also passionate about different technologies including programming languages such as Java/JEE, Javascript, Python, R, Julia, etc, and technologies such as Blockchain, mobile computing, cloud-native technologies, application security, cloud computing platforms, big data, etc. I would love to connect with you on Linkedin. Check out my latest book titled as First Principles Thinking: Building winning products using first principles thinking.
Posted in Software Engg, Web, Wordpress. Tagged with , .