How to programmatically create a page without WP post type or custom post type? Simply expand with data from different tables based on a URL query

How to programmatically create a page without WP post type or custom post type? Simply expand with data from different tables based on a URL query

3 minutes, 28 seconds Read

I have a process that creates invoices and stores wdcm_invoices in a table. Another process takes the data and outputs the HTML format and URL as https://inv.com/billing/richard-invoice-2036/. The numbers represent the row ID. There is no post type for this view, all content is dynamically generated based on the URL keyword billing and passed to a template.

The error I’m having is within wp_head() where none of the remove_action(‘wp_head’,’head tag’) hooks are functional, therefore duplication is happening and I need to add some default remove tags. Second, is there a “correct” process to achieve this “page without post_type or plugin” within WordPress?

Processes I created

WP rewriting methods

The flush is handled manually via the WP settings > permalinks

function invoicePageRewriteRule() 
{
    add_filter('query_vars', function($var) {
        $var[] = 'billing';
        return $var;
    });
    add_rewrite_tag('%billing%', '([^&]+)');
    add_rewrite_rule('^billing/([^/]*)/?', 'index.php?billing=$matches[1]', 'top');

    return;
}

Front exit

function invoicePage() 
{
    if( strstr(currenturl(), 'billing') ) 
    {
        $v = _vars(); // a function that collects various data from tables
        // load template
        add_filter('template_include', function() {
            return locate_template('page-billing.php');
        });
        remove_action('wp_head', 'title'); // does nothing
        // this hook adds browser title above default tag which causes duplicates
        add_action('wp_head', function() use($v) {
            echo '<title>'.$v->page_title.'</title>';
        },1);
        
        // action hook into template page-billing.php
        add_action('wdcm_billing', function() use($v)
        {
            global $wpdb;
            $invid = getint(currenturl()); //get number from basename
            $pre = $wpdb->prefix;
    
            $q = 'SELECT * FROM '.$pre.'wdcm_invoices WHERE invoice_id='.$invid;
    
            $inv = $wpdb->get_row($q);
            $html="";
            if( !isset($inv->client_id) )
                return;
            
            $user = get_userdata($inv->client_id)->data;
            $item = json_decode($inv->items)[0];
            $expire_date = date('Y-m-d', strtotime($inv->date_due.' +'.$item->period.' months'));
            $html="
            <h1>".$v->page_title.'</h1>
            <div >
                <h2 >
                    <span>'.$user->display_name.'</span>
                    <span>Amount Due: '.money($inv->total)->format.'</span>
                    <span>Invoice Number: '.$invid.'</span>
                    <span>Due: '.wp_date(DATEFORMAT, strtotime($inv->date_due)).'</span>
                </h2>
            </div>
            <div >
                <div>
                    <ul>
                        <li>'.$item->name.'</li>
                        <li>Service type: '.$item->type.'</li>
                        <li>Period: '.$item->period.' months</li>
                        <li>Start date: '.wp_date(DATEFORMAT, strtotime($inv->date_ordered)).'</li>
                        <li>Expire date: '.wp_date(DATEFORMAT, strtotime($expire_date)).'</li>
                    </ul>
                </div>
                <div>
                '.maaxPay::paypal([
                'clid' => $v->pp_client_id,
                'item_price' => $inv->total,
                'item_name' => $item->name,
                'currency' => $v->pay_currency
                ]).'
                </div>
            </div>
            ';
            
            echo $html;
        });
    }
}

Template-page-billing.php

<?php
/*
Template Name: Page Billing
Theme: WDCM
File: page-billing.php
*/

get_header(); ?>
<div id="cmsecontentBox" >
<?php do_action('wdcm_billing'); ?>
</div>
<?php get_footer(); ?>

Source of page view

There will be doubles printed

<!DOCTYPE html>

<html lang="en-US">
    <head>
    <meta name="robots" content="max-image-preview:large" />
    <style>img:is([sizes="auto" i], [sizes^="auto," i]) { contain-intrinsic-size: 3000px 1500px }</style>
<!-- hooked title -->
    <title>Client Area</title>
<link  href="//cdn.jsdelivr.net" />
<link  href="//cdnjs.cloudflare.com" />
<link  href="//ajax.googleapis.com" />
<link  href="//maxcdn.bootstrapcdn.com" />
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<!-- default WP title still printed -->
<title> | inv.com</title>

Image

#programmatically #create #page #post #type #custom #post #type #Simply #expand #data #tables #based #URL #query

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *