Last active
August 29, 2015 14:02
-
-
Save daggerhart/78608472ab4eec29a914 to your computer and use it in GitHub Desktop.
Wordcamp AVL 2014 - Introduction to Wordpress Plugin Development
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: Intro to Wordpres Plugin Development | |
Description: Generic example plugin | |
Plugin URI: http://www.daggerhart.com/blog/introduction-wordpress-plugin-development/ | |
Author: Jonathan Daggerhart | |
Author URI: http://www.daggerhart.com | |
Version: 1.0 | |
Text Domain: intro-to-plugin-dev | |
Domain: /lang | |
License: GPL2 | |
*/ | |
define( 'ITPD_PLUGIN_FILE', __FILE__); | |
define( 'ITPD_PLUGIN_DIR', dirname( __FILE__ ) ); | |
define( 'ITPD_PLUGIN_URL', plugin_dir_url( __FILE__ ) ); | |
/* | |
* init action | |
* http://codex.wordpress.org/Function_Reference/add_action | |
*/ | |
function itpd_init(){ | |
//print 'this happened'; exit; // proof | |
wp_register_style( 'itpd_css', ITPD_PLUGIN_URL. '/itpd.css' ); | |
wp_enqueue_style( 'itpd_css' ); | |
} | |
add_action( 'init', 'itpd_init' ); | |
/* | |
* the_content filter | |
* http://codex.wordpress.org/Function_Reference/add_filter | |
*/ | |
// Simple find and replace | |
function itpd_the_content_simple($content){ | |
return str_ireplace("FINDME", "REPLACE WITH ME", $content); | |
} | |
add_filter( 'the_content', 'itpd_the_content_simple'); | |
/* | |
shortcodes - [itpd_hr], [itpd_link id=1], [itpd_color color="purple"] | |
*/ | |
/* | |
* shortcode hr | |
* http://codex.wordpress.org/Function_Reference/add_shortcode | |
*/ | |
function itpd_shortcode_hr($attributes){ | |
return '<hr class="itpd_hr" />'; | |
} | |
add_shortcode( 'itpd_hr', 'itpd_shortcode_hr' ); | |
/* | |
* shortcode post link | |
* http://codex.wordpress.org/Function_Reference/add_shortcode | |
*/ | |
function itpd_link_to_post($attributes){ | |
$default_attributes = array( | |
'id' => '', | |
); | |
$merged_attributes = shortcode_atts( $default_attributes, $attributes ); | |
extract( $merged_attributes ); | |
if (!empty($id)){ | |
$href = get_permalink($id); | |
$title = get_the_title($id); | |
return "<a class='itpd_link' href='$href' title='$title'>$title</a>"; | |
} | |
} | |
add_shortcode( 'itpd_link', 'itpd_link_to_post' ); | |
/* | |
* shortcode color | |
* http://codex.wordpress.org/Function_Reference/add_shortcode | |
*/ | |
function itpd_shortcode_color_wrap($attributes, $content = '') { | |
$default_attributes = array( | |
'color' => 'black', | |
); | |
$merged_attributes = shortcode_atts( $default_attributes, $attributes ); | |
extract( $merged_attributes ); | |
return '<span style="color: '.$color.';">' . $content. '</span>'; | |
} | |
add_shortcode( 'itpd_color', 'itpd_shortcode_color_wrap' ); | |
/* | |
* Add add_menu page | |
* http://codex.wordpress.org/Function_Reference/add_menu_page | |
*/ | |
function itpd_admin_menu(){ | |
add_menu_page( "Intro To Plugin Dev", | |
"Intro to Plugin Dev Settings", | |
'manage_options', | |
'intro_to_plugin_dev', | |
'intro_to_plugin_dev_settings'); | |
} | |
add_action( 'admin_menu', 'itpd_admin_menu' ); | |
/* | |
* add_menu_page callback | |
*/ | |
function intro_to_plugin_dev_settings(){ | |
// print 'Here is the custom admin page'; return; // proof | |
// save the submission | |
if (isset($_GET['itpd_save'])){ | |
// TODO -- | |
// each of these update_option functions should sanitize data first (not shown in this example) | |
if (isset($_POST['itpd_find'])){ | |
update_option('itpd_find', $_POST['itpd_find']); | |
} | |
if (isset($_POST['itpd_replace'])){ | |
update_option('itpd_replace', $_POST['itpd_replace']); | |
} | |
// redirect back to form | |
wp_redirect($_SERVER['HTTP_REFERER']); | |
exit(); | |
} | |
// show the form | |
intro_to_plugin_dev_settings_form(); | |
} | |
/* | |
* Settings page form | |
*/ | |
function intro_to_plugin_dev_settings_form(){ | |
$itpd_find = get_option( 'itpd_find', '' ); | |
$itpd_replace = get_option( 'itpd_replace', '' ); | |
?> | |
<div class="wrap"> | |
<h1>Intro to Plugin Dev Settings</h1> | |
<form action="<?php print $_SERVER['PHP_SELF']; ?>?page=intro_to_plugin_dev&noheader=true&itpd_save=true" | |
method="post"> | |
<div> | |
<label>Find: </label><input type="text" name="itpd_find" value="<?php print $itpd_find; ?>" /> | |
<p class="description">The string to find in posts and pages.</p> | |
</div> | |
<div> | |
<label>Replace: </label><input type="text" name="itpd_replace" value="<?php print $itpd_replace; ?>" /> | |
<p class="description">The replacement string.</p> | |
</div> | |
<?php submit_button(); ?> | |
</form> | |
</div> | |
<?php | |
} | |
/* | |
* Complex find and replace based on DB setting | |
*/ | |
function itpd_the_content_with_setting($content){ | |
$itpd_find = get_option('itpd_find', ''); | |
if (!empty($itpd_find)){ | |
// allow for comma separated value | |
$itpd_find = explode( ",", $itpf_find); | |
$itpd_find = array_map( "trim", $itpd_find) ; | |
$itpd_replace = get_option( 'itpd_replace', '' ); | |
return str_ireplace( $itpd_find, $itpd_replace, $content ); | |
} | |
else { | |
return $content; | |
} | |
} | |
add_filter( 'the_content', 'itpd_the_content_with_setting' ); | |
// */ | |
// shortcodes in sidebars | |
add_filter( 'widget_text', 'shortcode_unautop'); | |
add_filter( 'widget_text', 'do_shortcode'); | |
/* | |
Some test | |
[itpd_hr] | |
More text | |
Link to post: [itpd_link id=1] | |
[itpd_color color="purple"]This text is purple...[/itpd_color] | |
FINDME in the body of this post. | |
This post is by JonDag. | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment