Enable Media Replace Shortcode

Below, you’ll find a detailed guide on how to add the Enable Media Replace 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 Enable Media Replace Plugin shortcode not to show or not to work correctly.

Before starting, here is an overview of the Enable Media Replace Plugin and the shortcodes it provides:

Plugin Icon
Enable Media Replace

"Enable Media Replace is a WordPress plugin that allows you to easily replace existing media files in your media library, thus saving you time on re-uploading and re-linking."

★★★★✩ (283) Active Installs: 600000+ Tested with: 6.3.2 PHP Version: 5.6
Included Shortcodes:
  • [file_modified]

Enable Media Replace [file_modified] Shortcode

The ‘file_modified’ shortcode from the Enable Media Replace plugin retrieves the modification date of a specific file. It uses the ‘id’ and ‘format’ attributes to fetch and format the date.

Shortcode: [file_modified]

Parameters

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

  • id – The unique identifier of the attached file
  • format – Controls how the file modification date is displayed

Examples and Usage

Basic example – Displaying the modified date of a file by referencing its ID.

[file_modified id=1 /]

Advanced examples

Displaying the modified date of a file by referencing its ID and using a custom date format.

[file_modified id=1 format="Y-m-d H:i:s" /]

Using the shortcode to display the modified date of a file by referencing its ID. If the file does not exist, it will return false.

[file_modified id=99999 /]

Using the shortcode without any id, it will return false as id is a required parameter.

[file_modified /]

PHP Function Code

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

Shortcode line:

add_shortcode('file_modified', array($this, 'get_modified_date'));

Shortcode PHP function:

function get_modified_date($atts)
    {
        $id=0;
        $format= '';

        extract(shortcode_atts(array(
        'id' => '',
        'format' => get_option('date_format') . " " . get_option('time_format'),
        ), $atts));

        if ($id == '') {
            return false;
        }

        // Get path to file
        $current_file = get_attached_file($id);

        if (! file_exists($current_file)) {
            return false;
        }

      // Get file modification time
        $filetime = filemtime($current_file);

        if (false !== $filetime) {
            // do date conversion
            return date($format, $filetime);
        }

        return false;
    }

Code file location:

enable-media-replace/enable-media-replace/classes/emr-plugin.php

Conclusion

Now that you’ve learned how to embed the Enable Media Replace 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 *