Created
November 25, 2020 20:05
-
-
Save webbouwer/6ae48b5b3ae3cac80c21a8a46bfc668d to your computer and use it in GitHub Desktop.
Wordpress plugin skeleton
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Skeleton Plugin | |
Plugin URI: https://webbouwer.org | |
Description: Skeleton code for Wordpress plugin | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} | |
function plugconstruct() { | |
return new ThisPluginClass(); | |
} | |
add_action( 'init', 'plugconstruct' ); | |
class ThisPluginClass { | |
function __construct() { | |
add_action( 'wp_enqueue_scripts', array( $this, 'load_scripts' ) ); | |
add_action( 'wp_ajax_skeplug', array( $this, 'ajax_callback_skeplug_function' ) ); | |
} | |
public function load_scripts() { | |
wp_register_script( 'skeplug', | |
plugins_url( 'skeletonpluginscript.js', __FILE__ ), | |
array( 'jquery' ) | |
); | |
wp_localize_script( 'skeplug', 'params', array( | |
'ajaxurl' => admin_url( 'admin-ajax.php' ) | |
) ); | |
wp_enqueue_script( 'skeplug' ); | |
} | |
function ajax_callback_skeplug_function() { | |
if ( check_ajax_referer( '_the_nonce', 'security' ) ) { | |
$somevar = $_POST['var_from_ajax_post']; | |
if ( $somevar === 'false' ) { | |
wp_send_json_success( array( 'something' => 12, 'message' => 'AOK' ) ); | |
} else { | |
wp_send_json_error(); | |
} | |
} | |
} | |
function add_click_link() { | |
$ajax_nonce = wp_create_nonce( "skeplug" ); | |
$link = '<a href="#" class="some-link-class" id="testbutton" ' | |
. 'data-nonce="' . $ajax_nonce . '" data-somevar="' . $varX . '">klik here</a>'; | |
return $link; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jQuery(document).ready(function ($) { | |
$('#testbutton').on('click', function (e) { | |
e.preventDefault(); | |
var data = { | |
action: 'skeplug', | |
security: $(this).data('nonce'), | |
var_from_ajax_post: $(this).data('somevar'), | |
}; | |
$.post(params.ajaxurl, data, function (response) { | |
$('#testbutton') | |
.data('somevar', response.data.something) | |
.text(response.data.message); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment