Docxpresso Shortcode

Below, you’ll find a detailed guide on how to add the Docxpresso Shortcode to your WordPress website, including its parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the Docxpresso Plugin shortcode not to show or not to work correctly.

Before starting, here is an overview of the Docxpresso Plugin and the shortcodes it provides:

Plugin Icon
Docxpresso

"Docxpresso is a highly efficient WordPress plugin that simplifies the creation, modification, and management of online documents within your website. Streamline your content workflow with Docxpresso."

★★★★☆ (31) Active Installs: 3000+ Tested with: 5.8.8 PHP Version: false
Included Shortcodes:
  • [docxpresso]

Docxpresso [docxpresso] Shortcode

The Docxpresso shortcode is a versatile tool that allows for dynamic content creation. This shortcode, add_shortcode(‘docxpresso’, ‘docxpresso_call’), enables users to embed files, sort tables, and even handle comments within their WordPress site. It works by checking for certain attributes in the shortcode call, such as ‘file’, ‘comments’, ‘svg’, ‘remote’, ‘sortTables’, and ‘sortNumberFormat’. These attributes allow for customization of the output. For instance, ‘file’ specifies the document to be embedded, ‘comments’ toggles the comment parsing, and ‘svg’ sets SVG parsing. .

Shortcode: [docxpresso]

Parameters

Here is a list of all possible docxpresso shortcode parameters and attributes:

  • file – specifies the path of the document to be processed.
  • comments – determines whether comments in the document are parsed.
  • svg – controls whether SVG graphics in the document are processed.
  • remote – indicates whether the document is located remotely or locally.
  • sortTables – decides whether tables in the document are sorted.
  • sortNumberFormat – sets the number format for sorting.

Examples and Usage

Basic example – Displaying a document using the file attribute in the shortcode.

[docxpresso file="https://example.com/myfile.odt" /]

Advanced examples

Displaying a document with comments enabled. This will parse the comments within the document and display them alongside the content.

[docxpresso file="https://example.com/myfile.odt" comments="true" /]

Displaying a document with SVG images disabled. This will prevent the SVG images from being rendered within the document.

[docxpresso file="https://example.com/myfile.odt" svg="false" /]

Displaying a document from a remote source. This will copy the document from the remote source to a temporary location on the server before rendering it.

[docxpresso file="https://example.com/myfile.odt" remote="true" /]

Displaying a document with custom sorting options for tables. This will sort the tables within the document according to the specified format.

[docxpresso file="https://example.com/myfile.odt" sortTables="true" sortNumberFormat=",.0" /]

PHP Function Code

In case you have difficulties debugging what causing issues with [docxpresso] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode('docxpresso', 'docxpresso_call');

Shortcode PHP function:

                    function docxpresso_call($attrs, $content = null) {
    $options = get_option('docxpresso', array());
    $buffer = '';
    if (isset($attrs['file'])) {
        if (isset($attrs['comments'])) {
            $comments = $attrs['comments'];
        } else {
            $comments = false;
        }
        if (isset($attrs['svg']) && $attrs['svg'] == 'false') {
            $SVG = false;
        } else {
            $SVG = true;
        }
        if (isset($attrs['remote']) && $attrs['remote'] == 'true') {
            $remote = true;
        } else if (isset($attrs['remote'])) {
            $remote = false;
	    } else {
            if (isset($options['remote']) && $options['remote']){
                $remote = true;
            } else {
                $remote = false;
            }
        }
        if (isset($attrs['sortTables'])) {
            $sortTables = $attrs['sortTables'];
        } else {
            $sortTables = true;
        }
        if (isset($attrs['sortNumberFormat'])) {
            $sortNumberFormat = $attrs['sortNumberFormat'];
        } else if (isset($options['sortNumberFormat'])){
            $sortNumberFormat = $options['sortNumberFormat'];
        } else {
            $sortNumberFormat = ',.';
        }
       $file = strip_tags($attrs['file']);
        if ($remote) {
            $unid = uniqid() . rand (999, 9999);
            $newfile = '/tmp/tmp_doc' . $unid . '.odt';
            $fullPath = dirname(__FILE__) . $newfile;
            copy($file, $fullPath);
        } else {
            $path = parse_url($file, PHP_URL_PATH);
            $fullPath = $_SERVER['DOCUMENT_ROOT'] . $path;
            if (!file_exists($fullPath)) {
                //this is a hack for servers that do not return the correct
                //$_SERVER['DOCUMENT_ROOT'] value
                $tempArray = explode('wp-content', $path);
                $fullPath = getcwd() . '/wp-content' . $tempArray[1];
            }
        }
        
        require_once 'CreateDocument.inc';

        $doc = new Docxpresso\createDocument(array('template' => $fullPath));
        $html = $doc->ODF2HTML5('test.html', array('format' => 'single-file', 
                                                   'download' => true, 
                                                   'parseLayout' => false, 
                                                   'parseComments' => $comments,
                                                   'parseSVG' => $SVG,
                                                   'sortNumberFormat' => $sortNumberFormat,
                                                   'sortTables' => $sortTables));
        $buffer = $html;
        unset($doc);
        if ($remote) {
            unlink($fullPath);
        }
    }
    return $buffer;
}
                    

Code file location:

docxpresso/docxpresso/plugin.php

Conclusion

Now that you’ve learned how to embed the Docxpresso Plugin shortcode, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.

Comments

Leave a Reply

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