0

First of all I'm still relatively new to programming and wordpress development.

I'm building a plugin that takes custom data from database and a list of saved drafts and then modifies draft chosen by user, using that data. Right now it works like this:

  • I added admin menu under Posts category using add_submenu_page(),
  • On that page I created a dropdown menu using html select form that fetches list of drafts from database and displays it to user,
  • I have a button next to dropdown menu that when you click it supposedly publishes post and modifies it using custom data from database.

My problem is that to accomplish this I would have to use POST and send all this stuff to another php page. I'd like to avoid this if possible because I want to keep this plugin simple and I'm not that comfortable with wordpress development.

What I want to know is if there's any other way to do this? Some sort of built-in wordpress action or hook that I can use to achieve this? Or is there any other simpler approach in general.

I've been considering also moving all of this to options and using Settings API and POSTing to options.php.

I appreciate any input.

Edit: Here's relevant code:

<div class="wrap">
    <p>Select post:</p>
        <form name="autogenerator"> 
            <select id='drafts' name="drafts">

            <?php foreach($keywords as $keyword) {
                $post = get_post($keyword);
                $title = $post->post_title;
                echo "<option value=$keyword>$title</option>";
            }?>

            <input type="button" onClick="" value="Generate!">
            </select>
        </form>
</div>

$keywords is a list of posts id. I'd like to be able to pass this id to my button so that when it's clicked it runs another function that publishes selected post and modifies it using data from database.

  • Please show us your code. Hard to guess what you're really doing. – kaiser Nov 3 '13 at 14:56
  • Added code to the op. – dkns Nov 3 '13 at 15:58
1

You have to POST the data somehow, or send it via GET (not good if you have a lot of data, and not really the intended purpose of GET anyway).

It is really hard to tell exactly what your problem is though. The options that come to my mind are:

  1. Use the AJAX API (even if the request isn't my by Javascript).
  2. Submit to the page you've already created.

Submitting to a different page is tricky if you need WordPress functions, and probably not worth the effort.

I'd use option #2. You can process the form in the same block of code that creates the plugin page, but if you need to redirect (PRG) you will need to hook the processing function into an early hook. Look at the wp-admin/admin.php file. Starting about halfway down you start to get a bunch of hooks beginning with the very general admin_init but also including some very specific hooks like 'load-' . $page_hook and 'load-' . $plugin_page.

Edit: Based on the code you added to the question, use the AJAX API. Also, your markup is malformed-- you need quotes around the option value attribute.

  • Thanks, your suggestion looks really solid. I added relevant code, maybe that will shed some light on what I'm trying to accomplish. – dkns Nov 3 '13 at 15:59
  • @s_ha_dum I'm not sure if the quotes are needed. IIRC they're only needed if you got spaces. – kaiser Nov 3 '13 at 16:16
  • 1
    @kaiser: Seems to be new to HTML5. HTML4 requires quotes. I guess you win :) – s_ha_dum Nov 3 '13 at 16:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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