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/
Related
I have this in my index.php file. It adds the home banner image in WordPress. I know that it is mostly generated in WordPress customizer, but I need to add an anchor tag in this section. I can't find it anywhere in the file structure.
<?php do_action('cleanblog_index_top'); ?>
I'm not able to find where cleanblog_index_top leads to. Any help would be great. Thank you!
I stumbled on this old one while looking up the docs for do_action(). The answers are brutal so I decided to provide a better answer in case anyone else stumbles here.
If a WordPress theme has something like do_action( 'example_action_hook_tag' ) somewhere in one of the template files (such as index.php, page.php or whatever) the purpose is to provide theme or plugin authors with a way to write their own custom function that they can then "hook" onto the action with the function add_action().
WordPress would then call this function any time and anywhere do_action( 'example_action_hook_tag' ) is called.
The creators of commercial themes will often litter their template files with hooks declared with do_action() to make it easier for their customers to customize their themes via functions.php or by writing a site-specific plugin.
It looks to me that this is the likely scenario that is impacting the OP. This also explains why the OP was unsuccessful in finding where this "leads to".
It would only "lead somewhere" if the OP wrote a function in the theme/child-theme functions.php or in a plugin and added the line do_action( 'cleanblog_index_top', 'name_of_ops_function' ) to hook their function onto the cleanblog_index_top. WordPress would then call their function when do_action( 'cleanblog_index_top' ) was called in index.php.
From the name of the OP's hook, cleanblog_index_top, it sounds like the theme author intended to provide a way for others to inject output at the top of the index page template.
Suppose the OP wanted <h1>Hello World</h1> to appear there.
In functions.php of a theme/child-theme the OP could add a function that echo's this out:
function op_customization() {
echo '<h1>Hello World</h1>';
}
And hook their function onto cleanblog_index_top:
add_action( 'cleanblog_index_top', 'op_customization' );
Cheers!
You should never edit the index.php file directly, for the same reason you should never edit core Wordpress files directly - the next time WP pushes an update, your changes will be overwritten (and that assumes you don't break anything). Never edit plugin files directly, same reason.
You need to look in your theme, you should only make changes to the functions.php and style.css files in your theme - unless you create a child theme and that is a topic you should Google.
I have a function in my theme functions.php file which returns a value:
function my_theme_function() {
return "100";
}
Anywhere in my theme templates I can simply do this...
echo my_theme_function()
...and I see the number 100 on the page. That's cool.
But in my plugin I would have expected to be able do also get access to this function by echoing my_theme_function() but instead I get a 'call to undefined function' error.
The strangest part is I'm certain this was working a couple of days ago, but I've not touched the code since. I suspect some WordPress shenanigans, but I don't know why or how to get around this.
The reason you may take this result can be the order in which the theme and the plugins are loaded.
For example, your plugin can get loaded before the theme, and obviously, in this case, the function it is not available in your plugin source code.
The solution to this issue are the WordPress Hooks. I don't know what is your plugin code style, but you can bootstrap your plugin in the init hook or even better in the after_setup_theme.
So for example, let's say, you need your plugin should run once your theme is loaded by the WordPress. You can use the following code to do so:
function my_theme_is_loaded() {
// Bootstrap your plugin here
// OR
// try to run your function this way:
if ( function_exists( 'my_theme_function' ) ) {
my_theme_function();
}
}
// You can also try replace the `after_setup_theme` with the
// `init`. I guess it could work in both ways, but whilw your
// plugin rely on the theme code, the following is best option.
add_action( 'after_setup_theme', 'my_theme_is_loaded' );
What the above code does, is like you say to your plugin, wait until the theme is totally loaded, and then try to run my plugin code that rely on the theme code.
And of course, I suggest either wrap your theme function in a plugin function like that:
// This way, your plugin will continue running even if you remove
// your theme, or by mistake your rename the function in the theme
// or even if you totally decide to remove the function at all in the
// side of the theme.
function function_from_theme() {
if ( function_exists( 'my_theme_function' ) ) {
return my_theme_function();
} else {
return 0; // Or a value that is suitable with what you need in your plugin.
}
}
This is going to protect your site against theme de-activation or theme change. In this cases, you are going to have a plugin looking for a function in your theme, and while you change the theme or deactivate your theme, your plugin will break your site.
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.
With Wordpress you have good/bad plugins, some being much more economical than others. By economical I mean that they only call the CSS and JavaScript required for the function when the page is loaded where that function is enabled. Others don't discriminate and call the code on all pages. This can have a negative effect on page load speed/performance.
I have some plugins that are heavy in CSS and are laden with reems of jQuery/Javascript files - I only want them to be enabled on particular pages (not home). In hand I have the page ID and alias. Looking in the plugin folder I also see the main php file that includes all the JS / CSS. Within that file I tried something like is_page() but it seems to have no impact as if is_page has not yet been set.
<?php if ( is_page( '3486' ) ) { exit; } ?>
exit on a line by itself kills the page indicating that the script is being called.
The question, "How and where do you place an if statement that will prevent the plugin CSS/JavaScript from being called on all pages but a particular one (or perhaps an array of pages)?
I could name the plugin but the question is really more generic to any plugin.
You can use wp_deregister_script, this will remove unwanted JS,CSS from specific pages.
add_action( 'wp_print_scripts', 'my_deregister_javascript', 100 );
function my_deregister_javascript()
{
if ( is_page('YOUR PAGE NAME') )
{
wp_deregister_script( 'WORDPRESS JS file NAME' );
}
}
Refer : https://codex.wordpress.org/Function_Reference/wp_deregister_script
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) {}