Skip to content

Instantly share code, notes, and snippets.

@aldakur
Last active May 31, 2017 10:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save aldakur/31962ca16c0448fe3456c5cd71dd7d8c to your computer and use it in GitHub Desktop.
Save aldakur/31962ca16c0448fe3456c5cd71dd7d8c to your computer and use it in GitHub Desktop.
labur
jQuery(document).ready(function($) {
jQuery("#button_polr_get_url").click(function(){
var doc_pathname = '';
var suwp_pathname = document.location.pathname.split('/')[1];
// add the path element, if necessary
if( suwp_pathname == 'wp-admin' ) {
doc_pathname = '/wp-admin/admin-ajax.php';
} else {
doc_pathname = '/' + suwp_pathname + '/wp-admin/admin-ajax.php';
}
var url = document.location.protocol + '//' + document.location.host + doc_pathname;
jQuery.ajax({
type: 'POST',
url: url,
dataType: 'text',
data: {
postID: MyAjax.post_id, // From PHP
action: MyAjax.action // From PHP-tik
},
success: function(data, textStatus, XMLHttpRequest){
$('#polr_shortened_url').val(data);
},
error: function(MLHttpRequest, textStatus, errorThrown){
alert(errorThrown);
}
});
});
});
<?php
/*
Plugin Name: Polr Wordpress Plugin
Plugin URI: https://wordpress.org/plugins/polr-wordpress-plugin
Description: Polr is a quick, modern, and open-source link shortener. This plugin allows you to use Polr service in Wordpress.
Version: 1.0
Author: Egoitz Gonzalez
Author URI: http://www.egoitzgonzalez.com
License: GPLv3
License URI: https://www.gnu.org/licenses/gpl.html
Text Domain: polr
Domain Path: /languages
*/
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
// === INCLUDES === //
include( plugin_dir_path( __FILE__ ) . 'admin/polr-metabox.php');
include( plugin_dir_path( __FILE__ ) . 'admin/admin-all-posts-page.php');
include( plugin_dir_path( __FILE__ ) . 'admin/admin-menu.php');
include( plugin_dir_path( __FILE__ ) . 'admin/plugins-page.php');
include( plugin_dir_path( __FILE__ ) . 'admin/settings-page.php');
// === //
// Set up to internationalize the plugin
$plugin = plugin_basename( __FILE__ );
add_filter( "plugin_action_links_$plugin", 'polr_plugin_add_settings_link' ); // admin/plugins-page.php
/**
* @wp-hook plugins_loaded
* Set to internationalize the plugin
*/
function polr_plugin_load_textdomain() {
$text_domain = 'polr';
$path_languages = basename(dirname(__FILE__)).'/languages/'; // basename(dirname(__FILE__)) relative url. This function extracts the name of the plugin directory. In this case, Polr.
load_plugin_textdomain($text_domain, false, $path_languages );
}
add_action('plugins_loaded', 'polr_plugin_load_textdomain'); // init hook is valid too
/**
* @wp-hook add_meta_boxes
* Load external files
* JQuery
* polr.js
*/
function polr_init() {
// registers plugin's js file. Jquery is a requirement for this script, so we specify it
// polr-js is the name to reference polar.js file as identifile. Jquery is a requirement for polr.js file
wp_register_script( 'polr-js', plugins_url( '/polr.js', __FILE__ ), array('jquery') );
//load scripts
wp_enqueue_script('jquery'); // Load jquery for polr.js
wp_enqueue_script('polr-js'); // Load polr.js file. This file is registered as polr-js
global $post;
$post_id = $post->ID;
wp_localize_script('polr-js', 'MyAjax', array(
'post_id' => $post_id,
'action'=> 'polr_get_url' // polr_get_url is the action, but without the wp_ajax prefix. This acction launches polr_get_url_process function
));
}
add_action( 'add_meta_boxes', 'polr_init' ); // wp hook is valid too
/**
* @wp-hook wp_ajax_polr_get_url
* Function to process the Ajax
* Get post url, service api key, host url and create shortened url
*/
function polr_get_url_process() {
if(isset($_POST['postID']))
{
$post_id = intval( $_POST['postID'] );
$post_url = esc_url(get_permalink($post_id));
$api_key = esc_attr(get_option('polr_settings_api_key'));
$host_url = esc_url(get_option('polr_settings_host'));
$shortenedurl = file_get_contents($host_url.'/api/v2/action/shorten?key='.$api_key.'&url='.$post_url.'&is_secret=false');
update_post_meta($post_id, 'polr_shortened_url', esc_url_raw($shortenedurl)); // post id, field name, data to insert. This functions save the data
echo esc_url($shortenedurl);
}
exit();
}
add_action('wp_ajax_polr_get_url', 'polr_get_url_process'); // Defined polr_get_url action. This is called in polr.js file . wp_ajax is a standard prefix in Wordpress
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment