0

I'm by no means a PHP guru and have been given the task of updating a WordPress site. I've managed to get my php-includer plugin to work so that I can use a shortcode within the content area which the editor can then reference a stand-alone file which will then get injected into the main page.

The code I'm using is this:

<?php
function PHP_Include($params - array()) {
extract(shortcode_atts(array(
'file' => 'default'
), $params));
ob_start();
include(get_stylesheet_directory() . "/inc/$file.php");
return ob_get_clean();
}
add_shortcode('phpinclude', 'PHP_Include');
?>

I've saved this as a plugin and then using [phpinclude file='testfile'] in the content on a blog post. While the external testfile.php is included into the content, what I am after is a way of targeting parameters.

So for example, if the page held 4 images, I could specify in a parameter how many I would want to display. The editor could then add the following code [phpinclude file='testfile' images=2]. This would then display only 2 images instead of the default 4 images.

Does anyone know of a way to do this with the code I've shown above or point me in the right direction?

lozzaC
  • 1
  • 1
  • I know this isn't the answer you are looking for, but shortcodes are easy to create and maintain (without a plugin even). http://codex.wordpress.org/Shortcode_API – Matthew Darnell Mar 03 '15 at 16:23
  • possible duplicate of [Passing a variable from one php include file to another: global vs. not](http://stackoverflow.com/questions/4675932/passing-a-variable-from-one-php-include-file-to-another-global-vs-not) – cpilko Mar 03 '15 at 16:28

1 Answers1

1

If you just add that parameter to your shortcode:

<?php
function PHP_Include($params - array()) {
    extract(shortcode_atts(array(
        'file' => 'default',
        'images' => 4
    ), $params));
    ob_start();
    include(get_stylesheet_directory() . "/inc/$file.php");
    return ob_get_clean();
    }
    add_shortcode('phpinclude', 'PHP_Include');
    ?>

then the $images variable containing the number of images to display is available to the code of your included PHP file, which can act upon it accordingly.