WordPress: Check if plugin is installed (ACF) - php

I want to prevent fatal error in my theme if the ACF plugin is deactivated or not installed.
The main function of the plugin is get_field().
I wrote this code in my functions.php to check:
if ( !function_exists('get_field') ) {
function get_field() {
echo '<span>plugin ACF is not installed</span>';
}
}
Please tell me this is acceptable practice?

ACF itself uses a check to see if the framework has been loaded. If it has already been included and invoked by another plugin or theme, then ACF won't re-instantiate its own class again. It does this with a class check:
if (!class_exists('ACF')) {
// The ACF class doesn't exist, so you can probably redefine your functions here
}
I use exactly this in my own plugins that rely on the presence of ACF so that if it happens to get deactivated, the whole site doesn't bomb out.

First of all, this is not the main plugin function, just one of them. Probably, most commonly used by a plugin user in a theme. Another one is the_field(), which actually prints value (get_field() returns it).
Regarding practice of defining your custom function - it's fine. However, I would not print that long message in every place where ACF field is expected - some of them may be short (numbers), and this message will break the layout. Printing something shorter is better, imo.
Also, function_exists is proper check, not is_plugin_active, because ACF can also be shipped as a library with a theme framework or other plugin.
Another option is to remove ACF dependency on the frontend completely. You can output the contents of the fields with get_post_meta() and prevent ACF plugin from loading on the frontend entirely. See these two posts for details:
http://www.billerickson.net/code/disable-acf-frontend/
http://www.billerickson.net/advanced-custom-fields-frontend-dependency/

There is a wordpress function for that:
is_plugin_active('advanced-custom-fields/acf.php');
Note: You might run into issues when changing to the premium version of a plugins.

Yes, it's a good way to check if the plugin function exists.
You can also try is_plugin_active function to check if the plugin is activated, because the function can be redeclared somewhere.
I think the main reason you are doing that is to prevent Fatal Errors, so it doesn't matter which way you can use.

Related

Wordpress plugin that overrides function in wp-includes file

This is my first time attempting to make a Wordpress plugin. I would like to insert three lines of code into the post-template.php file under wp-includes. I am thinking I will need a way to override the function, in this case, get_the_content, so that it calls my plugin's version of the function instead of the default one.
Based on what I've searched, it is not a good idea to override the core files so I'm hoping there is a simple way to modify the function through my plugin.
What would be the best method to go about doing so?
You could overwrite it using the same function name inside your plugin, also you could create a child theme and inside that you can copy the file post-template.php so it will load the edited version instead without updating issues, or it may be possible (if its plugable) to overwrite or edit it using a hook, check out this answer: https://stackoverflow.com/a/17202451/7862006
You got that right. Never modify the core files directly for these reasons. However, luckily there are different ways to achieve what you want in WordPress. The simplest would be to add a filter in functions.php (if you are using a theme find the functions.php inside ../themes/..) like this:
add_filter('the_content', 'new_content_filter');
function new_content_filter( $content )
{
$new_content = 'hello';
return $new_content;
}
This will get the content and apply a new filter on it.

Wordpress customizer change sanitize_callback for setting doesn't have any effect

My problem:
I have replaced the color input in the customizer with an input that supports the alpha channel. The sanitization function from Wordpress is only for hex colors but I get rgba() colors. I wrote a sanitization function that works perfectly for any new control I add to the customizer but if I replace an existing one and change the sanitize_callback parameter of the corresponding setting to my own function ($wp_customize->get_setting("background_color")->sanitize_callback = "slug_sanitize_color";) Wordpress still uses its standard sanitize_hex_color. The output of var_dump($wp_customize->get_setting("background_color")->sanitize_callback); is string(19) "slug_sanitize_color" so I guess it should work. If more code is needed I can provide it.
My question:
What do I have to do to make Wordpress use my sanitization function for a preexisting control instead of the one Wordpress ships with?
Addition: It all happens inside a function hooked to customize_register
I found the solution myself.
If you want to change the sanitize_callback, sanitize_js_callback or validate_callback you have to manually unregister the old callback function (remove_filter("customize_sanitize_{$settingid}",$wp_customize->get_setting($settingid)->sanitize_callback);), then change the value for the object ($wp_customize->get_setting($settingid)->sanitize_callback = "my_custom_filter_function";) and finally register the new filter function (add_filter("customize_sanitize_{$settingid}",my_custom_filter_function,10,2);).

Checking for buddypress from a theme

