1

I am new to WordPress and I'm wondering if anyone could help me understand how I should go for rendering the output of a plugin when a form of a widget is submitted in the front-end.

I'm developing a plugin that has two "views" and one widget. The widget is just a form with a dropdown menu to select a value from a list. When a value is selected, the form should submit this value to a page where the first view of the plugin is rendered. This first view should print links that take the visitor to the second view. It's like if the first view was a list of rows, and the second view was the details of the row selected from the first view. My widget's form should know to which URL the data should be posted. This is the goal.

I've been reading the Plugin API, but I don't seem to have found a proper solution. My question is: should I only rely on the Shortcodes, previously put on certain pages, to render the plugin in the front-end? Is this the only way?

I tried to submit a custom action with the form of my widget (with action get_admin_url().'admin-post.php'), and hook it in the plugin with:

add_action('admin_post_customaction', 'handle_customaction');
add_action('admin_post_nopriv_customaction', 'handle_customaction');
function (handle_customaction) {
    //do something
}

but the page admin-post.php truncates the execution, so I guess this practice is only for executing certain tasks and then redirect, not to display content within a page in the front-end.

Is there a solution beside using Shortcodes? Because then my question would be "which action attribute should I use in the form of the widget?", like how do I know which pages use the Shortcode I need?

Maybe by creating a custom post type within the plugin? I do not need anything special in the admin section of the website. However, at least there, my plugin is called by WordPress with the link wp-admin/admin.php?page=myplugin and my main callback is called. But what about the front-end?

Am I missing something? I would like to not rely on the theme's code. Any hints would be much appreciated! Thanks

7
  • I see 3 things in this question : shortcode, form submission and custom post type. you will have more help if you create one question for each thing.
    – mmm
    Feb 3, 2018 at 12:21
  • Concerning shortocode, you ask if you must only use shortcode. it's not a obligation but it's the easiest way for your plugin users to put the form in a page. Other solution can be using a widget or using hook or filter to put content on other places defined by the theme.
    – mmm
    Feb 3, 2018 at 12:24
  • @mmm thanks for commenting. I am already using a widget that is declared by the main plugin. The main question is how to make the form of the widget point to the plugin in the front-end. My plugin is supposed to receive POST data in order to render content. If the widget is displayed on a page where there is no shortcode for my plugin, then how am I supposed to know from the widget where the form should point to? Feb 3, 2018 at 16:55
  • @mmm also, I may have added too many tags, but I don't need an explanation of what each tag does. I am just wondering to which address a form of a widget should be pointing. Assuming that I use a Shortcode in a specific page, how do I know from the widget which page it is? If there was a specific hook I could listen to, then I could submit a specific action data through the form. I hope this makes sense. Feb 3, 2018 at 16:58
  • to know from whitch page the form is submited, you need to put the page identifier in a hidden field of the form. then you can use this value to know where you redirect after the treatment
    – mmm
    Feb 3, 2018 at 17:25

1 Answer 1

0

I think you are complicating it more than it needs to be. You don't really need a widget at all. You need just a shortcode that outputs the form with the select, and also the data if there has been something selected. That data would be either the first view or the second view, depending on the parameters that you embed in the links and your form. So the page the shortcode is on is controlling the whole thing. This way it is not limited to one use on a site, although you should probably include a define in the shortcode that you check, so that it can't be used more than once per page.

The form would not need an action attribute, so it will be handled by the current page it is on. It would need to have the current value of which view is shown, and the links of the first view would need to have the value of the second view. That makes it have to use GET instead of POST, which looks messier in the address bar. So if you can change the first view to use buttons or something instead of links, you could use POST.

3
  • Thanks for your answer. I figured out that using the Shortcode is the best and only solution. The widget could still be handy, maybe with a self-action URL. Navigating from one View to another is not a problem if I work on the request variables, however, it seems that rewriting the URLs of the second view is not possible when I'm inside a shortcode. Basically, the first View rendered through the Shortcode display links to the second View, and these links cannot be rewritten according to custom rules/slugs. I will accept your answer in any case. Thanks Feb 9, 2018 at 17:24
  • "the first View rendered through the Shortcode display links to the second View, and these links cannot be rewritten according to custom rules/slugs." It's not rewrite or custom slugs, it's variables to indicate which view is needed, hence the concern about GET and POST mixture, because it's the same shortcode that will be interpreting both. Feb 11, 2018 at 15:18
  • Thanks for your comment. I suppose I should close this question because we are speaking about a different topic. I actually needed to rewrite the URLs of the first View according to the permalinks that use the Pages with my Shortcodes. The navigation between one view and another is clear, but there is no way to define a custom rewrite rule from the execution of the plugin, unless we use a custom post type. Therefore, I had to write a Class by myself to parse the Shortcodes used inside the Posts/Pages to get the permalink. I hope this will be helpful to someone else. Feb 14, 2018 at 17:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.