I need to set a session value in my plugin, but session isn't started when WordPress loads my plugin and I don't want to run if (!session_id()) session_start(); manually, because other plugins might rely on setting cookies, etc and face a "headers already sent error".
Is there any hook in WordPress that runs after session is started?
If not, how do you professionals handle such situations?
Wordpress does not use session. There are actually a number of reasons for this outlined very well in this stackexchange post:
https://wordpress.stackexchange.com/questions/167585/using-sessions-to-filter-posts-bad-thing/168089#168089
I would say if you are concerned about stepping on a plugin's toes, try hooking into "plugins_loaded" and use that hook as an entry for your plugin (if you can, I know that will limit functionality somewhat). But because Wordpress does not start a session itself it is really a coin toss whether you are interrupting another plugin or not.
session_start() isn't used by Wordpress.
Have you tried wp_loaded firing after WordPress is fully loaded? Or even wp which fire after the WP object is set up (ref array).
You can have a look at this question which will give you the action run sequence.
You can also have a look at the CODEX to have a better view at all action hooks available https://codex.wordpress.org/Plugin_API/Action_Reference
Related
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 can I disable OnePage Checkout in OpenCart?
Version 1.5.x comes with it in the default template and I would rather not use it as we want to have step by step pages and not use Ajax (speed/page views and process serves our needs better) for our customers.
It is possible to remove the one-page setup, but you'll have to do a very good work on the template (the checkout folder contains all steps). You will need personalized controllers as well. The built-in checkout page uses jQuery and Ajax to gathering all the information necessary in only one page, to send everything together when the customer clicks "confirm".
Once you don't want to use ajax, you will have to send the information from one page to the next using post requests, putting then in hidden <input>'s and/or in $_SESSION variable. Anyway, you will have some problems with the countries and locations, since OpenCart retrieves then via ajax.
Actually, some time ago I've found some templates on ThemeForest and other sites that implemented what you want, but I don't know if they are available anymore.
I would actually recommend you use something like Uber Checkout which while its still a short checkout process, is better visually as you don't have the panels that are standard in 1.5.X. If you're wanting to completely rewrite it to work like the old checkout system, well in theory it's already there, you just need to rewrite the controllers for the various steps to output full pages rather than JSON and put in validations through each step to make sure previous steps have been completed
Hey there. I'm currently learning how to write plugins for wordpress platform and I already know the basics. However, I need to invoke some function when a single article page (single.php) loads. Basically, I want to take that post ID and use it in my algorithm. I can't find any pre-defined action hook in wordpress framework so I thought I could write my very own one, but I don't know what's the mechanism for it. I read several guides on internet but still have no clue how it works. Can someone help me with this? Code examples are welcome.
You can start at the loop_start hook and check is_single() or whatever else you need to determine if you're being called from a singular page. I highly doubt you need to be creating new action hooks in the WordPress core for whatever you're doing.
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.
Is it possible for plugins to block System Events in Joomla? I'm currently using a url rewriting plugin and the onAfterRoute event does not seem to be called (which I need for another plugin)
Thanks
Plugins shouldn't be blocking the onAfterRoute event. However, plugins responding to that event must be published after they are installed.
Also, onAfterRoute is called after the URL is parsed and variables have been pushed into JRequest. Is this this point where you're wanting the plugin to be called, or does it need to come before onAfterRoute?
Does the other plugin (the one that uses onAfterRoute) work if you disable the URL rewriting plugin? If not, then that's not the problem.
If it does, then I suggest you look at the plugin and find the bit that targets onAfterRoute to see what it's doing. (Maybe post it here if it's not too involved.)