• Do you know how to code php and want to add lots of custom content to your WordPress blog? I had a design that called for executing batch scripts nightly that would update data in a mysql database. Next I wanted POSTs to automatically generate in my WordPress blog displaying this data. Automation.

    Initially I was frustrated wondering how to execute php and get table tags embedded in my posts. I found a generic plugin (wp-tables) and dug into the code. Turned out the solution was super easy. In the two years looking at WordPress, I wish someone would have told me this… so I’m sharing with the rest of my newbie world.

    The technique uses a simple substitution method. The code below will scan all POSTS for specific tags. When these tags are found, they are substituted for real data that you format in the function below. First create a post with this:

    [YourModule=1]

    Then create one php file (yourmodule.php) with the following code.

    <?php
    
    /*
    Plugin Name: yourmodule
    Plugin URI: https://www.GTXWebDesign.com
    Description: This is a prototype module for easily creating custom, content for your posts.
    Author: Tom Jurgens
    Version: 1.0
    Author URI: https://www.GTXWebDesign.com
    
    Copyright 2006-2007 by Tom Jurgens
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    */ 
    
    // Define your module to WordPress admin
    //load_plugin_textdomain('yourmodule','wp-content/plugins/yourmodule');
    
    // Plugin activation
    //add_action('activate_yourmodule/yourmodule.php', 'checkdatabase');
    
    // Define module constant
    define(“_YOURMODULE”,”yourmodule”);
    
    // Action calls for all functions
    add_filter('the_content', 'yourfunction');
    
    function yourfunction($content) {
    
      $search = "/\[yourmodule\s*=\s*(\w+)\]|\[yourmodule\s*=\s*([^\]]+)\]/i";   
    
      preg_match_all($search, $content, $matches);
    
      if (is_array($matches[1])) {
        for ($m = 0; $m < count($matches[0]); $m++) {
          $search = $matches[0][$m];
          $lenx = strlen(_YOURMODULE) + 1;
          $getdigit = substr($search,$lenx,1);
    
          switch ($getdigit) {
          case 1: // format this content
            $replace = yourfunction1();
            break;
          case 2: // format this content
            $replace = yourfunction2();
            break;
          }
          $content = str_replace ($search, $replace, $content);
        }
      }
    
      return $content;
    }
    
    function yourfunction1() {
    
    	// Edit this function to format your content
    	// Fetch data from mysql and format into nice tables… whatever
    	$mycontent = "<table width='100%' cellspacing='1' cellpadding='2' border='1'>";
    	$mycontent .= "<tr bgcolor='#4169E1'><td align='center'>Team</td><td align='center'>Record</td></tr>";
    	$mycontent .= "<tr><td align='center'>Chicago</td><td align='center'>23-0-0</td></tr>";
    	$mycontent .= "<tr><td align='center'>New York</td><td align='center'>0-23-0</td></tr></table>";
    	$mycontent .= "Chicago is so better than New York!";
    	Return $mycontent;
    }
    
    function yourfunction2() {
    
    	// Edit this function to format your content
    	// Fetch data from mysql and format into nice tables… whatever
    	$mycontent = "The Yankees blow chunks!";
    	Return $mycontent;
    }
    
    ?>

    Now install the module using the WordPress admin page. If you view the post, your code should have substituted the literal [YourModule=1] with the output from yourfunction1. You can use this code for 0-9 and then A-Z…

  • The topic ‘Adding Content from Your MySql data & Formatting nice tables in POSTs’ is closed to new replies.