PHP snippets

PHP cannot be inserted directly as “<?php … ?>”. It must be done using a “shortcode” (or “snippet”). These snippets can be generated using a plugin, e.g. “Insert PHP Code Snippet” (XYZ Scripts), or by simple coding of a php-function as as shortcode. I tried the plugin but found that it is not needed.

Direct PHP inside an HTML block:

A shortcode is defined as a php-function as follows:

<?php
function jk_greeting($atts) {
    $name = $atts['name'];
    return "Hello $name";
}
add_shortcode( 'jk_greeting', 'jk_greeting' );
// call as [jk_greeting name="Jiri"]
?>

and called on a HTML page as [ jk_greeting name="Jiri" ]. The result of this call is: Hello Jiri. The result of the call is not seen in the design mode, only in the normal view.

Leave a Comment

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