Failing to check if function_exists
-
Code Snippets of course is the greatest plugin, love it !!!
I’ve noticed with over-riding some theme functions in the Flatsome Theme that Code Snippets is not checking if the function exists. As a result I have to add a
if ( ! function_exists( 'the_function' ) ) { // my overrides }
to all the over-rides in my snippets, developers said Code Snippets should be checking for this.
-
This topic was modified 3 years ago by
mitchellk.
-
This topic was modified 3 years ago by
-
You just need to check if the function exists if you are going to use the same function name (or if you want to use that fact as a condition, of course).
You can add your own code without that check, if you are not in that situation.
?Can you post an override example?
Hi Elias, thanks for a quick response here’s an example snippet. If this is added directly to functions.php without wrapping it inside
if(!function_exists('flatsome_next_post_link_product'))
it works but I have to do as below for it to work in Code Snippets and Dev’s of the theme say it’s unneccesary.if(!function_exists('flatsome_next_post_link_product')) { function flatsome_next_post_link_product() { global $post; $next_post = get_next_post(true,'','product_cat'); if ( is_a( $next_post , 'WP_Post' ) ) { ?> <li class="prod-dropdown has-dropdown"> <a>ID ); ?>" title="View Previous Product" rel="next" class="button icon is-outline circle"> <?php echo get_flatsome_icon('icon-angle-left'); ?> </a> </li> <?php } } } if(!function_exists('flatsome_previous_post_link_product')) { function flatsome_previous_post_link_product() { global $post; $prev_post = get_previous_post(true,'','product_cat'); if ( is_a( $prev_post , 'WP_Post' ) ) { ?> <li class="prod-dropdown has-dropdown"> <a>ID ); ?>" title="View Next Product" rel="next" class="button icon is-outline circle"> <?php echo get_flatsome_icon('icon-angle-right'); ?> </a> </li> <?php } } }
-
This reply was modified 3 years ago by
mitchellk.
I think you are trying, as you said, overriding functions from your theme. But if you check if it does not exist, it’ll never pass because is a function of your theme that do exists.
If the function does not exists, and you know that, you don’t need to check for it. So I don’t get what you’re doing ??
I think the way to modify the way a theme (or plugin) works, is using the hooks. So you can remove some some theme function, and then add yours (with the same or different name). I think that if it not working from the plugin is because of the load order.
Anyway, this plugin just allows you to write code without needing to edit the actual files, so I think it should not do anythink by its own. If I wrote a function that already exists, I prefer to get an error so I realize the cause, instead of just the code not working and not knowing why ???♂?
Make no mistake, I only use Code Snippets on all my sites even though I am advanced in WordPress and PHP, I simply detest having a lengthy uncontrollable functions.php so I always recommend each and every person I assist to use Code Snippets while as I say I use it myself on every site I own.
This started last week with a function given to me by one of the devs of the theme which would not work until I wrapped it in a if(!function_exists.
The function does actually exist which is why he said I should not have to wrap it in and additional if(!function_exists. After had had admin access to my site he confirmed his snippet worked without doing this when placed directly into functions.php
As for me it’s not an issue as it works with the above snippets just perfectly but just thought I would report what the dev said to me. Keep up the great work, I cannot live without your plugin !!!!
Hello @eliasgdj ??
These are the functions (from Flatsome theme php file) that @mitchellk wants to manipulate ?
if(!function_exists('flatsome_next_post_link_product')) { function flatsome_next_post_link_product() { global $post; $next_post = get_next_post(true,'','product_cat'); if ( is_a( $next_post , 'WP_Post' ) ) { ?> <li class="prod-dropdown has-dropdown"> <a href="<?php echo get_the_permalink( $next_post->ID ); ?>" rel="next" class="button icon is-outline circle"> <?php echo get_flatsome_icon('icon-angle-left'); ?> </a> <div class="nav-dropdown"> <a title="<?php echo get_the_title( $next_post->ID ); ?>" href="<?php echo get_the_permalink( $next_post->ID ); ?>"> <?php echo get_the_post_thumbnail($next_post->ID, apply_filters( 'woocommerce_gallery_thumbnail_size', 'woocommerce_gallery_thumbnail' )) ?></a> </div> </li> <?php } } } if(!function_exists('flatsome_previous_post_link_product')) { function flatsome_previous_post_link_product() { global $post; $prev_post = get_previous_post(true,'','product_cat'); if ( is_a( $prev_post , 'WP_Post' ) ) { ?> <li class="prod-dropdown has-dropdown"> <a href="<?php echo get_the_permalink( $prev_post->ID ); ?>" rel="next" class="button icon is-outline circle"> <?php echo get_flatsome_icon('icon-angle-right'); ?> </a> <div class="nav-dropdown"> <a title="<?php echo get_the_title( $prev_post->ID ); ?>" href="<?php echo get_the_permalink( $prev_post->ID ); ?>"> <?php echo get_the_post_thumbnail($prev_post->ID, apply_filters( 'woocommerce_gallery_thumbnail_size', 'woocommerce_gallery_thumbnail' )) ?></a> </div> </li> <?php } } }
As you can see, the original functions from Flatsome are already wrapped in
!function_exists('flatsome_previous_post_link_product')
, which allows users to override them.However, your plugin seems to not recognise the
!function_exists('flatsome_previous_post_link_product')
from the theme file, which is why @mitchellk needs to wrap his own custom code in!function_exists('flatsome_previous_post_link_product')
. This is not good as his custom code can now be overwritten.Removing the custom code from your plugin and placing it (without the
!function_exists('flatsome_previous_post_link_product')
) in function.php, works as it should.I hope I’ve explained the issue clearly.
This is not my plugin.
The theme should use hooks to allow users/developers to modify its behaviour.
As I said, using thefunction_exists
you only achieve to not get an error, but your function is not going to be executed because it is already defined by the theme.This has nothing to do with Code Snippets and I think as for this, Flatsome is not well programmed, because it does not allow you to add a
remove_action
to remove the theme’s function, and then add aadd_action
for your custom function. That is the “WordPress way”.It makes no sense to check for a function that you KNOW is there, and I’m not sure why it works in
functions.php
(I think it’s related with the loading order).Regarding “use hooks”, Flatsome does use hooks and more get added as we go along. Unfortunately, at the moment, there’s no hooks for this case scenario ??
Regarding “Flatsome is not well programmed”, I disagree with you. Yes, improvements can be made but that goes for all themes and plugins.
In any case, you’re missing the point. All we want to know is, why does the custom function not work in the plugin (Code Snippets) but works well in function.php?
If you don’t know why, that’s ok ??
Of course I always referred to this case scenario… You didn’t copied that part: “I think as for this, Flatsome is not well programmed”.
I told you already 2 times: because of the loading order.
I’m missing nothing:
1. The first message only says: “is not checking if the function exists”, and I replied to that.
2. In my second and third message I answer with my opinion on that. Maybe I am not right but of course got the point.Checking the loading order of WordPress, plugins load before the theme so if you define the function on Code Snippets, when the theme tries to define the function it won’t work, because it is already defined in Code Snippets.
So I am testing it locally and if I define the function in
functions.php
it does not work, which makes sense with the load order. I don’t know how it is working for you infunctions.php
.@eliasgdj Thank you for testing ??
I forgot to mention that a child theme is used in this case (Not sure if you used a child theme for your testing), Sorry about that. So define that custom function in Flatsome
child theme > function.php
. I did test again and it worked well when defined inchild theme > function.php
.The functions defined in the parent theme are Pluggable functions, which is why they are wrapped with
if ( !function_exists() )
. The child theme functions are loaded before the parent theme functions, which is why the custom code works, as it should, when defined inchild theme > functions.php
. However, for some reason, if the same custom code is defined in Code Snippets, it does not work.Here is a good example of Pluggable Functions
Please note that the custom function only removes the product “thumbnails” that appear when you hover over the right/left navigation arrows. The custom function does not remove the right/left navigation arrows.
-
This reply was modified 3 years ago by
- The topic ‘Failing to check if function_exists’ is closed to new replies.