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.
Related
i want to hide few plugins style sheets to reduce load on our Index page and categories pages. Actually we want to display plugin style sheet only on Post not on other pages.
we have used following code in plugin, but it doesn't work. please help how to use it.
if( is_single() || is_singular('post') ) wp_enqueue_style('savrix-style.css');
If you are modifying your own plugin I see no reason your code wouldn't work. The is_single() condition is not needed, and will result in the stylesheet being loaded on custom post types and other singles that you don't intend.
However your wp_enqueue_style call is incomplete, so unless you have a wp_register_style call somewhere else defining the handle and URL of the stylesheet you need to change it to something along these lines:
if (is_singular('post')) {
wp_enqueue_style('savrix-style', plugins_url('savrix-style.css', __FILE__);
}
However, I get the impression that you are actually trying to remove a stylesheet included by a thirdparty plugin. It is generally a bad idea to modify a third-party plugin, as your modifications will be lost on the next update... it is very difficult to maintain that sort of modifications in the long run.
Instead make a new plugin and modify whatever you need from there.
What you want to achieve can be accomplished by:
Create a new folder in the wp-content/plugins folder, fx. my_load_reducer.
Inside that folder create a new file called my_load_reducer.php
Paste this in the file:
<?php
/*
Plugin Name: My Load Reducer
Description: Removes unneeded and unwanted stylesheets from other plugins
Version: 0.1
*/
//Use a class to avoid conflicts
class my_load_reducer {
function __construct() {
//Hook into wp_enqueue_scripts with a high priority
add_action( 'wp_enqueue_scripts', array($this, 'deregister_styles'), 1000 );
}
function deregister_styles() {
//Check that current post is not a single post
if (!is_singular('post')) {
//deregister the stylesheet - this removes the twentyfifteen
//main stylesheet - obviously you need to substitute the handle
//of the stylesheet you actually want to remove
wp_deregister_style( 'twentyfifteen-style' );
}
}
}
//Instantiate the class
$my_load_reducer = new my_load_reducer();
Activate the plugin through the wordpress admin.
You can remove perticular plugin css on selected page.
below code is remove plugin css to other pages and display only on post pages:
/*disable loading plugin css to page and load on post page*/
add_action('wp_print_styles', 'my_deregister_styles', 99999);
function my_deregister_styles()
{
if(!is_single())
{
wp_dequeue_style('plugin-css-handle');
wp_deregister_style('plugin-css-handle');
}
}
where 'plugin-css-handle' is perticular plugin's css handle which you want to remove.
I need to know how to take the output of a custom built Wordpress plugin and output it onto a specific page.
I know that I need to use the add_action() / add_filter() functions to call the function which outputs the plugins output when a wordpress hook function runs.
Currently I am using the 'the_content' hook.
This outputs my plugins output to all pages in my theme which call the the_content() function.
Which hook can I use to make the output only appear on a specific page.
Also
It would be useful to know how to create a page using my plugin.
Checkout is_page()
if (is_page(123)) {
// put your hooks here
}
put your code in a file inside plugin directory
then
use this
function page_directory()
{
if(is_page('123')){
$dir = plugin_dir_path( __FILE__ );
include($dir."custom/page.php");
die();
}
}
add_action( 'wp', 'page_directory' );
I want to modify/overwrite functions written in woocommerce-functions.php file but I don't want to modify woocommerce-functions.php file. That is I want to achieve this in plug-in or in my theme.
It is possible to override woocommerce functions, I did this recently and added all of my woocommerce extended functionality to my theme's functions.php file so that the woocommerce plugin files remained untouched and are safe to update.
This page gives an example of how you can remove their action and replace it with your own -
http://wordpress.org/support/topic/overriding-woocommerce_process_registration-in-child-theme-functionsphp
This page gives an example of extending upon their functions without removing their function, as well as using child themes -
http://uploadwp.com/customizing-the-woocommerce-checkout-page/
Hope this helps :)
WooCommerce provides a templating system. It is possible to override woocommerce functions. great way to customize WooCommerce without modifying core files, is to use hooks -
If you use a hook to add or manipulate code, you can add your custom code to your theme functions.php file.
Using action hooks -
To execute your own code, you hook in by using the action hook do_action(‘action_name’);.
See below for a great example on where to place your code:
add_action('action_name', 'your_function_name');
function your_function_name()
{
// Your code
}
Using filter hooks-
Filter hooks are called throughout are code using apply_filter(‘filter_name’, $variable);
To manipulate the passed variable, you can do something like the following:
add_filter('filter_name', 'your_function_name');
function your_function_name( $variable )
{
// Your code
return $variable;
}
Here you can get WooCommerce Action and Filter Hook - https://docs.woothemes.com/wc-apidocs/hook-docs.html
If you have a child theme, you can copy the relevant file to your theme and rewrite the copy. The copy will be used in preference to the WooCommerce version.
I was needed to add "Play" button for videos on mobile devices (by default this button is shown just on desktop).
I was needed to override the function in wp-content/themes/gon/framework/theme_functions.php:
function ts_template_single_product_video_button(){
if( wp_is_mobile() ){
return;
}
global $product;
$video_url = get_post_meta($product->id, 'ts_prod_video_url', true);
if( !empty($video_url) ){
$ajax_url = admin_url('admin-ajax.php', is_ssl()?'https':'http').'?ajax=true&action=load_product_video&product_id='.$product->id;
echo '<a class="ts-product-video-button" href="'.esc_url($ajax_url).'"></a>';
}
}
I found this instruction which states If you use a hook to add or manipulate code, you can add your custom code to your theme’s functions.php file.
I already had wp-content/themes/gon-child/functions.php, (ie the original gon theme had been copied to gon-child), so what I did was:
// Enable tour video on mobile devices
remove_action('ts_before_product_image', 'ts_template_single_product_video_button', 1);
add_action('ts_before_product_image', 'ts_template_single_product_video_button_w_mobile', 1);
function ts_template_single_product_video_button_w_mobile(){
global $product;
$video_url = get_post_meta($product->id, 'ts_prod_video_url', true);
if( !empty($video_url) ){
$ajax_url = admin_url('admin-ajax.php', is_ssl()?'https':'http').'?ajax=true&action=load_product_video&product_id='.$product->id;
echo '<a class="ts-product-video-button" href="'.esc_url($ajax_url).'"></a>';
}
}
?>
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/
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