-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwp-simple-donation.php
More file actions
60 lines (55 loc) · 1.97 KB
/
wp-simple-donation.php
File metadata and controls
60 lines (55 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
/**
* Plugin Name: Simple Donation Plugin
* Plugin URI: https://github.com/tejasjundre/wp-simple-donation
* Description: A basic WordPress plugin that registers a "Donations" custom post type and provides a [donation_form] shortcode.
* Version: 1.0
* Author: Tejas Jundre
* Author URI: https://github.com/tejasjundre
* License: GPL2
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: wp-simple-donation
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
// Register Custom Post Type: Donation
function wp_simple_donation_cpt() {
$labels = array(
'name' => 'Donations',
'singular_name' => 'Donation',
);
$args = array(
'labels' => $labels,
'public' => true,
'supports' => array('title', 'editor', 'custom-fields'),
'menu_icon' => 'dashicons-heart',
);
register_post_type( 'donation', $args );
}
add_action( 'init', 'wp_simple_donation_cpt' );
// Donation Form Shortcode
function wp_simple_donation_form() {
ob_start();
?>
<form method="post" action="">
<label for="donor_name">Name:</label><br>
<input type="text" name="donor_name" required><br><br>
<label for="donation_amount">Amount:</label><br>
<input type="number" name="donation_amount" required><br><br>
<input type="submit" name="donate_now" value="Donate">
</form>
<?php
// Handle form submission
if ( isset($_POST['donate_now']) ) {
$name = sanitize_text_field($_POST['donor_name']);
$amount = floatval($_POST['donation_amount']);
wp_insert_post( array(
'post_title' => 'Donation from ' . $name,
'post_type' => 'donation',
'post_status'=> 'publish',
'meta_input' => array('amount' => $amount),
) );
echo '<p>Thank you for your donation, ' . esc_html($name) . '!</p>';
}
return ob_get_clean();
}
add_shortcode( 'donation_form', 'wp_simple_donation_form' );