Quantcast
Channel: WordPress.org Forums » All Posts
Viewing all articles
Browse latest Browse all 120178

linuxplayground on "[Plugin: Custom Content Type Manager] How do I call a template for the shortcode"

$
0
0

OK - I think I have it resolved. I had to edit your code. Never a good thing for me because when you update the plugin, my edits will all be gone. But it's that important to me.

After much inserting of var_dump($blah); exit; lines I figured out what was happening.

The changes I made are as follows:

1. In the get posts function I included a block to unset $raw_args['tpl'] as follows (just after the block that unsets the help option... :) :

//added code to unset tpl from raw_args so it's not used in filter.
        if (isset($raw_args['tpl']) ) {
            unset($raw_args['tpl']);
        }

2. I had to change the test order for the template. So the heirachy is: if there is a template argument, try to use it. If not or it fails to load the template then look for the $content value. If that's not provided then use the default or if it is provided, read it and parse the bit between the open and close shortcode.

Here is the new function:

//------------------------------------------------------------------------------
    //! Private functions
    //------------------------------------------------------------------------------
    /**
     * Get the template (tpl) to format each search result.
     *
     * @param string  $content
     * @param array   $args    associative array
     * @return string
     * EDIT - DAVID LATHAM - Changed order of test for empty(content) and
     *                       empty(args[tpl])
     */
    private static function _get_tpl($content, $args) {
        $content = trim($content);
        if( !empty($args['tpl']) ) {
            // strip possible leading slash
            $args['tpl'] = preg_replace('/^\//', '', $args['tpl']);
            $file = ABSPATH .$args['tpl'];

            if ( file_exists($file) ) {
                $content = file_get_contents($file);
            }
            else {
                // TODO: throw an error
            }
        }
        elseif ( empty($content) ) {
            $content = self::result_tpl; // default
        }
        // Read from between [summarize-posts]in between[/summarize-posts]
        else {
            $content = html_entity_decode($content);
            // fix the quotes back to normal
            $content = str_replace(array('”', '“'), '"', $content );
            $content = str_replace(array('‘', '’'), "'", $content );
        }
        return $content;
    }

Maybe you want to give this some review time, and possibly include it in your next update? Otherwise - I will have to stick with this version or update your next update to suite...

I really like the idea of handing out this short code to the editors and having the template take care of itself. Maybe it's something we could load into the database even from the manager side. But that's for another rainy day. I don't care about it ***THAT*** much.. :)

Regards
David Latham


Viewing all articles
Browse latest Browse all 120178

Trending Articles