• Resolved djfenske

    (@djfenske)


    I’ve added a custom shortcode to provide a specific date format to be populated in the CF7 field, it works great. However, whenever this plugin updates gets updated, it overwrites my edited shortcodes.php file and I have to restore it from backup. Is there a way to avoid this, like how you can add custom code to a child theme, or the functions.php file for other applications?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Tessa (they/them), AuRise Creative

    (@tessawatkinsllc)

    In general, it’s bad practice to edit plugins for this exact reason. When the files get updated in the SVN and your install pulls those files, it will overwrite what you have.

    I do very much like your idea though, perhaps I could create a template structure that could automatically include a specific PHP file for users to create their custom shortcodes.

    In the mean time, you can absolutely move your custom functions in to your child theme’s functions.php.

    Plugin Author Tessa (they/them), AuRise Creative

    (@tessawatkinsllc)

    I’ve added this capability in version 4.1.0!

    Method 1: use your theme

    If you’re using a custom or child theme, you can certainly add your custom code there and it won’t get overwritten when this plugin is updated.

    Method 2: use a plugin

    Alternatively, if you’re not super code-savvy (more for other people who land on this thread that might not be), you can check out this Code Snippets plugin. I’ve never used it but it seems to be highly rated and recommended.

    Method 3: create the dtx.php file

    In version 4.1.0, I introduced a feature that looks for a dtx.php file in three (3) locations:

    1. the wp-content directory (e.g. file path C:\path\to\website\wp-content/dtx.php or URL example.com/wp-content/dtx.php)
    2. the directory of your currently active theme (e.g. file path C:\path\to\website\wp-content/themes/my-theme/dtx.php or URL example.com/wp-content/themes/my-theme/dtx.php)
    3. the parent directory of your current active theme (e.g. file path C:\path\to\website\wp-content/themes/parent-theme/dtx.php or URL example.com/wp-content/themes/parent-theme/dtx.php)

    And calls them using include_once should they exist. Does nothing if they don’t. So you can create that file, modify it however you see fit, and upload it via FTP to be executed.

    To speed up your custom shortcode creation, you’re welcome to call any of the functions defined in DTX’s shortcodes.php and utilities.php directly such as wpcf7dtx_post, wpcf7dtx_get_post_var, or wpcf7dtx_format_atts, that way you don’t have to always define functions for getting variables from things, you can just call the appropriate function and get right to it, outputting what you need your custom shortcode to output.

    I definitely second the “use a plugin” suggestion.

    I wrote about creating a CF7DTX shortcode back in 2019 (it’s really just a regular shortcode). https://www.damiencarbery.com/2019/11/shortcode-for-contact-form-7-dynamic-text-extension/

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to avoid custom shortcodes being overwritten on updates’ is closed to new replies.