How can I check for buddypress from a theme? I've found this page for BP Plugin development
but this action never gets loaded if I hook from a theme. Why does it not work?
P.S. I need call some BP's functions from a theme, like: Show "BP's Activity Stream" at specific places.
What I mean with "never gets loaded" is:
(File in subdirectory of theme, and included in functions.php)
function sometestfunction() {
exit();
}
add_action ('bp_include', 'sometestfunction');
This must make wordpress show a blank page, won't ?
Simple check if function 'bp_is_active' is defined:
if ( function_exists('bp_is_active') ) {
// do something here...
}
see shanebp answer and comments.
this action never gets loaded
You mean never gets called?
Not sure why that would be, since you don't provide any code.
But it's meant for loading plugins.
If BP is installed, it is loaded by the time theme templates are parsed.
Have you tried calling BP functions in your theme?
I've never had a problem doing that.
Put your function(s) in one of these places:
a plugin
theme/functions.php
bp-custom.php
http://codex.buddypress.org/plugindev/bp-custom-php/
If your code will be used on installs you don't control, you should check for a BuddyPress component:
if( bp_is_active( 'activity' ) {
http://codex.buddypress.org/developer/bp-is-active/

Wordpress basic concept - how to extend/override/customize a plugin hook?

I am working on a wordpress site and would like to clarify a basic concept that is definitely very important, and this is how to customize/extend a wordpress hook (at least that's what I think I want to do!)
As the real world example, I am setting up a wp-ecommerce site. When a user adds an item to the cart, I would like to do one or two more things than the original function does. Looking through the source, I find:
/wp-content/plugins/wp-e-commerce/wpsc-includes/ajax.functions.php
with the function:
function wpsc_add_to_cart()
I know I could simply edit the code right here, but obviously that is the completely wrong way to go about it as when the plugin is updated, I will lose changes. What is the correct way to extend a function that is part of a plugin, or wordpress for that matter?
Endless thanks in advance.
You can use the wordpress action hooks to resolve the code loss while plugin upgrade.
You can remove the function which is in plugin file by using remove_action hook and do your own code by adding add_action in your function.php file. So that you can customize your plugin code from theme's function.php.
Here are the examples to explain.I hope it will help.
http://codex.wordpress.org/Plugin_API
http://themeshaper.com/2009/05/03/filters-wordpress-child-themes/
I use a little supressed notice function (it lives in my child themes function.php page), for plugins that get irritating eg: please setup twitter account to use , this kind of warning is not useful at certain stages and sometimes just do not care for it.
function supressed_notices_active(){
echo '<div class="error"><p>Supressed Notices are active</p></div>';
}
if(function_exists('the_plugin_custom_function_call')){
remove_action('the_plugin_custom_function_call' );
add_action('admin_notices','supressed_notices_active');
}else{
function test_message_from_me(){
echo '<h1>show</h1>';
}
add_action('admin_notices','test_message_from_me');
}
So I create the supressed notice function to at least create a warning, so i remember.
Check if the target function exists with the function_exists($target_function) hook
then remove this action from running with the remove_action($tag,$target_function) hook
then just add your custom function with the add_action($tag,$target_function) hook (do not need to have a separate function this could just be a closure)
then else if the function does not exist either still run a new action or leave this section, it can be useful for testing to just add anything so you atleast get some feed back.
What you could try... Copy the function within the plugin file,
paste it into your themes functions.php file,
ie:
function wpsc_add_to_cart() {
global $wpdb, $wpsc_cart;
// default values etc..etc..
// new code here?
}
the only thing with this is, if the plugin is updated and that funciton is renamed or removed, changed or something you could start to run into trouble...
could you not ask the plugin developer to possibly add your requirements to it,
possibly for a small fee? but if your using it as your main shopping cart, then chances are, that small investment could be a good thing.
Marty

How to theme Ubercart Checkout Complete page

I want to theme the page /cart/checkout/complete
I already saw the settings in admin/store/settings/checkout/settings, but they are not enough.
I want to add some HTML i.e. add a print button at the top of the page.
I would like to have a .tpl.php file to use as template, or otherwise, using an alternate checkout page, how to insert the texts defined in checkout settings.
I tried to make a uc_cart_complete_sale.tpl.php but it isn't called.
Thank you in advance.
According to the Template Suggestion documentation you can provide a custom page.tpl.php for absolutely any path, so a template file with the following name would override page.tpl.php for the path cart/checkout/complete:
page--cart--checkout--complete.tpl.php
Be sure to clear Drupal's cache once you've create the file so the changes are picked up in the theme registry.
After hard work, i found the template page.
It is:
page--cart--checkout--complete.tpl.php
remember to clear the cache
firstly, you should probably check this page: admin/store/settings/checkout/edit/messages
there you can customize a header for the message displayed when the checkout completes.
other than that, you can implement some functions to alter this page. from a short look in the ubercart api maybe this function will do: my_module_checkout_complete() in this link the guy says it worked
another function that should work is theme_uc_cart_complete_sale
there are other options, such as in your template.php check if this is /checkout/complete and do whatever you want. like this:
if (arg(0) == 'cart' && arg(1) == 'checkout' && arg(2) == 'complete')
and than redirect to your page. anyway, there are plenty of ways to accomplish this, but just naming a file 'uc_cart_complete_sale.tpl.php' won't work. sorry...
In D6 at least, you can theme the message by overriding theme_uc_cart_complete_sale() - so if that's what you're after, theme the message by overriding that in your theme (for example, function mytheme_uc_cart_complete_sale($message, $order) {}

Categories