Where to call the function ..? - php

I am new to plugin development/editing with Wordpress. I am using a famous plugin called Polylang for multilingual support.
And I am using another plugin called Clean and Simple contact form for creating an AJAX based contact form.
Now, looking into the documentation for Polylang I have found the following function:
Register String
Allows plugins to add their own strings in the “strings translation” panel. The function must be called on admin side (the functions.php file is OK for themes).
Usage:
pll_register_string($name, $string, $multiline);
As it says, function must be called on admin side. I am unable to figure out what that means ...
I intend to add a few custom strings to the strings translation tab. Calling this function from functions.php in the theme worked but didn't translate strings from the plugin. The plugin doesn't yet support Japanese so I am having to take this path.
Can you tell me where exactly am I supposed to call this function. I tried to call it in class.view.php and the file with the name: clean-and-simple-contact-form-by-meg-nicholas.php
But, both ended up giving a fatal error saying that the function is undefined. What's the exact meaning of "The function must be called on admin side of a plugin..."
Thanks and Regards !

You might need to adjust your ajax plugin, so the function gets called in the admin. (I don't really know the ajax plugin but I am sure that there is some configuration part, which will be called somewhere in the admin panel)
Another way would be to create another plugin, which is just taking care of that function call. Just be sure, that this new plugin has some link you can call from the admin, so it is run in the right userspace.

Related

Add an element to a hook with a FrontController

I am currently developing a prestashop module where the user can add as many visual elements (an accordion) as he wishes. I finished the configuration of the back office with my own database, everything is ok on that side.
The problem comes from the fact that depending on the hook chosen by the user (displayHome or a custom hook) the visual element will be added to it.
During my testing phases, I managed to display the corresponding visual rendering on the displayHome hook, via this method in my main file :
public function hookDisplayHome($params)
{
$this->context->smarty->assign([
//...
]);
return $this->display(__FILE__, 'my_file.tpl');
}
The prestashop documentation for the FrontController explains how to configure it for visual rendering on a custom page, but nothing related to hooks.
I tried a similar process with the initContent() method but it doesn't seem to work.
I think that if() will be enough to assign the variables to different places according to the choice of the user. But if I can't just display it, I won't be able to think about it anyway.
How to interact with hooks via the FrontController ?

Where should WordPress pre_cache_alloptions filters be registered?

I have customized some wp-option return values with the dynamic pre_option_{option} hook and for completeness would like to also override these option values when they are returned via wp_load_alloptions(). This function has a hook that is available to allow for this customization: pre_cache_alloptions.
However, although the hook exists there doesn't appear to be any way to call add_filter("pre_cache_alloptions", ...) early enough for it to take effect before the first call to wp_load_alloptions(). Since wp_load_alloptions() is called very early when loading WordPress, and since the values are cached on first-call, I haven't been able to register the hook in time for it to take effect. I have tried calling add_filter("pre_cache_alloptions", ...) during a mu-plugin load and also in a custom theme functions.php file, but neither of those places appear to hook in early enough for this to work.
The only solution I have found so far is to hack the actual wp-includes\plugin.php file and add in the add_filter("pre_cache_alloptions", ...) call right after the require of class-wp-hook.php at the top. This does work as expected when registered at that code point... but I'm searching for a way to register this filter so that it works without altering the core WordPress code.
Since this hook must be registered after the add_filter() function is declared in plugin.php, but before the first call to wp_load_alloptions(), the only user-customizable code point that appears to work is the optional custom db.php file, which is loaded in wp-settings via require_wp_db(). Adding a custom wp-content/db.php file with the necessary add_filter("pre_cache_alloptions", ...) reference at the top works as a means to register this customization in time for it to be useful. After much searching, this appears to be the only way to handle this without mucking with the core itself.

How to add custom class to functions in wordpress

I have a wordpress website on localhost, and I want an add to cart button.
I've used a wordpress function for the same, but I need design that of what I've made.
The function I've used is:
print_wp_cart_button_for_product()
I just wanted to know how do I add class to the button which is generated from the above function.
Thanks
I don't have the correct solution to your problem, but what you could do is inspect which classes/id there are already added (in your browser > inpect).
You could define these classes in your CSS and override them with your own styling.

How to setup a hook with a view in Drupal 7?

So I have a view called "export-spreadsheets". When an admin goes to this page, I want a function to be called so that it basically runs a long SQL query and exports its results to a CSV.
I was wondering where I would add this function to be called?
Thanks.
Make a custom module, add on to the *_menu() hook, make it call the function when you go to a certain url. Have it output the .csv file and die()

register_activation_hook vs add_action('init')

I'm trying to determine why some WordPress plugins use register_activation_hook(__FILE__, 'activate_plugin') while others use the action add_action('init', 'activate_plugin');
The two do different things, register_activation_hook is used to register a function which will be called once when the plug-in is activated (on the Wordpress plug-in management page), whereas functions hooked into the init action will be called on every request.
So, common examples would be to use an activation function to create database tables, or set default options for a plug-in and then an init action function to load translated strings.
A few reasons:
register_activation_hook is WP2+, add_action was available to use before that
register_activation_hook allows the developer to specify the file the function resides in (although this seems rarely used)
for me, register_activation_hook is 'cleaner'
So I'd bet that plugins using add_action date from before version 2 or the developer isn't aware of register_activation_hook
Hooking an "activate_plugin" function to init looks to either be code done a long time ago, or by someone who doesn't know about register_activation_hook. A third possibility is that despite the function name they want it to run whether or not register_activation_hook is called.
For example, when updating a plugin, the plugin is deactivated and reactivated, but the activate hook is not called. (And it's certainly not called if the plugin is updated via FTP or similar.) So if I were putting in some code that need to run on activate or after an update I might hook it to init.

Categories