Support » Plugin: Freesoul Deactivate Plugins - Plugin manager and cleanup » PHP code snippets Critical Error

  • Resolved jaredpooley

    (@jaredpooley)


    I use a PHP code snippet to display a download link for the ACF plugin, however when I activate this plugin i get a critical error. The backend page shows the following message:

    “INSERT PHP CODE SNIPPET caused a fatal error and has been deactivated in the FDP backend pages.”

    The error specifically reads:

    Uncaught Error: Call to undefined function get_field() in wp-content/plugins/insert-php-code-snippet/shortcode-handler.php(99) : eval()'d code:2 Stack trace: #0 wp-content/plugins/insert-php-code-snippet/shortcode-handler.php(99): eval() #1 wp-includes/shortcodes.php(355): xyz_ips_display_content(Array, '', 'xyz-ips') #2 [internal function]: do_shortcode_tag(Array) #3 wp-includes/shortcodes.php(227): preg_replace_callback('/\\[(\\[?)(xyz\\-i...', 'do_shortcode_ta...', '[xyz-ips snippe...') #4 wp-content/plugins/elementor-pro/modules/dynamic-tags/tags/shortcode.php(54): do_shortcode('[xyz-ips snippe...') #5 wp-content/plugins/elementor/core/dynamic-tags/tag.php(35): ElementorPro\Modules\DynamicTags\Tags\Shortcode->render() #6 /home/otterbhouse/staging

    Just if it is any interest, the snippet in question is the following:

    <?php
    $file = get_field('menu_file', 402);
    if( $file ): ?>
        <?php echo $file['url']; ?>
    <?php endif; ?>

    I have had to deactivate the plugin so the functionality on my site still works. Is there anyway to stop it checking for errors/ even leave this page alone completely as I can assure I have absolutely no errors when I don’t have this plugin deactivated.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Jose

    (@giuse)

    Hi @jaredpooley

    the function get_field is defined in the ACF plugin.
    Where you call that function the ACF plugin must not be deactivated, in another case that function doesn’t exist, and you call something that doesn’t exist, hence you have a fatal error.

    When you use a function that doesn’t always exist you should first check if that function exist. Your code should be something like this:

    <?php 
    $file = function_exists( 'get_field' ) ? get_field('menu_file', 402) : false; 
    if( $file ) echo $file['url'];
    ?>

    The code above will not generate fatal errors where ACF is not active, however, it will not print the file URL when ACF is not active.

    I suggest you modify your code, but most of all, be sure ACF is always active where you call that code.

    The issue. is probably not caused by a bug but by the fact that you asked FDP to disable ACF also where you need it.

    I hope it helps.

    Best regards

    Jose

    • This reply was modified 6 months, 3 weeks ago by Jose.
    • This reply was modified 6 months, 3 weeks ago by Jose.
    • This reply was modified 6 months, 3 weeks ago by Jose.
    Thread Starter jaredpooley

    (@jaredpooley)

    Legend!

    That makes so much sense and is working just fine now I have enabled ACF to be active on that page.

    Thank You!

    • This reply was modified 6 months, 3 weeks ago by jaredpooley.
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘PHP code snippets Critical Error’ is closed to new replies.