WordPress Math Publisher Plugin Home Page

Description

The “WP Math Pub” plugin for WordPress, or wpmathpub for short, displays mathematical equations within your posts, comments, and pages. It’s a simple plugin that takes advantage of Pascal Brachet’s PHP Math Publisher version 0.3 library - bundled with this plugin.

How it works

Put your plain text mathmatical expressions between custom tags that look like this:

[pmath size=xx]…[/pmath]

The optional size attribute controls how large the images will be displayed. Useful xx integer values range from 8 to 24. Size defaults to 12 when the size attribute is omitted.

I created the first version of the plugin by starting with Matteo Bertini’s wpmathpublisher version 1.0.3 WordPress plugin source code. This is why my first version (v. 1.0.4) was just a plus 1 increment to Matteo’s version; while, my current version is still backward compatible with his tagging scheme too.

Here’s an example:

Code (mathtext)
  1.  
  2. [pmath](a^2+b^2)=a^2+2ab+b^2[/pmath]

Results in:

(a^2+b^2)=a^2+2ab+b^2

In the example above, pmath is the tag to indicate to my plugin that the text that follows up to the /pmath tag, should be automatically submitted to Pascal’s PHP Math Publisher library for creation of a transparent graphic image. Finally, the tags and its encompassed text are replaced by the graphic image for display in this blog post.

I built upon Matteo’s pmath tag by adding an optional size attribute. Pascal’s PHP Math Publisher Library supports a size parameter as shown on his demo page: here>

Here is a simple continuation of Matteo’s example - with size attribute included:

Code (mathtext)
  1. [pmath size=16](a^2+b^2)=a^2+2ab+b^2[/pmath]
  2.  
  3. where:
  4. [pmath size=12]~a[/pmath] is defined as a
  5. [pmath size=12]~b[/pmath] is defined as b

Results in:

(a^2+b^2)=a^2+2ab+b^2

where:
~a is defined as a
~b is defined as b

But perhaps this is a more stunning example:

Code (mathtext)
  1. [pmath size=8]f(x)~left~ sum{kappa=1}{infty}{delim{[}{{1/(x_kappa)^kappa}}{]}}[/pmath] ; size=8
  2.  
  3. [pmath size=12]f(x)~left~ sum{kappa=1}{infty}{delim{[}{{1/(x_kappa)^kappa}}{]}}[/pmath] ; size=12 (same as default)
  4.  
  5. [pmath size=16]f(x)~left~ sum{kappa=1}{infty}{delim{[}{{1/(x_kappa)^kappa}}{]}}[/pmath] ; size=16
  6.  
  7. [pmath size=24]f(x)~left~ sum{kappa=1}{infty}{delim{[}{{1/(x_kappa)^kappa}}{]}}[/pmath] ; size=24

Results in:

f(x)~left~ sum{kappa=1}{infty}{delim{[}{{1/(x_kappa)^kappa}}{]}} ; size=8

f(x)~left~ sum{kappa=1}{infty}{delim{[}{{1/(x_kappa)^kappa}}{]}} ; size=12 (same as default)

f(x)~left~ sum{kappa=1}{infty}{delim{[}{{1/(x_kappa)^kappa}}{]}} ; size=16

f(x)~left~ sum{kappa=1}{infty}{delim{[}{{1/(x_kappa)^kappa}}{]}} ; size=24

Source code for version 1.0.7

Code (php)
  1. < ?php
  2. /***************************************************************************************
  3. Plugin Name: WP Math Publisher
  4. Plugin URI: http://www.embeddedcomponents.com/blogs/wordpress/wpmathpub/
  5. Description: Display mathematical equations within your posts and comments. Put your plain text <a href="http://www.xm1math.net/phpmathpublisher/doc/help.html">mathmatical expressions between [pmath size=xx]…[/pmath] tags. The optional size attribute controls how large the images will be displayed. Useful xx integer values range from 8 to 24. Size defaults to 12 when attribute omitted. Pascal Brachet’s PHP Math Publisher <a href="http://www.xm1math.net/phpmathpublisher/">library</a> is included.
  6. Version: 1.0.7
  7. Date: Sept. 6, 2008
  8. Author: Ron Fredericks, Embedded Components
  9. Author URI: http://www.embeddedcomponents.com/blogs/
  10.  
  11. Easy install notes:
  12.         Just copy the wpmathpub directory and all its contents into your WordPress plugins directory.
  13.  
  14. Platforms tested:
  15.         1) Linux Apache web server, php 4.4.4, WordPress 2.0.4, default theme, installed in subdirectory,
  16.         2) Linux Apache web server, php 4.4.4, WordPress 2.3.3, clasic theme, installed in root directory,
  17.         3) Linux Apache web server, php 4.4.4, WordPress 2.6.2, default theme, installed in subdirectory.
  18.        
  19. References:
  20.         Pascal Brachet’s phpmathpublisher
  21.                 Home: http://www.xm1math.net/phpmathpublisher/
  22.                 Usage: http://www.xm1math.net/phpmathpublisher/doc/help.html
  23.         Matteo Bertini’s WordPress plugin called PHP Math Publisher
  24.                 http://www.slug.it/naufraghi/programmazione-web/wpmathpublisher
  25.         Randy Morrow’s WordPress plugin called Axiom
  26.                 http://wordpress.org/extend/plugins/axiom/#post-2794
  27.        
  28. ***************************************************************************************/
  29. /***************************************************************************************
  30.  
  31.     Copyright 2008  Ron Fredericks, Embedded Components, Inc.  (email : ronf@EmbeddedComponents.com)
  32.        
  33.         GNU General Public License
  34.     This program is free software; you can redistribute it and/or modify
  35.     it under the terms of the GNU General Public License as published by
  36.     the Free Software Foundation; either version 2 of the License, or
  37.     (at your option) any later version.
  38.  
  39.     This program is distributed in the hope that it will be useful,
  40.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  41.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  42.     GNU General Public License for more details.
  43.  
  44.     You should have received a copy of the GNU General Public License
  45.     along with this program; if not, write to the Free Software
  46.     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  47. ***************************************************************************************/
  48. //                     ** Begin wpmathpub Code **
  49. define("WPMATHPUBVERSION", ‘1.0.7′);
  50.  
  51. //
  52. // Control automatic test and set feature for write access to ‘img’ directory.
  53. //           Automatic test and set feature when define("AUTOCHMOD", true);
  54. //           Manual automatic test and set feature when define("AUTOCHMOD", false);
  55. define("AUTOCHMOD", true);
  56.  
  57. // Control support for [pmath] tag in blog comments
  58. //            Engage the [pmath] tag support in blog comments when define("ENGAGECOMMENTS", true);
  59. //            Disengage the [pmath] tag support in blog comments when define("ENGAGECOMMENTS", false);
  60. define("ENGAGECOMMENTS", true);
  61.  
  62. // Include Pascal’s php math publisher library.
  63. define("PHPMATHLIB", ‘phpmathpublisher’, true);
  64. require_once(PHPMATHLIB.‘/mathpublisher.php’) ;
  65.  
  66. // Determine depth of relative addressing based on location of current running script: default value is that of WordPress install directory.
  67. $depth=(end(explode("/",getcwd())) == "wp-admin") ? "..": ".";
  68.  
  69. // Overwrite mathpublisher.php’s default pointers to /img and /fonts subdirectories with a flexible relative addressing scheme.
  70. $basedir = ‘/wp-content/plugins/’.basename(dirname(__FILE__), ".php").‘/’.PHPMATHLIB;
  71. $dirfonts=$depth.$basedir.‘/fonts’;
  72. $dirimg=$depth.$basedir.‘/img’;
  73.  
  74. // Create a seperate absolute pointer to the phpmathpublisher /img/ subdirectory because our relative address scheme won’t work when called from the "apply filter" php module within the WordPress Loop.
  75. $abs_dirimg = get_bloginfo(‘url’).$basedir.‘/img/’;
  76.  
  77. // test for proper installation and a known run-time environment
  78. $abs_dirimg_readable = is_readable($dirimg);
  79.  
  80. // Attempt to make $dirimg writable if it is not writable already.
  81. $abs_use_mathfilter = true;
  82. if (AUTOCHMOD && $abs_dirimg_readable) {
  83.         if (!is_writable($dirimg)) {
  84.                 if (!chmod($dirimg, 0755)) {
  85.                         $abs_use_mathfilter = false;
  86.                 }
  87.                 clearstatcache();
  88.         }
  89. }
  90.  
  91. // Returns with <img src=http://www.yoursite.com/yourblog/wp-content/pluglins/wpmathpub/phpmathpublisher/img/some_unique_image.png/> HTML tag.
  92. // Makes reference to mathfilter function included from PHPMATHLIB/mathpublisher.php code.
  93. function wpmathfilter($ascii_math, $size_math)
  94. {
  95.         global $abs_dirimg;
  96.         global $abs_use_mathfilter;
  97.         global $abs_dirimg_readable;
  98.                
  99.         // Define the default font size.
  100.         if (empty($size_math)) $size_math = ‘12′;
  101.        
  102.         if ($abs_use_mathfilter && $abs_dirimg_readable) {
  103.                 // html_entity_decode() converts HTML entities like ">" back to standard text like ">", when present.
  104.                 $phpmath = mathfilter("<m>".html_entity_decode($ascii_math)."</m>", $size_math, $abs_dirimg);
  105.         } else if ($abs_dirimg_readable) {
  106.                 $phpmath = ‘<span style="color: red">Error:</span>’." $abs_dirimg must have write access".‘ <a href="http://wordpress.org/extend/plugins/wpmathpub/faq/" title="use ‘."’chmod 755 img’".‘ to attempt to manually fix this problem on your server">Read the official wpmathpub plugin FAQ for more details</a>’;
  107.         } else {
  108.                 $phpmath = ‘<span style="color: red">Error:</span>’." wpmathpub plugin not usable under these conditions: $abs_dirimg";
  109.         }
  110.         return $phpmath;
  111. }
  112.  
  113. // Create a WordPress text filter
  114. function to_phpmath($content)
  115. {
  116.         // Add an new optional font size attribute size=xx to Matteo’s original preg_replace.
  117.         $content = preg_replace(‘#\[pmath(\s+size=|\s?)(\d*)(\])(.*?)\[/pmath\]#sie’, ‘wpmathfilter(\’\\4\’, \’\\2\’);’, $content);
  118.         return $content;
  119. }
  120.  
  121. // action function for above hook
  122. function mt_add_pages() {
  123.     // Add a new submenu under Manage:
  124.     add_management_page(‘wpmathpub’, ‘wpmathpub’, 8, ‘wpmathpubmanage’, ‘wpmathpub_manage_page’);
  125. }
  126.  
  127. // wpmathpub_manage_page() displays the page content for the Test Manage submenu
  128. function wpmathpub_manage_page() {
  129.         global $abs_dirimg;
  130.         global $dirimg
  131.         global $abs_use_mathfilter;
  132.         global $abs_dirimg_readable;
  133.         global $abs_addfilter_test;
  134.  
  135.         $abs_dirimg_executable = is_executable($dirimg);
  136.         if ($abs_use_mathfilter)
  137.                 $abs_use_mathfilter = is_writable($dirimg);     // make sure img directory really is writable
  138.        
  139.         $arraytemp = gd_info(); // collect details on server’s support of GD graphics library
  140.        
  141.         $tabcolor = array(‘#dddddd’, ‘#ffffcc’);
  142.         $tabcnt = 0;   
  143.         echo "<table width=’700′ border=’0′ cellspacing=’1′ cellpadding=’1′>";
  144.        
  145.         echo "<tr>";
  146.     echo "<th scope=’col’> </th><th scope=’col’><span style=’color: blue’><h3>wpmathpub plugin status: ".WPMATHPUBVERSION."</h3></span></th>";
  147.         echo "</tr>";
  148.        
  149.         echo "<tr>";
  150.         echo "<th scope=’row’ width=’240′ align=’right’><span style=’color: blue’>Operating system:</span></th>";
  151.     echo "<td bgcolor=".$tabcolor[$tabcnt++ %2].">".PHP_OS."</td>";
  152.         echo "</tr>"
  153.                
  154.         echo "<tr>";
  155.         echo "<th scope=’row’ width=’240′ align=’right’><span style=’color: blue’>PHP version:</span></th>";
  156.     echo "<td bgcolor=".$tabcolor[$tabcnt++ %2].">".phpversion()."</td>";
  157.         echo "</tr>";  
  158.        
  159.         echo "<tr>";
  160.     echo "<th scope=’row’ width=’240′ align=’right’><span style=’color: blue’>PHP GD library:</span></th>";
  161.     echo "<td bgcolor=".$tabcolor[$tabcnt++ %2].">".((strlen($arraytemp["GD Version"])>1) ? ("version ".$arraytemp["GD Version"]) : "<span style=’color: red’> ERROR: GD library not found on this server</span>") .(($arraytemp["PNG Support"]===true) ? " with PNG format supported" : "<span style=’color: red’> ERROR: PNG format not supported</span>")."</td>";
  162.         echo "</tr>";
  163.        
  164.         echo "<tr>";
  165.     echo "<th scope=’row’ width=’240′ align=’right’><span style=’color: blue’>Ownership:</span></th>";
  166.     echo "<td bgcolor=".$tabcolor[$tabcnt++ %2].">".((getmyuid()==fileowner($dirimg)) ? "" : "<span style=’color: red’>WARNING </span>")." script owner=".getmyuid().", img file owner=".fileowner($dirimg)."</td>";
  167.         echo "</tr>"
  168.        
  169.         echo "<tr>";
  170.     echo "<th scope=’row’ width=’240′ align=’right’><span style=’color: blue’>Blog’s url:</span></th>";
  171.     echo "<td bgcolor=".$tabcolor[$tabcnt++ %2].">".get_bloginfo(‘url’)."</td>";
  172.         echo "</tr>"
  173.        
  174.         echo "<tr>";
  175.     echo "<th scope=’row’ width=’240′ align=’right’><span style=’color: blue’>WordPress version:</span></th>";
  176.     echo "<td bgcolor=".$tabcolor[$tabcnt++ %2].">".get_bloginfo(‘version’)."</td>";
  177.         echo "</tr>"
  178.        
  179.         echo "<tr>";   
  180.         echo "<th scope=’row’ width=’240′ align=’right’><span style=’color: blue’>WordPress plugin name:</span></th>";
  181.     echo "<td bgcolor=".$tabcolor[$tabcnt++ %2].">".basename(dirname(__FILE__), ".php")."</td>";
  182.         echo "</tr>";  
  183.  
  184.         echo "<tr>";
  185.     echo "<th scope=’row’ width=’240′ align=’right’><span style=’color: blue’>Relative img path:</span></th>";
  186.     echo "<td bgcolor=".$tabcolor[$tabcnt++ %2].">".$dirimg."</td>";
  187.         echo "</tr>"
  188.        
  189.         echo "<tr>";
  190.     echo "<th scope=’row’ width=’240′ align=’right’><span style=’color: blue’>Working directory:</span></th>";
  191.     echo "<td bgcolor=".$tabcolor[$tabcnt++ %2].">".getcwd()."</td>";
  192.         echo "</tr>";  
  193.  
  194.         echo "<tr>";
  195.     echo "<th scope=’row’ width=’240′ align=’right’><span style=’color: blue’>Absolute img path:</span></th>";
  196.     echo "<td bgcolor=".$tabcolor[$tabcnt++ %2].">$abs_dirimg</td>";
  197.         echo "</tr>";
  198.        
  199.         echo "<tr>";
  200.     echo "<th scope=’row’ width=’240′ align=’right’><span style=’color: blue’>img directory readable:</span></th>";
  201.     echo "<td bgcolor=".$tabcolor[$tabcnt++ %2].">".(($abs_dirimg_readable) ? "yes" : "<span style=’color: red’>error</span>")."</td>";
  202.         echo "</tr>";
  203.        
  204.         echo "<tr>";
  205.     echo "<th scope=’row’ width=’240′ align=’right’><span style=’color: blue’>img directory writable:</span></th>";
  206.     echo "<td bgcolor=".$tabcolor[$tabcnt++ %2].">".(($abs_use_mathfilter) ? "yes" : "<span style=’color: red’>error</span>")."</td>";
  207.         echo "</tr>";
  208.        
  209.         echo "<tr>";
  210.     echo "<th scope=’row’ width=’240′ align=’right’><span style=’color: blue’>img directory executable:</span></th>";
  211.     echo "<td bgcolor=".$tabcolor[$tabcnt++ %2].">".(($abs_dirimg_executable) ? "yes" : "<span style=’color: red’>error</span>")."</td>";
  212.         echo "</tr>";
  213.                
  214.         echo "<tr>";
  215.     echo "<th scope=’row’ width=’240′ align=’right’><span style=’color: blue’>Content filter added:</span></th>";
  216.     echo "<td bgcolor=".$tabcolor[$tabcnt++ %2].">".(($abs_addfilter_test) ? "yes" : "<span style=’color: red’>error</span>")."</td>";
  217.         echo "</tr>";
  218.                
  219.         echo "<tr>";
  220.     echo "<th scope=’row’ width=’240′ align=’right’><span style=’color: blue’>mathfilter(y=mx^2+b):</span></th>";
  221.     echo "<td bgcolor=".$tabcolor[$tabcnt++ %2].">".mathfilter("<m>".html_entity_decode("y=mx^2+b")."</m>", ‘12′, $abs_dirimg)."</td>";
  222.         echo "</tr>"
  223.        
  224.         echo "</table>";
  225. }
  226.  
  227. // Register our WordPress text filter, to_phpmath, into the two hook routines, the_content and comment_text.
  228. if (!ENGAGECOMMENTS) {
  229. //           Register comment_text updates after all priorty comment processing filters.
  230. //            Note: calling the comment filter first, before the content filter, fixed comment_RSS feed errors.
  231.         remove_filter(‘comment_text’, ‘to_phpmath’);
  232. } else {
  233.         add_filter(‘comment_text’, ‘to_phpmath’);
  234. }
  235. //           Register the_content updates after all priorty content processing filters.
  236. $abs_addfilter_test = add_filter(‘the_content’, ‘to_phpmath’, 5);
  237.  
  238. // Hook for adding admin menus
  239. add_action(‘admin_menu’, ‘mt_add_pages’);
  240. //
  241. //                     ** End wpmathpub Code **
  242. ?>

Changes in version 1.0.7

  • Improve compatibility with streaming video plugins that would otherwise monopolize the WordPress built-in add_filter() function which in turn cause wpmathpub [ pmath ] tagging to fail
  • Add a new menu into for the WordPress blogger using the Site Admin -> Manage menu to display operating status of the wpmathpub plugin

Download plugin

The wpmathpub math publisher plugin for WordPress.org blogs is now available directly from WordPress.org’s plugin site. The WordPress.org hosted version manages several promotional and support features:

  • Overview of the plugin here>
  • Overview of the PHP Math Publisher library here>
  • Installation instructions here>
  • Example of how wpmathpub works, and how to install it here>
  • Frequently Asked Questions and their answers too here>
  • Metrics here>
  • Download from WordPress.org here>

Support:
Share your problems, thoughts, or success with the community here:
www.embeddedcomponents.com/blogs/2008/03/wpmathpubsupport/

Donations accepted

Do you like the engineering work we do in creating better components for your reuse? Are you satisfied with this plugin? Are you using this effort to help you or your company make profit? If the answer is yes to any of these questions, perhaps you might consider making a voluntary donation.


Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • scuttle
  • Slashdot

12 Responses to “WordPress Math Publisher Plugin Home Page”

  1. Ron Fredericks Says:

    This is a test of an open ended start tag embedded in a comment [pmath size=14] along with some text…

  2. Ron Fredericks Says:

    …and some more text e=sum{infty}{n=0}{1/{n!}}[/pmath] with an end tag to insure the new comments-enabled feature works without breaking something.

  3. Ron Fredericks Says:

    Comments seem to work fine, even when the tag is not used correctly. But it works even better when the math text and end tag from the previous comment are used together like this:

    [ pmath size=14 ]e=sum{infty}{n=0}{1/{n!}}[ /pmath ]

    or with the spaces removed from around the pmath brackets to form valid tags, like this:

    e=sum{infty}{n=0}{1/{n!}}

    to create a real equation.

  4. Ron Fredericks Says:

    Comments on this page are limited to Admin Team. To post a comment related to this plugin, please use this link:

    http://www.embeddedcomponents.com/blogs/2008/03/wpmathpubsupport/

  5. DiseƱo Web Says:

    Well done job, the size attribute are great.

    Still testing,

    Keep the work!

  6. martialarts Says:

    Hi all great information here and good thread to comment on.

    Can I ask though - how did you get this picked up and into google news?

    Very impressive that this blog is syndicated through Google and is it something that is just up to Google or you actively created?

    Obviously this is a popular blog with great data so well done on your seo success..

  7. Philix Says:

    This is a great plug in :)

  8. ipang Says: