Skip to content

Instantly share code, notes, and snippets.

@martinandersen3d
Last active February 1, 2023 22:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save martinandersen3d/754c70d07ec4c34377d201c443539f80 to your computer and use it in GitHub Desktop.
Save martinandersen3d/754c70d07ec4c34377d201c443539f80 to your computer and use it in GitHub Desktop.
wordpress plugin post request form
//-----------------------Intro - most important:
// wp-content/themes/evolve/functions.php
function funkyMonky() {
// Handle request then generate response using echo or leaving PHP and using HTML
echo "hi";
}
add_action('admin_post_takeaway_settings_order','funkyMonky');
// wp-content/plugins/custom-admin-settings/admin/class-submenu-page.php
echo '<form action="' . esc_url( admin_url( "admin-post.php" ) ) . '" method="post" >';
<form action="wp-admin/admin-post.php" method="post">
<input type="hidden" name="action" value="takeaway_settings_order"><table>
</form>
//-----------------------Full:
// wp-content/plugins/custom-admin-settings/custom-admin-settings.php
<?php
/**
* The plugin bootstrap file
*
* This file is read by WordPress to generate the plugin information in the
* plugin admin area. This file also defines a function that starts the plugin.
*
* @link http://code.tutsplus.com/tutorials/creating-custom-admin-pages-in-wordpress-1
* @since 1.0.0
* @package Custom_Admin_Settings
*
* @wordpress-plugin
* Plugin Name: Custom Admin Settings
* Plugin URI: http://code.tutsplus.com/tutorials/creating-custom-admin-pages-in-wordpress-1
* Description: Demonstrates how to write custom administration pages in WordPress.
* Version: 1.0.0
* Author: Tom McFarlin
* Author URI: https://tommcfarlin.com
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
// Include the dependencies needed to instantiate the plugin.
foreach ( glob( plugin_dir_path( __FILE__ ) . 'admin/*.php' ) as $file ) {
include_once $file;
}
add_action( 'plugins_loaded', 'tutsplus_custom_admin_settings' );
/**
* Starts the plugin.
*
* @since 1.0.0
*/
function tutsplus_custom_admin_settings() {
$plugin = new Submenu( new Submenu_Page() );
$plugin->init();
}
// wp-content/plugins/custom-admin-settings/admin/class-submenu.php
<?php
/**
* Creates the submenu item for the plugin.
*
* @package Custom_Admin_Settings
*/
/**
* Creates the submenu item for the plugin.
*
* Registers a new menu item under 'Tools' and uses the dependency passed into
* the constructor in order to display the page corresponding to this menu item.
*
* @package Custom_Admin_Settings
*/
class Submenu {
/**
* A reference the class responsible for rendering the submenu page.
*
* @var Submenu_Page
* @access private
*/
private $submenu_page;
/**
* Initializes all of the partial classes.
*
* @param Submenu_Page $submenu_page A reference to the class that renders the
* page for the plugin.
*/
public function __construct( $submenu_page ) {
$this->submenu_page = $submenu_page;
}
/**
* Adds a submenu for this plugin to the 'Tools' menu.
*/
public function init() {
add_action( 'admin_menu', array( $this, 'add_options_page' ) );
}
/**
* Creates the submenu item and calls on the Submenu Page object to render
* the actual contents of the page.
*/
public function add_options_page() {
add_options_page(
'Tuts+ Custom Administration Page',
'Custom Administration Page',
'manage_options',
'custom-admin-page',
array( $this->submenu_page, 'render' )
);
}
}
// wp-content/plugins/custom-admin-settings/admin/class-submenu-page.php
<?php
/**
* Creates the submenu page for the plugin.
*
* @package Custom_Admin_Settings
*/
/**
* Creates the submenu page for the plugin.
*
* Provides the functionality necessary for rendering the page corresponding
* to the submenu with which this page is associated.
*
* @package Custom_Admin_Settings
*/
class Submenu_Page {
/**
* This function renders the contents of the page associated with the Submenu
* that invokes the render method. In the context of this plugin, this is the
* Submenu class.
*/
public function render() {
$openhours = array(
"ons"=>array(
"16"=>10,
"17"=>10,
"18"=>10,
"19"=>10,
"20"=>10,
"21"=>10,
"22"=>10,
),
"tor"=>array(
"16"=>10,
"17"=>10,
"18"=>10,
"19"=>10,
"20"=>10,
"21"=>10,
"22"=>10,
),
"fre"=>array(
"16"=>10,
"17"=>10,
"18"=>10,
"19"=>10,
"20"=>10,
"21"=>10,
"22"=>10,
),
"lør"=>array(
"16"=>10,
"17"=>10,
"18"=>10,
"19"=>10,
"20"=>10,
"21"=>10,
"22"=>10,
),
);
// Style
echo '
<style>
input[type=number]{
width:45px;
}
tr { display: block; float: left; }
th, td { display: block; height:26px}
.opdater{width:300px;}
</style>';
// takeaway_settings_order: <input type="hidden" name="action" value="admin_post_takeaway_settings_order">
//
// Header
echo '<h1>Max Antal Ordre per time ( 1 order = 500kr):</h1>';
// Form
echo '<form action="' . esc_url( admin_url( "admin-post.php" ) ) . '" method="post" >';
// Wordpress Hook
echo '<input type="hidden" name="action" value="takeaway_settings_order">';
// table
echo '<table>
<tr>
<th> Klokken</th>';
// Klokken - Coll
foreach ($openhours['tor'] as $k => $v) {
echo "<th>" . $k . "</th>";
}
echo '
</tr>
<th> Ons</th>';
foreach ($openhours['ons'] as $k => $v) {
echo '<td><input name="asda" id="movie" type="number" value="'. $v .'" min="0"></td>';
}
echo '
</tr>
<th> Tor</th>';
foreach ($openhours['tor'] as $k => $v) {
echo '<td><input id="movie" type="number" value="'. $v .'" min="0"></td>';
}
echo '
</tr>
<th> Fre</th>';
foreach ($openhours['fre'] as $k => $v) {
echo '<td><input id="movie" type="number" value="'. $v .'" min="0"></td>';
}
echo '
</tr>
<th> Lør</th>';
foreach ($openhours['lør'] as $k => $v) {
echo '<td><input id="movie" type="number" value="'. $v .'" min="0"></td>';
}
echo '
</tr>
</table>
';
echo '<br>
<input type="submit" class="opdater button button-primary" value="Opdater">
</form>';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment