Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+3 votes
976 views
in Q2A Core by
reopened by
After clicking the button, he must immediately want to get redirected to the question list page intead of directing him to the actual question page...

Any one please help me with this

1 Answer

+3 votes
by
selected by
 
Best answer

You'll have to create a simple plugin for that:

1. Create a directory qa-plugin/your-plugin. All the following files go inside this directory

2. Create the file metadata.json with the following content:

{  "name": "Your plugin" }

3. Create the file qa-plugin.php with the following content:

<?php
if (!defined('QA_VERSION')) {
   header('Location: ../../');
   exit;
}
qa_register_plugin_module('event', 'qa-your-plugin-event.php', 'YourPluginEvent', 'Your Plugin Event');

3. Create the file qa-your-plugin-event.php with the following content

<?php
class YourPluginEvent {
    public function process_event($event, $userid, $handle, $cookieid, $params)  {
        if ($event === 'q_post') {
            qa_redirect('');
        }
    }
}

4. Navigate to admin/plugins and enable the "Your plugin" (remember to click the Save button below the list) event and try asking a question

by
edited by
+1
Hello pupi,


It works fine ;-)
by
+1
For what it's worth, the plugins in Q2A are executed in alphabetical order. For this particular case, if a vanilla Q2A installation is been used, the execution order will be as follows:

- Under 'qa-include/plugins':
1) 'Q2A Event Limits', from qa-event-limits.php
2) 'Q2A Event Notify', from qa-event-notify.php
3) 'Q2A Event Updates' from qa-event-updates.php

- Under 'qa-plugin':
4) 'Event Logger', from event-logger/qa-event-logger.php

New plugins will be executed somewhere after the third item above, alongside 'Event Logger'; However, depending on the name of the new plugin, it could prevent the execution of other plugins.

In the list above, for example, if the new plugin was saved under 'qa-plugin/another-event' it'd prevent the execution of the plugin 'Event Logger'; on the other hand, if it was saved under 'qa-plugin/other-event' it'd be executed after the plugin 'Event Logger'.

So the name of the new plugin should be chosen keeping this information in mind, in order to prevent unexpected side effects.
by
+1
Possibly, the smartest comment I've read in the last 12 months :)

You're 100% correct. It's not really the plugin name the thing that has to be chosen, but rather plugin directory name. It is fetched by the glob function. Furthermore, as it comes from the file system, plugin order depends on the file system rules, so capitalization matters.

A similar scenario happens with plugin layers that don't call their parent method, they just destroy the chain. Sometimes there isn't much to do about it.

I think renaming the directory is just a workaround but I can't figure out a bullet-proof approach for this:

 1. I thought about adding metadata in the metadata.json file so that plugins can state their execution order (e.g. run_after: [ "plugin_id1", "plugin_id2" ]) but this will require plugins to have an ID (something I suggested 5 years back and developers still don't respect: https://www.question2answer.org/qa/31654) and also plugin developers will have to KNOW about those other plugins and add them in the metadata as dependencies. So this solution doesn't scale.

 2. I also thought of adding priority to plugins in their metadata. Just a float number between 0 and 1. Then developers will know whether they want to execute as close to the beginning of the execution process (0) or at the end (1). This could fix the issue that you brought up but only if there is just one plugin with priority 1. Having 2 would mitigate the issue, of course, but still there is chance of breaking

At the end of the day, it depends so much in the plugins a user is running in their system that thinking in dependencies or priorities set by the plugins themselves is not the perfect solution. The best approach I could come up with is letting the user define the plugin execution order. For example, setting it in admin/plugins somehow (e.g. adding some arrows to move them). I think most users will have no idea what to do with that, though.
by
Thank you @pupi1985, your ideas are really clever.  I like this one:
"...letting the user define the plugin execution order..."
by
Hello pupi,
     with your solution,now after submitting the question,the page gets directed to the homepage.how to navigate it to the ask page itself instead of directing it to the homepage?
by
Try:

qa_redirect('ask');
by
it actually worked pupi
...