I want to apply a separate theme for each user role. I know there is option to do this from the admin side, but I want to do this programmatically.
I found an option using global $custom_theme. I changed my code as
function mymodule_config_preprocess_page(&$variables) {
global $custom_theme;
$custom_theme = 'bluemarine';
init_theme();
}
But it is not affecting the theme .Is this required any modifications?
Pls help me
I think it's too late to change the theme at the point of preprocessing variables, you'll probably want to do this in hook_init():
function mymodule_init() {
global $custom_theme;
$custom_theme = 'bluemarine';
}
As far as I know there's no need to call init_theme() as Drupal will do this for you later on in the process, using the global $custom_theme to decide which theme to use.
The theme is already initialized when your mymodule_config_preprocess_page() is executed. once initialized, the theme cannot be re-initialized.
Looking at the ThemeKey module code, it changes the value of $custom_theme in hook_init(). That's probably the best hook to implement theme change in Drupal 6.
function mymodule_init() {
global $custom_theme;
$custom_theme = 'bluemarine';
}
Related
I'm using "Advanced Scripts plugin" to modify a function of other plugin, the fuction I'm trying to modify is wrappd with if( !function_exists('some_function') ).
the function inside the plugins is like this
if( !function_exists('send-invoice') ){
function send-invoice(){
//The Plugin Invoice
}
}
This is what I did
function send-invoice(){
//My Custom Invoice
}
add_action('init', 'send-invoice');
How can I make sure that my code runs before the plugin codes?
The plugin load before the theme, I tried plugin-loaded hook but nothing changed
You can to use the anonymous function for example:
add_action('init', function() {
//code here
});
More detail is here
Or use another hook muplugins_loaded:
function send-invoice(){
//My Custom Invoice
}
add_action('muplugins_loaded', 'send-invoice');
If nothing else work for you you can create a custom plugin, name it something like "aaamyplugin" and just insert there a single .php file with the function you are trying to override. This is the easiest (not cleanest) way to make sure your code overrides the plugin functions.
The reason for this is because Wordpress plugin loading order is simply alphabetical, that means that everything named before the plugin you are trying to override, get loaded first.
The cleanest way would be to look into the source code of the plugin to understand how it does what it does. Like: when does it load that file that contains the function you are trying to override? That's the important question to answer if you want to go with clean way
I am writing a Wordpress plugin in PHP and I want to be able to use a variable that is not provided in the action hook (of other separate) plugin. I want to pass a variable from one hook to another.
These functions are in my functions.php file (not the theme functions file).
I cannot nest the action hooks because they happen at different times, specifically, the one with the variable happens after the one with where I want it.
I don't think I can use a closure because the variable is inside another hook.
I can't use a class because if I set the class in action 1 then it's not in scope in action 2 and I need to make a new one.
I could use a global or access the DB but I feel like there should be a better way than either of those.
action 1 - my plugin
function foo($level){
do stuff
}
add_action('change-membership', 'foo');
action 2 - my plugin
function bar(){
if($level==0){
do stuff
}
}
add_action('put-in-checkout-text', 'bar');
Other devs plugin
do_action('change_membership', $level);
// a little later
do_action('put_in_checkout_text');
$levels exists at the time I want it - but there is no action hook at that point to grab it.
What I understand is you have something like this:
// Plugin 2, the plugin you are writing.
do_action('put_in_checkout'); // --> bar()
// Plugin 1, another author's plugin
$level = "definition of level";
do_action('change_membership', $level); // --> foo($level)
You say you need to access to $level before the change_membership action happens, so $level may not be defined at that time. Even if it was, it may be in a scope not reachable from put_in_checkout handler functions.
Just in case, check if plugin 2 instanciates a global variable where you have access to the value of $level. Otherwise, you need to take it from the database (if it is stored there) or generate it somehow.
I want to add some HTML code to a plugin but I want to do that in a child theme, the function that I wanna override it is in a file named as admin-functions.php
I want to add extra buttons to booked_render_custom_fields function
admin-functions.php:
function booked_render_custom_fields($calendar = false) { ?>
<button class="button">Text</button>;
<?php }
any suggestion? thanks
you can't really override a function like that. you can only use the filters and hooks that the plugin implements. but you got some options:
a) make a copy of the plugin, change it's name and make the changes you need. after that, it wont get updates anymore. you'll have to implement them manually.
b) you could add just one line to the original plugin and define a filter or hook to which you subscribe in your functions.php . so you only have to fix this one line after updating the plugin.
c) since the newer versions of wordpress, you have the possibilites to run your own code after any shortcode. so you could add or inject your own html into the output of the shortcode. https://developer.wordpress.org/reference/hooks/do_shortcode_tag/
Trying to fetch output in A.tpl but not getting any output. I think i'm doing something wrong to call php function in tpl file.
A.tpl
{myModifier}
B.php
class Geolocation{
public function sm_loc($params, Smarty_Internal_Template $template)
{
return "100.70";
}
}
$smarty1 = new Smarty();
$smarty1->registerPlugin('modifier', 'myModifier', array('Geolocation', 'sm_loc'));
I 've already used this code. And this doesn't seem to work. It also breaks my existing working code in A.tpl post this use.
My Need here is to get output from the php function in A.tpl from an external php file.
Thanks in Advance. Sorry for being noob.
To add this modifier to Smarty and use it in your template, you're best to use a WHMCS hook.
If you create a new PHP file under the '~/includes/hooks/' directory (you can name it anything - in this case, let's use 'myhook.php'), WHMCS will automatically inject this hook on each request.
For this, you're going to want to use the ClientAreaPage hook. Inside your hook, you can then access the global $smarty variable.
Example:
function MySmartyModifierHook(array $vars) {
global $smarty;
// I recommend putting your Geolocation class in a separate PHP file,
// and using 'include()' here instead.
class Geolocation{
public function sm_loc($params, Smarty_Internal_Template $template) {
return "100.70";
}
}
// Register the Smarty plugin
$smarty->registerPlugin('modifier', 'myModifier', array('Geolocation', 'sm_loc'));
}
// Assign the hook
add_hook('ClientAreaPage', 1, 'MySmartyModifierHook');
That should do the trick. If you want to explore with other hooks, you can take a look at the Hook Index in the WHMCS documentation.
The function names in each of your hook files must be unique.
As a side note, if you're only wanting to run this hook on specific pages, you can check the templatefile key in the passed $vars array. For example, say you only wanted this hook to run on the 'View Cart' page on the order form:
function MySmartyModifierHook(array $vars) {
global $smarty;
// If the current template is not 'viewcart', then return
if ($vars['templatefile'] != 'viewcart')
return;
// ... your code here ...
}
Also, note that with hooks like the 'ClientAreaPage' hook, returning an array of keys and values will automatically add them as Smarty variables. So if your hook function ended with return ['currentTime' => time()];, you could then use {$currentTime} in your Smarty template to output its value.
I need to change the value of a function from a WordPress plugin that I'm using.
If I do it in the original plugin file, it works perfectly, but I'd rather make it in file functions.php to avoid problems on plugin update.
I need to change this:
public static function loop_shop_per_page() {
return get_option( 'posts_per_page' );
}
Into this:
public static function loop_shop_per_page() {
return '-1';
}
How would I go to create a filter in file functions.php to do just that?
Without changing the core plugin code, I don't think you can achieve what you want.
Well written plugins usually have the apply_filters method called on anything that might need to be tweaked by a theme developer. As such, the plugin code should look something like this:
// Using apply_filters() to allow overriding of values...
public static function loop_shop_per_page() {
$value = get_option( 'posts_per_page' );
return apply_filters('filter_loop_shop_per_page', $value);
}
Then, in your functions.php file, you'd be able to change the value on for that by adding a filter like so:
add_filter('filter_loop_shop_per_page', 'mytheme_loop_shop_per_page');
function mytheme_loop_shop_per_page($value){
return -1;
}
Unfortunately, unless you edit the plugin code, you probably won't be able to do what you want. You could always search the plugin for apply_filters to see if there's anything else you might be able to tweak to achieve the same results.