Skip to content

Instantly share code, notes, and snippets.

@devkabir
Created November 22, 2023 17:41
Show Gist options
  • Select an option

  • Save devkabir/2d586daa2dd13f6058a73b208df4e458 to your computer and use it in GitHub Desktop.

Select an option

Save devkabir/2d586daa2dd13f6058a73b208df4e458 to your computer and use it in GitHub Desktop.
Coding Section Plugin
<?php
/*
* Plugin Name: Coding Section Plugin
* Plugin URI: https://yourwebsite.com
* Description: This plugin creates a custom table in the WordPress database and provides shortcodes for user interaction.
* Version: 1.0
* Requires at least: 5.2
* Requires PHP: 7.2
* Author: Dev Kabir
* Author URI: https://yourwebsite.com
* License: GPL v2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: devkabir-coding-section
*/
/**
* Creates a table in the WordPress database for the devkabir_coding_section plugin.
* @return void
*/
function devkabir_coding_section_create_table() {
global $wpdb;
$table_name = $wpdb->prefix . 'devkabir_coding_section_table';
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE IF NOT EXISTS $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
name tinytext NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
dbDelta( $sql );
}
register_activation_hook( __FILE__, 'devkabir_coding_section_create_table' );
/**
* Adds shortcodes for the devkabir_coding_section plugin.
*
* @return void
*/
function devkabir_coding_section_add_shortcode() {
add_shortcode( 'devkabir_coding_section_form', 'devkabir_coding_section_shortcode_form' );
add_shortcode( 'devkabir_coding_section_list', 'devkabir_coding_section_shortcode_list' );
}
add_action( 'init', 'devkabir_coding_section_add_shortcode' );
function devkabir_coding_section_add_shortcode_scripts() {
wp_enqueue_script( 'devkabir-coding-section', plugins_url( 'script.js', __FILE__ ), array( 'jquery' ), '1.0.0', true );
wp_localize_script(
'devkabir-coding-section',
'wpApiSettings',
array(
'root' => sprintf( '%sdevkabir-coding-section/v1/names', esc_url_raw( rest_url() ) ),
'nonce' => wp_create_nonce( 'wp_rest' ),
)
);
}
add_action( 'wp_enqueue_scripts', 'devkabir_coding_section_add_shortcode_scripts' );
/**
* Generates the function comment for the given function body.
*
* @return string The generated function comment.
*/
function devkabir_coding_section_shortcode_form(): string {
ob_start();
?>
<form method="post">
<input type="text" name="name" value="">
<button type="submit">Submit</button>
</form>
<?php
return ob_get_clean();
}
/**
* Generates a list of items for the devkabir_coding_section shortcode.
* @return false|string
*/
function devkabir_coding_section_shortcode_list() {
$data = devkabir_coding_section_get_table_data();
ob_start();
foreach ( $data as $datum ) {
?>
<li>
<?php echo esc_html( $datum['name'] ); ?>
</li>
<?php
}
return ob_get_clean();
}
/**
* Retrieves the table data from the "devkabir_coding_section_table" table.
*
* @global wpdb $wpdb The WordPress database object.
* @return array The retrieved table data.
*/
function devkabir_coding_section_get_table_data(): array {
global $wpdb;
return $wpdb->get_results( "SELECT id, name FROM {$wpdb->prefix}devkabir_coding_section_table LIMIT 1000", ARRAY_A );
}
/**
* Inserts data into the specified table.
*
* @param string $name The name to be inserted into the table.
*
* @return void
*/
function devkabir_coding_section_insert_data_to_table( string $name ) {
global $wpdb;
$table_name = $wpdb->prefix . 'devkabir_coding_section_table';
$wpdb->insert(
$table_name,
array(
'name' => $name,
),
array(
'%s',
)
);
}
/*
* -----------------------------------------------------------------------------------------------------------------
* Bonus Part
* -----------------------------------------------------------------------------------------------------------------
*/
add_action(
'rest_api_init',
function () {
register_rest_route(
'devkabir-coding-section/v1',
'/names',
array(
array(
'methods' => 'POST',
'callback' => 'devkabir_coding_section_rest_insert_data',
'permission_callback' => 'devkabir_coding_section_rest_permissions_check',
),
array(
'methods' => 'GET',
'callback' => 'devkabir_coding_section_rest_get_data',
'permission_callback' => 'devkabir_coding_section_rest_permissions_check',
),
)
);
}
);
/**
* Insert Item from request
*
* @param WP_REST_Request $request from frontend.
* @return WP_REST_Response
*/
function devkabir_coding_section_rest_insert_data( WP_REST_Request $request ): WP_REST_Response {
$name = sanitize_text_field( $request->get_param( 'name' ) );
devkabir_coding_section_insert_data_to_table( $name );
return new WP_REST_Response( 'Data inserted successfully.', 200 );
}
/**
* Get items from the collection
*
* @return WP_REST_Response
*/
function devkabir_coding_section_rest_get_data(): WP_REST_Response {
$data = devkabir_coding_section_get_table_data();
return new WP_REST_Response( $data, 200 );
}
/**
* Check if a given request has access to get items
*
* @return bool
*/
function devkabir_coding_section_rest_permissions_check(): bool {
return current_user_can( 'manage_options' );
}
if (typeof jQuery !== undefined) {
jQuery( document ).on(
'submit',
'form',
function (event) {
event.preventDefault();
const data = jQuery( this ).serializeArray();
jQuery.ajax(
{
url: wpApiSettings.root,
method: 'POST',
beforeSend: function ( xhr ) {
jQuery( document ).find( 'button[type="submit"]' ).prop( 'disabled', true ).text( 'Sending...' );
xhr.setRequestHeader( 'X-WP-Nonce', wpApiSettings.nonce );
},
data
}
).done(
function ( response ) {
alert( response );
}
).always(
function () {
jQuery( document ).find( 'button[type="submit"]' ).prop( 'disabled', false ).text( 'Send' );
jQuery( 'form' ).trigger( 'reset' );
}
);
}
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment