Exempelkod – egen plugin, onsdag v 23

Vår exempelkod:

plugin-example 2

<?php
/**
 * @package Plugin Example
 * @version 1.0
 */
/*
Plugin Name: Plugin Example
Description: This is just a small example of a basic plugin.
Author: Thomas
Version: 1.0
*/

// A function that I am going to use with the_content hoook/filter
function plugin_example_the_content($content){
  return '<button class="btn btn-default">Lika detta!</button>'.$content;
}
// Add the function to a hook/filter
add_filter("the_content","plugin_example_the_content");

// A function that adds my JavaScript(s)
function plugin_example_enqueue_scripts(){
  wp_register_script(
    "exampleJS", // my own name of the script (unique)
    plugins_url("example.js",__FILE__), // calculate path to 
    array('jquery') // dependencies (scripts we need)
  );
  wp_enqueue_script("exampleJS");
}

// Add the function to a hook/action
add_action("wp_enqueue_scripts","plugin_example_enqueue_scripts");


// Run this function when the plugin is activated
function plugin_example_activate(){
  global $wpdb; // global built in variable in WP

  // always make sure to use the db prefix in your table names
  $table_name = $wpdb->prefix . "plugin_example_message";

  // create tables with the same character encoding as the 
  // rest of the database
  $charset_collate = $wpdb->get_charset_collate();

  $sql = "CREATE TABLE $table_name ( 
   id int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
   userID bigint(20) NOT NULL,
   message TEXT
  ) $charset_collate;";

  // Run our sql
  $wpdb->query($sql);
}
// Add the function to the activation hook
register_activation_hook(__FILE__,"plugin_example_activate");

// When someone uninstalls our plugin - clean up
// delete db tables etc...
function plugin_example_uninstall(){
  global $wpdb;
  $table_name = $wpdb->prefix . "plugin_example_message";
  $sql = "DROP TABLE IF EXISTS $table_name";
  $wpdb->query($sql);
}
// Add the function to the uninstall hook
register_uninstall_hook(__FILE__,"plugin_example_uninstall");


// This function will recieve data from an AJAX call
// the call should be made from JavaScript to the url
// /cms-wp/wp-admin/admin-ajax.php
// The action sent from javascript should be set_message

function plugin_example_ajax_set_message(){
  // Pick up data sent via AJAX
  $message = $_POST["message"];

  // Get the current logged in WP user
  $user = wp_get_current_user();

  // Normally (in your upcoming exercise) we might
  // like to write something to the DB (our own table?)
  // then it can be useful to know about the following methods
  global $wpdb;

  // get the table with the correct prefix
  $table_name = $wpdb->prefix . "plugin_example_message";

  // INSERT THE MESSAGE CONNECTED TO THE USER IN THE DATABASE

  // create your sql
  $sql = "INSERT INTO $table_name(userID,message) VALUES(%s,%s)";
  // prepare the db query with data from an array
  $q = $wpdb->prepare($sql,array($user->ID,$message));
  // perform the query
  $wpdb->query($q);

  // Tell the web browser I am sending json back
  header("Content-type: application/json");
  // Encode a PHP array into json and echo it as an answer
  echo(json_encode([
    // For this to be useful maybe we should save your message in DB
    // so we can return it multiple times with get_message
    // but for now just echo the message from the client back
    "yourMessage" => $message,
    "user" => $user
  ]));
  exit(); // stop php execution
}
$ajax_action = "set_message";
add_action("wp_ajax_".$ajax_action,"plugin_example_ajax_set_message");

function plugin_example_ajax_get_message(){
  // Tell the web browser I am sending json back
  header("Content-type: application/json");
  // Encode a PHP array into json and echo it as an answer
  echo(json_encode([
    "name" => "Amanda",
    "age" => 115
  ]));
  exit(); // stop php execution
}
$ajax_action = "get_message";
add_action("wp_ajax_".$ajax_action,"plugin_example_ajax_get_message");

JavaScript:

var $ = jQuery;

$.ajax({
  url: "/cms-wp/wp-admin/admin-ajax.php", // url
  dataType: "json", // content type expected back
  method: "POST", // http request method
  // a bunch of data parameters
  data: {
    // Note! Action important - decides what function
    // to trigger on the backend in our WP plugin
    action: "set_message",
    message: "This is an uncool message!"
  },
  // Run success when we get a "good" answer back
  success: function(data){
    // Check that we get data back
    console.log(data);
  },
  // Run error if we get malformed data or an error back
  error: function(){

  }
});
0 votes