The XSLT Processor Plugin includes a number of XSL Stylesheets with general-purpose templates. Some of those templates are pure XSL, but many rely on PHP to generate their results. Some call built-in PHP functions directly. For instance, the string-replace
template calls str_replace()
. Others, like the date-format
template, call custom plugin methods to better handle defaults and options. Custom methods can also return not just scalar values, but full XML nodesets.
In this How To we’ll demonstrate both strategies – first, calling a built-in function and, second, calling a custom function. After creating and including a new PHP file, we’ll create a new XSL file to test the code using [xslt_transform_xml/]
.
For background on this feature, see the XSLTProcessor::registerPHPFunctions page at php.net .
shortcode
Let’s start at the end, and write the shortcode for this test. We’ll use [xslt_transform_xml/]
, of course, and first specify our new stylesheet in the xsl
attribute. We’ll skip the xml
attribute and just take the default – <NODATA />
– as input. And finally, we’ll add two ‘extra’ attributes – param1
and param2
.
The shortcode attributes will be used in the XSL as function parameters, first for the built-in PHP function convert_uuencode()
, and then again for the custom PHP function xslt_function_sample()
.
Compare the shortcode input and final output, here. The intermediate steps – config, php, and xsl – are detailed below.
[xslt_transform_xml xsl="sample-xslt-functions.xsl" param1="value one" param2="value two" /]
Final Output:
convert_uuencode(param1):
- )=F%L=64@;VYE `
convert_uuencode(param2):
- )=F%L=64@=’=O `
xslt_function_sample(param1,param2):
- param1: value one
- param2: value two
tenandtwo-xslt-functions.php
Following best practices, our additions to the callback system are kept in a separate file, which is added to the plugin (or quickly removed from it) at the bottom of plugins/xslt-processor/tenandtwo-xslt-functions.php
.
/**
* Sample extension
*
* built-in: convert_uuencode( string $string ): string
* custom: xslt_function_sample( string $param1, string $param2 ) : DomDocument
*/
if (is_readable(XSLT_PLUGIN_DIR.'includes/sample-xslt-functions.php')) {
require_once(XSLT_PLUGIN_DIR.'includes/sample-xslt-functions.php');
}
sample-xslt-functions.php
The first line in our PHP file adds a built-in function name to the global list of allowed callbacks – $XSLT_PLUGIN_PHP_FUNCTIONS. The function convert_uuencode()
is straightforward; it takes a single (1) string parameter and returns a string. In the XSL, we’ll call it with <xsl:value-of select="php:functionString( 'convert_uuencode', string($param) )" />
.
The second line adds our custom function name to the list of allowed callbacks. Our function xslt_function_sample()
takes two (2) string parameters and returns an XML/HTML fragment which, for simpicity, lists the values of those parameters. We’ll call this function with <xsl:copy-of select="php:function( 'xslt_function_sample', string($param1), string($param2) )" />
Note that at this point, inside our new function, we could do literally anything that PHP or WordPress allows. We could query the database, read a file, write to the log, combine local and remote sources, etc, etc. The only requirement is that we return either a scalar for <xsl:value-of/>
, or a nodeset for <xsl:copy-of/>
. The caller can either echo the value, as sample-xslt-functions.xsl
does, or capture the value in an <xsl:variable/>
for further processing.
$XSLT_PLUGIN_PHP_FUNCTIONS[] = 'convert_uuencode'; // built-in
$XSLT_PLUGIN_PHP_FUNCTIONS[] = 'xslt_function_sample'; // custom
function xslt_function_sample( $param1 = 'missing', $param2 = 'missing' )
{
$xml = '<ul>' .
'<li>param1: '.$param1.'</li>' .
'<li>param2: '.$param2.'</li>' .
'</ul>';
$doc = new DomDocument('1.0', 'utf-8');
$doc->loadXML( $xml );
return $doc;
}
sample-xslt-functions.xsl
In order to use select="php:function()"
and select="php:functionString()"
in the XSL, the PHP prefix and namespace URI must be added to the stylesheet declaration: xmlns:php="http://php.net/xsl"
. Then, to prevent that namespace info from appearing in the output (the default behavior), we add exclude-result-prefixes="php"
.
After setting the output method="html"
, we use <xsl:param/>
to catch the attributes set in the shortcode. Then, finally, we call our newly enabled functions –convert_uuencode()
and xslt_function_sample()
– mixing the results into the final output.
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:php="http://php.net/xsl"
exclude-result-prefixes="php"
>
<xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes" />
<xsl:param name="param1">dflt1</xsl:param>
<xsl:param name="param2">dflt2</xsl:param>
<xsl:template match="/">
<p>convert_uuencode(param1): </p>
<ul>
<li>
<xsl:value-of select="php:functionString('convert_uuencode', string($param1))" />
</li>
</ul>
<p>convert_uuencode(param2): </p>
<ul>
<li>
<xsl:value-of select="php:functionString('convert_uuencode', string($param2))" />
</li>
</ul>
<p>xslt_function_sample(param1,param2): </p>
<xsl:copy-of select="php:function('xslt_function_sample', string($param1), string($param2))" />
</xsl:template>
</xsl:stylesheet>