DevinVinson / WordPress-Plugin-Boilerplate

[WordPress] A foundation for WordPress Plugin Development that aims to provide a clear and consistent guide for building your plugins.
http://wppb.io
7.67k stars 2.25k forks source link

My own get / post requests for a custom crud. #385

Open NebriBlackwing opened 8 years ago

NebriBlackwing commented 8 years ago

Hello,

I'm trying to create a plugin for a client that will accept a form entry to populate a custom database table. I'm having difficulty using the adminpost(action) functionality in wordpress with this the plugin-boilerplate.

I referenced this: https://codex.wordpress.org/Plugin_API/Action_Reference/admin_post_(action)

to come up with the following in my class-plugin-name.php

`/* * Register all of the hooks related to the admin area functionality * of the plugin. * @since 1.0.0 * @access private */ private function define_admin_hooks() {

    $plugin_admin = new Plugin_Employee_Directory_Admin( $this->get_plugin_name(), $this->get_version() );

    $this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_styles' );
    $this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_scripts' );

    // create the directory crud screen.
    $this->loader->add_action('admin_menu', $plugin_admin, 'display_admin_page');
    $this->loader->add_action('admin_post_add_foorbar', $plugin_admin, 'add_foobar');
}`

And then inside my plugin-name-admin.php I've declared the following public function:

public function add_foobar() { wp_redirect( home_url() ); exit; }

Inside the partial view I have:

<form id="employeeForm" action="<?php echo admin_url('admin-post.php'); ?>" method="post"> <input type="hidden" name="action" value="add_foobar">

Now when I submit the form itself, I do get sent to /wp-admin/admin-post.php, but it just stays a white screen. I never get the redirect back to my home url as expected. This plugin will have several get and post requests that I'll need to create to do my own processing. Would greatly appreciate some help as I'm either not understanding something, or am missing something in my implementation.

LinzardMac commented 8 years ago

Might I suggest doing wp_die(var_dump($_POST)); inside of the add_foobar() function before your redirect code just to make sure your function is, in fact, being called?

You might need to try adding your action into your Plugin_Employee_Directory_Admin() __construct method like this:

add_action('admin_post_nopriv_add_foobar',array(&$this,'add_foobar'));

widoz commented 8 years ago

Your hook name is wrong you try to use 'admin_post_add_foorbar' but should be 'admin_post_add_foobar' ( foorbar ).

LinzardMac commented 8 years ago

LOL good catch!